diff --git a/Bolts/iOS/BFAppLinkNavigation.m b/Bolts/iOS/BFAppLinkNavigation.m index bc4d58ca3..dd10d1483 100644 --- a/Bolts/iOS/BFAppLinkNavigation.m +++ b/Bolts/iOS/BFAppLinkNavigation.m @@ -94,31 +94,25 @@ - (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) { @@ -126,20 +120,16 @@ - (BFAppLinkNavigationType)navigate:(NSError **)error { 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; } diff --git a/BoltsTests/AppLinkTests.m b/BoltsTests/AppLinkTests.m index 4decea595..af0c39d7e 100644 --- a/BoltsTests/AppLinkTests.m +++ b/BoltsTests/AppLinkTests.m @@ -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; } /*!