Browse Source

fix(WKWebView): resolved crash with WebKit on iOS 9.x. (#342)

This PR fixes two crashes when enableWebKit is true (which was the default) on iOS 9.x. The first one when the WebView component was mounted (fixes issue #150) and the second one, when the component was unmounted (fixes issue #213).

The first problem happen when the RNCWKWebView.m was loaded:

The programming guide for WebKit (version 2006) ([pdf](http://mirror.informatimago.com/next/developer.apple.com/documentation/Cocoa/Conceptual/DisplayWebContent/WebKit_DisplayWebContent.pdf)) from Apple said, that it is (was) required to check if the 'new' WebKit Framework was available. This was required when the WebKit framework was only available on Mac OS X (10.2) when the Safari was installed. 😆

Because WebKit is (currently..) a fix part of iOS we didn't need this check this anymore to determinate if WebKit is available. I also see no other reference that this is required in iOS so I just remove this code. Without this code the WebView works also on iOS 9.x.

The second issue happen when the WKWebView was removed from the native view hierarchy. Its required (only on iOS 9) to remove the UIScrollView delegate before cleaning up the webview.

Its possible to try this PR with:

```
npm install --save "react-native-webview@jerolimov/react-native-webview#fix-ios9-crash"
# or
yarn add "react-native-webview@jerolimov/react-native-webview#fix-ios9-crash"
```

If you use CocoaPods, you should also update your Pods with

```
cd ios && pod update && cd ..
```
Christoph Jerolimov 5 years ago
parent
commit
f27f13e931
1 changed files with 6 additions and 24 deletions
  1. 6
    24
      ios/RNCWKWebView.m

+ 6
- 24
ios/RNCWKWebView.m View File

43
   BOOL _savedHideKeyboardAccessoryView;
43
   BOOL _savedHideKeyboardAccessoryView;
44
 }
44
 }
45
 
45
 
46
-- (void)dealloc{}
47
-
48
-/**
49
- * See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html.
50
- */
51
-+ (BOOL)dynamicallyLoadWebKitIfAvailable
52
-{
53
-  static BOOL _webkitAvailable=NO;
54
-  static dispatch_once_t onceToken;
55
-
56
-  dispatch_once(&onceToken, ^{
57
-    NSBundle *webKitBundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/WebKit.framework"];
58
-    if (webKitBundle) {
59
-      _webkitAvailable = [webKitBundle load];
60
-    }
61
-  });
62
-
63
-  return _webkitAvailable;
64
-}
65
-
66
 - (instancetype)initWithFrame:(CGRect)frame
46
 - (instancetype)initWithFrame:(CGRect)frame
67
 {
47
 {
68
   if ((self = [super initWithFrame:frame])) {
48
   if ((self = [super initWithFrame:frame])) {
91
   return self;
71
   return self;
92
 }
72
 }
93
 
73
 
74
+- (void)dealloc
75
+{
76
+  [[NSNotificationCenter defaultCenter] removeObserver:self];
77
+}
78
+
94
 /**
79
 /**
95
  * See https://stackoverflow.com/questions/25713069/why-is-wkwebview-not-opening-links-with-target-blank/25853806#25853806 for details.
80
  * See https://stackoverflow.com/questions/25713069/why-is-wkwebview-not-opening-links-with-target-blank/25853806#25853806 for details.
96
  */
81
  */
105
 - (void)didMoveToWindow
90
 - (void)didMoveToWindow
106
 {
91
 {
107
   if (self.window != nil && _webView == nil) {
92
   if (self.window != nil && _webView == nil) {
108
-    if (![[self class] dynamicallyLoadWebKitIfAvailable]) {
109
-      return;
110
-    };
111
-
112
     WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
93
     WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
113
     if (_incognito) {
94
     if (_incognito) {
114
       wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
95
       wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
186
         [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHandlerName];
167
         [_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHandlerName];
187
         [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
168
         [_webView removeObserver:self forKeyPath:@"estimatedProgress"];
188
         [_webView removeFromSuperview];
169
         [_webView removeFromSuperview];
170
+        _webView.scrollView.delegate = nil;
189
         _webView = nil;
171
         _webView = nil;
190
     }
172
     }
191
 
173