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

[TIMOB-23888] iOS: Use asynchronous openURL API on iOS 10+ #9759

Merged
merged 9 commits into from
May 15, 2018
26 changes: 26 additions & 0 deletions apidoc/Titanium/Platform/Platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,39 @@ methods:
- name: openURL
summary: |
Opens this URL using the system's default application for its protocol.
description: |
**iOS Note**: For iOS 10 and later, this method is performed asynchronously
and will call the function passed as an (optional) third parameter instead
of returning a boolean synchronously.

Example:

Ti.Platform.openURL('myapp://', {
'UIApplicationOpenURLOptionUniversalLinksOnly': true
}, function(e) {
Ti.API.info('URL open successfully? ' + e.success);
});

platforms: [android, iphone, ipad]
returns:
type: Boolean
parameters:
- name: url
summary: The url to open.
type: String
- name: options
summary: |
The optional options to pass to the URL handling (iOS 10+). Pass a
dictionary with one or more of the following string-keys:
* `UIApplicationOpenURLOptionsSourceApplicationKey` (String value)
* `UIApplicationOpenURLOptionsAnnotationKey` (Array value)
* `UIApplicationOpenURLOptionsOpenInPlaceKey` (Boolean value)
* `UIApplicationOpenURLOptionUniversalLinksOnly` (Boolean value)
Read more about the available keys in the [Apple documentation](https://developer.apple.com/documentation/uikit/uiapplicationopenurloptionskey?language=objc).
type: Dictionary
- name: callback
summary: The optional callback that is called once the URL is opened (iOS 10+).
type: Callback

- name: is24HourTimeFormat
summary: Returns whether the system settings are configured to show times in 24-hour format.
Expand Down
28 changes: 27 additions & 1 deletion iphone/Classes/PlatformModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,34 @@ - (NSNumber *)openURL:(NSArray *)args
NSString *newUrlString = [args objectAtIndex:0];
NSURL *newUrl = [TiUtils toURL:newUrlString proxy:self];
BOOL result = NO;

// iOS 10+
KrollCallback *callback = nil;
NSMutableDictionary *options = [NSMutableDictionary dictionary];

if ([args count] >= 2) {
if ([[args objectAtIndex:1] isKindOfClass:[NSDictionary class]]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of count = 1, [args objectAtIndex:1] will fail. Due to this first test case is failing.

ENSURE_ARG_AT_INDEX(options, args, 1, NSMutableDictionary);
if ([args count] == 3) {
ENSURE_ARG_AT_INDEX(callback, args, 2, KrollCallback);
}
} else {
ENSURE_ARG_AT_INDEX(callback, args, 1, KrollCallback);
}
}

if (newUrl != nil) {
[[UIApplication sharedApplication] openURL:newUrl];
if ([TiUtils isIOS10OrGreater]) {
[[UIApplication sharedApplication] openURL:newUrl
options:options
completionHandler:^(BOOL success) {
if (callback != nil) {
[callback call:@[ @{ @"success" : @(success) } ] thisObject:self];
}
}];
} else {
result = [[UIApplication sharedApplication] openURL:newUrl];
}
}

return [NSNumber numberWithBool:result];
Expand Down
6 changes: 5 additions & 1 deletion iphone/Classes/TiApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ - (void)launchToUrl
UIApplication *app = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:[launchDefaults objectForKey:@"application-launch-url"]];
if ([app canOpenURL:url]) {
[app openURL:url];
if ([TiUtils isIOS10OrGreater]) {
[app openURL:url options:@{} completionHandler:nil];
} else {
[app openURL:url];
}
} else {
DebugLog(@"[WARN] The launch-url provided : %@ is invalid.", [launchDefaults objectForKey:@"application-launch-url"]);
}
Expand Down
6 changes: 5 additions & 1 deletion iphone/Classes/TiUIWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,11 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
UIApplication *uiApp = [UIApplication sharedApplication];

if ([uiApp canOpenURL:newUrl] && !willHandleUrl) {
[uiApp openURL:newUrl];
if ([TiUtils isIOS10OrGreater]) {
[uiApp openURL:newUrl options:@{} completionHandler:nil];
} else {
[uiApp openURL:newUrl];
}
return NO;
}

Expand Down