Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复dealloc方法移除通知时崩溃bug #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ API_AVAILABLE(ios(7.0))
@property(readonly, nonatomic) NSURL *URL;
/// Shows tool bar. Default is YES.
@property(assign, nonatomic) BOOL showsToolBar;

///记录有没有跑过视图加载方法 如果没有跑过 就无需移除通知KVO
@property(assign, nonatomic) BOOL hasRunViewDidLoadMethod;

/// Shows background description label. Default is YES.
@property(assign, nonatomic) BOOL showsBackgroundLabel;
/// Shows navigation close bar button item. Default is YES.
Expand Down Expand Up @@ -206,7 +210,7 @@ API_AVAILABLE(ios(7.0))
/// @param baseURL a baseURL to be loaded.
///
/// @return a instance of `AXWebViewController`.
- (instancetype)initWithHTMLString:(NSString*)HTMLString baseURL:(NSURL*)baseURL;
- (instancetype)initWithHTMLString:(NSString *)HTMLString baseURL:(NSURL * _Nullable)baseURL;
/// Load a new url.
///
/// @param URL a new url.
Expand Down
47 changes: 30 additions & 17 deletions AXWebViewController/AXWebViewController/AXWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB

- (void)initializer {
// Set up default values.
_hasRunViewDidLoadMethod = NO;//init 方法里还没有跑过
_showsToolBar = YES;
_showsBackgroundLabel = YES;
_showsNavigationCloseBarButtonItem = YES;
Expand Down Expand Up @@ -342,7 +343,7 @@ - (void)viewDidLoad {
[self loadURLRequest:_request];
} else if (_URL) {
[self loadURL:_URL];
} else if (_baseURL && _HTMLString) {
} else if (/*_baseURL && */_HTMLString) {
[self loadHTMLString:_HTMLString baseURL:_baseURL];
} else {
// Handle none resource case.
Expand All @@ -363,6 +364,8 @@ - (void)viewDidLoad {

// [_webView.scrollView addObserver:self forKeyPath:@"backgroundColor" options:NSKeyValueObservingOptionNew context:NULL];
#endif

_hasRunViewDidLoadMethod = YES;
}

- (void)viewDidLayoutSubviews {
Expand Down Expand Up @@ -504,10 +507,17 @@ - (void)dealloc {
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
_webView.UIDelegate = nil;
_webView.navigationDelegate = nil;
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
[_webView removeObserver:self forKeyPath:@"scrollView.contentOffset"];
[_webView removeObserver:self forKeyPath:@"title"];
// [_webView.scrollView removeObserver:self forKeyPath:@"backgroundColor"];


if (_hasRunViewDidLoadMethod) {//如果加载过 再进行移除 防止崩溃
[_webView removeObserver:self forKeyPath:@"estimatedProgress"];
[_webView removeObserver:self forKeyPath:@"scrollView.contentOffset"];
[_webView removeObserver:self forKeyPath:@"title"];
// [_webView.scrollView removeObserver:self forKeyPath:@"backgroundColor"];
}



#else
_webView.delegate = nil;
#endif
Expand Down Expand Up @@ -1141,15 +1151,22 @@ - (void)stopClicked:(UIBarButtonItem *)sender {
#endif
}

- (void)actionButtonClicked:(id)sender {
- (void)actionButtonClicked:(UIBarButtonItem *)sender {
NSArray *activities = @[[AXWebViewControllerActivitySafari new], [AXWebViewControllerActivityChrome new]];
NSURL *URL;
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
URL = _webView.URL;
#else
URL = _webView.request.URL;
#endif

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[URL] applicationActivities:activities];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIPopoverPresentationController *popover = activityController.popoverPresentationController;
popover.barButtonItem = sender;
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}

[self presentViewController:activityController animated:YES completion:nil];
}

Expand Down Expand Up @@ -1301,8 +1318,10 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
// For appstore and system defines. This action will jump to AppStore app or the system apps.
if ([[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'https://itunes.apple.com/' OR SELF BEGINSWITH[cd] 'mailto:' OR SELF BEGINSWITH[cd] 'tel:' OR SELF BEGINSWITH[cd] 'telprompt:'"] evaluateWithObject:components.URL.absoluteString]) {
if ([[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'https://itunes.apple.com/'"] evaluateWithObject:components.URL.absoluteString] && !_reviewsAppInAppStore) {
[[AXPracticalHUD sharedHUD] showSimpleInView:self.view.window text:nil detail:nil configuration:^(AXPracticalHUD *HUD) {
HUD.lockBackground = YES;
[[AXPracticalHUD sharedHUD] showNormalInView:self.view.window text:nil detail:nil configuration:^(AXPracticalHUD *HUD) {
// Disabled the background touching lock to fix the
// issue: https://github.com/devedbox/AXWebViewController/issues/67
// HUD.lockBackground = YES;
HUD.removeFromSuperViewOnHide = YES;
}];
SKStoreProductViewController *productVC = [[SKStoreProductViewController alloc] init];
Expand All @@ -1316,7 +1335,6 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
[productVC loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: @([[components.URL.absoluteString substringWithRange:range] integerValue])} completionBlock:^(BOOL result, NSError * _Nullable error) {
if (!result || error) {
[[AXPracticalHUD sharedHUD] showErrorInView:self.view.window text:error.localizedDescription detail:nil configuration:^(AXPracticalHUD *HUD) {
HUD.lockBackground = YES;
HUD.removeFromSuperViewOnHide = YES;
}];
[[AXPracticalHUD sharedHUD] hide:YES afterDelay:1.5 completion:NULL];
Expand All @@ -1332,7 +1350,6 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
}
}
if ([[UIApplication sharedApplication] canOpenURL:components.URL]) {

if (@available(iOS 10.0, *)) {
[UIApplication.sharedApplication openURL:components.URL options:@{} completionHandler:NULL];
} else {
Expand All @@ -1342,12 +1359,11 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
decisionHandler(WKNavigationActionPolicyCancel);
return;
} else if (![[NSPredicate predicateWithFormat:@"SELF MATCHES[cd] 'https' OR SELF MATCHES[cd] 'http' OR SELF MATCHES[cd] 'file' OR SELF MATCHES[cd] 'about'"] evaluateWithObject:components.scheme]) {// For any other schema but not `https`、`http` and `file`.

if (@available(iOS 8.0, *)) { // openURL if ios version is low then 8 , app will crash
if (!self.checkUrlCanOpen || [[UIApplication sharedApplication] canOpenURL:components.URL]) {
if (@available(iOS 10.0, *)) {
[UIApplication.sharedApplication openURL:components.URL options:@{} completionHandler:NULL];
}else{
} else {
[[UIApplication sharedApplication] openURL:components.URL];
}
}
Expand All @@ -1357,13 +1373,12 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati
}
}


decisionHandler(WKNavigationActionPolicyCancel);
return;
}

// URL actions for 404 and Errors:
if ([navigationAction.request.URL.absoluteString isEqualToString:kAX404NotFoundURLKey] || [navigationAction.request.URL.absoluteString isEqualToString:kAXNetworkErrorURLKey]) {
if ([[NSPredicate predicateWithFormat:@"SELF ENDSWITH[cd] %@ OR SELF ENDSWITH[cd] %@", kAX404NotFoundURLKey, kAXNetworkErrorURLKey] evaluateWithObject:components.URL.absoluteString]) {
// Reload the original URL.
[self loadURL:_URL];
}
Expand Down Expand Up @@ -1467,8 +1482,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
// For appstore.
if ([[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'https://itunes.apple.com/' OR SELF BEGINSWITH[cd] 'mailto:' OR SELF BEGINSWITH[cd] 'tel:' OR SELF BEGINSWITH[cd] 'telprompt:'"] evaluateWithObject:request.URL.absoluteString]) {
if ([[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'https://itunes.apple.com/'"] evaluateWithObject:components.URL.absoluteString] && !_reviewsAppInAppStore) {
[[AXPracticalHUD sharedHUD] showSimpleInView:self.view.window text:nil detail:nil configuration:^(AXPracticalHUD *HUD) {
HUD.lockBackground = YES;
[[AXPracticalHUD sharedHUD] showNormalInView:self.view.window text:nil detail:nil configuration:^(AXPracticalHUD *HUD) {
HUD.removeFromSuperViewOnHide = YES;
}];
SKStoreProductViewController *productVC = [[SKStoreProductViewController alloc] init];
Expand All @@ -1482,7 +1496,6 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
[productVC loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: @([[components.URL.absoluteString substringWithRange:range] integerValue])} completionBlock:^(BOOL result, NSError * _Nullable error) {
if (!result || error) {
[[AXPracticalHUD sharedHUD] showErrorInView:self.view.window text:error.localizedDescription detail:nil configuration:^(AXPracticalHUD *HUD) {
HUD.lockBackground = YES;
HUD.removeFromSuperViewOnHide = YES;
}];
[[AXPracticalHUD sharedHUD] hide:YES afterDelay:1.5 completion:NULL];
Expand Down