Skip to content

Commit

Permalink
Merge pull request #120 from mingflifb/master
Browse files Browse the repository at this point in the history
Remove the need to check canOpenURL and just use openURL instead.
  • Loading branch information
nlutsenko committed Aug 18, 2015
2 parents f4f3f42 + 9fdf042 commit c6273b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
34 changes: 12 additions & 22 deletions Bolts/iOS/BFAppLinkNavigation.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,52 +94,42 @@ - (NSURL *)appLinkURLWithTargetURL:(NSURL *)targetUrl error:(NSError **)error {
}

- (BFAppLinkNavigationType)navigate:(NSError **)error {
// Find the first eligible/launchable target in the BFAppLink.
BFAppLinkTarget *eligibleTarget = nil;
for (BFAppLinkTarget *target in self.appLink.targets) {
if ([[UIApplication sharedApplication] canOpenURL:target.URL]) {
eligibleTarget = target;
break;
}
}

NSURL *appLinkURLToOpen = nil;
NSURL *openedURL = nil;
NSError *encodingError = nil;
BFAppLinkNavigationType retType = BFAppLinkNavigationTypeFailure;
if (eligibleTarget) {
NSURL *appLinkAppURL = [self appLinkURLWithTargetURL:eligibleTarget.URL error:&encodingError];

// Find the first eligible/launchable target in the BFAppLink.
for (BFAppLinkTarget *target in self.appLink.targets) {
NSURL *appLinkAppURL = [self appLinkURLWithTargetURL:target.URL error:&encodingError];
if (encodingError || !appLinkAppURL) {
if (error) {
*error = encodingError;
}
} else if ([[UIApplication sharedApplication] canOpenURL:appLinkAppURL]) {
} else if ([[UIApplication sharedApplication] openURL:appLinkAppURL]) {
retType = BFAppLinkNavigationTypeApp;
appLinkURLToOpen = appLinkAppURL;
openedURL = appLinkAppURL;
break;
}
}

if (!appLinkURLToOpen && self.appLink.webURL) {
if (!openedURL && self.appLink.webURL) {
// Fall back to opening the url in the browser if available.
NSURL *appLinkBrowserURL = [self appLinkURLWithTargetURL:self.appLink.webURL error:&encodingError];
if (encodingError || !appLinkBrowserURL) {
// If there was an error encoding the app link data, fail hard.
if (error) {
*error = encodingError;
}
} else if ([[UIApplication sharedApplication] canOpenURL:appLinkBrowserURL]) {
} else if ([[UIApplication sharedApplication] openURL:appLinkBrowserURL]) {
// This was a browser navigation.
retType = BFAppLinkNavigationTypeBrowser;
appLinkURLToOpen = appLinkBrowserURL;
openedURL = appLinkBrowserURL;
}
}

[self postAppLinkNavigateEventNotificationWithTargetURL:appLinkURLToOpen
[self postAppLinkNavigateEventNotificationWithTargetURL:openedURL
error:error ? *error : nil
type:retType];
if (appLinkURLToOpen) {
[[UIApplication sharedApplication] openURL:appLinkURLToOpen];
}
// Otherwise, navigation fails.
return retType;
}

Expand Down
10 changes: 8 additions & 2 deletions BoltsTests/AppLinkTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ - (NSURL *)dataUrlForHtml:(NSString *)html {
Swizzled-in replacement for UIApplication openUrl so that we can capture results.
*/
- (BOOL)openURLReplacement:(NSURL *)url {
[openedUrls addObject:url];
return YES;
if ([url.absoluteString hasPrefix:@"bolts://"]
|| [url.absoluteString hasPrefix:@"bolts2://"]
|| [url.absoluteString hasPrefix:@"http://"]
|| [url.absoluteString hasPrefix:@"file://"]) {
[openedUrls addObject:url];
return YES;
}
return NO;
}

/*!
Expand Down

0 comments on commit c6273b7

Please sign in to comment.