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

Remove the need to check canOpenURL and just use openURL instead. #120

Merged
merged 1 commit into from
Aug 18, 2015
Merged
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
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