Open URL scheme from iOS extension

2019-04-16 17:19发布

问题:

I have this code that return success = NO

[self.extensionContext openURL:[NSURL URLWithString:@"URLApp://"] completionHandler:^(BOOL success) {

     [self.extensionContext completeRequestReturningItems:nil completionHandler:nil];

 }];

So and I can't open containing app from my share extension when I debug it.

I've configured main target of contained app like this:

I've tested open URLApp:// from safari and it works for me.

I also used some examples provided here to understand how to open containing app using url scheme.

回答1:

EDIT: Ok, just a little correction here. I got it working with placing a button over the label just like suggested above and the following code:

 NSURL *url = [NSURL URLWithString:@"floblog://"];
 [self.extensionContext openURL:url completionHandler:nil]; 

I linked it to a "Touch Up Inside" event. However, this also causes the app to launch when the user scrolls the Today view.

=======================================

I ran into the same issue. However, it seems that there is no solution for now since the release notes for the first beta of iOS 8 mention:

Known Issues: openURL does not work from an extension.

So I guess we will at least have to wait until beta 2.



回答2:

I found this answer here by Julio Bailon:

UIWebView * webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
NSString *urlString = @"URLApp://";
NSString * content = [NSString stringWithFormat : @"<head><meta http-equiv='refresh' content='0; URL=%@'></head>", urlString];
[webView loadHTMLString:content baseURL:nil];
[self.view addSubview:webView];
[webView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:2.0];