App Extensions里不能直接调用openurl的方式去打开app,sdk提供了一种方法:
// Asks the host to open an URL on the extension's behalf
- (void)openURL:(NSURL *)URL completionHandler:(void (^ __nullable)(BOOL success))completionHandler;
但是这种方法目前只能适用于today extension(其他的extension暂时不可用)。
通过网上查找资料,目前搜集到两个方法:
第一种,sdk版本要8.3以上(包括8.3,9.2测试还可以用)
// Prepare the URL request
// this will use the custom url scheme of your app
// and the paths to the photos you want to share:
NSString * urlString = [ NSString stringWithFormat: @"%@://%@", APP_SHARE_URL_SCHEME, ( NULL == invokeArgs ? @"" : invokeArgs ) ];
NSURL * url = [ NSURL URLWithString: urlString ];
NSString *className = @"UIApplication";
if ( NSClassFromString( className ) )
{
id object = [ NSClassFromString( className ) performSelector: @selector( sharedApplication ) ];
[ object performSelector: @selector( openURL: ) withObject: url ];
}
第二种,sdk版本低于8.3的
// openURL doesn't work for any extension, but the Today one,
// so we'll use a workaround:
// 1. Instantiate a UIWebView that's so tiny it won't show on the screen:
UIWebView * webView = [ [ UIWebView alloc ] initWithFrame: CGRectMake( 0, 0, 0, 0 ) ];
// 2. Prepare the URL request for the UIWebView:
// this will use the custom url scheme of your app
// and the paths to the photos you want to share:
NSString * urlString = [ NSString stringWithFormat: @"%@://%@", APP_SHARE_URL_SCHEME, ( NULL == invokeArgs ? @"" : invokeArgs ) ];
NSURL * url = [ NSURL URLWithString: urlString ];
NSURLRequest * request = [ NSURLRequest requestWithURL: url ];
// 3. Now get the UIWebView to load the request, which will launch the app:
[ webView loadRequest: request ];
[ self.view addSubview: webView ];
// 4. And finally, say bye to the UIWebView.
// The delay is important here, otherwise the UIWebView won't have time
// to invoke your app before it's dismissed:
[ webView performSelector: @selector( removeFromSuperview ) withObject: NULL afterDelay: 2.0 ];
以上主要方法来自http://easynativeextensions.com/how-to-launch-your-app-from-the-ios-8-share-menu/ 这篇文章