Skip to content

Commit

Permalink
fix(WKWebView): resolved crash with WebKit on iOS 9.x. (#342)
Browse files Browse the repository at this point in the history
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 ..
```
  • Loading branch information
jerolimov authored and Titozzz committed Feb 14, 2019
1 parent 833d4d6 commit f27f13e
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions ios/RNCWKWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,6 @@ @implementation RNCWKWebView
BOOL _savedHideKeyboardAccessoryView;
}

- (void)dealloc{}

/**
* See https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DisplayWebContent/Tasks/WebKitAvail.html.
*/
+ (BOOL)dynamicallyLoadWebKitIfAvailable
{
static BOOL _webkitAvailable=NO;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
NSBundle *webKitBundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/WebKit.framework"];
if (webKitBundle) {
_webkitAvailable = [webKitBundle load];
}
});

return _webkitAvailable;
}

- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
Expand Down Expand Up @@ -91,6 +71,11 @@ - (instancetype)initWithFrame:(CGRect)frame
return self;
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
* See https://stackoverflow.com/questions/25713069/why-is-wkwebview-not-opening-links-with-target-blank/25853806#25853806 for details.
*/
Expand All @@ -105,10 +90,6 @@ - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWe
- (void)didMoveToWindow
{
if (self.window != nil && _webView == nil) {
if (![[self class] dynamicallyLoadWebKitIfAvailable]) {
return;
};

WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
if (_incognito) {
wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
Expand Down Expand Up @@ -186,6 +167,7 @@ - (void)removeFromSuperview
[_webView.configuration.userContentController removeScriptMessageHandlerForName:MessageHandlerName];
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
[_webView removeFromSuperview];
_webView.scrollView.delegate = nil;
_webView = nil;
}

Expand Down

0 comments on commit f27f13e

Please sign in to comment.