我知道在watch kit扩展中的openParentApplication api可以在后台打开主机应用程序,但不能在前台打开.
我也尝试使用NSExtensionContext的openUrl()api,如下所示:
NSExtensionContext *ctx = [[NSExtensionContext alloc] init];
NSURL *url = [NSURL URLWithString:@"myScheme://today"];
[ctx openURL:url completionHandler:^(BOOL success) {
NSLog(@"fun=%s after completion. success=%d",__func__,success);
}];
[ctx completeRequestReturningItems:ctx.inputItems completionHandler:nil];
主机应用程序也没有启动.我错过了什么吗?或者是不可能的
从手表套件扩展程序启动主机应用程序?
解决方法
如果您需要在前台打开您的父应用程序,请使用Handoff!
https://developer.apple.com/handoff/
例:
在某个地方共享:
static let sharedUserActivityType = "com.yourcompany.yourapp.youraction" static let sharedIdentifierKey = "identifier"
在你的手表上
updateUserActivity(sharedUserActivityType,userInfo: [sharedIdentifierKey : 123456],webpageURL: nil)
在你的iPhone应用程序代表:
func application(application: UIApplication,willContinueUserActivityWithType userActivityType: String) -> Bool {
if (userActivityType == sharedUserActivityType) {
return true
}
return false
}
func application(application: UIApplication,continueUserActivity userActivity: NSUserActivity,restorationHandler: ([AnyObject]!) -> Void) -> Bool {
if (userActivity.activityType == sharedUserActivityType) {
if let userInfo = userActivity.userInfo as? [String : AnyObject] {
if let identifier = userInfo[sharedIdentifierKey] as? Int {
//Do something
let alert = UIAlertView(title: "Handoff",message: "Handoff has been triggered for identifier \(identifier)",delegate: nil,cancelButtonTitle: "Thanks for the info!")
alert.show()
return true
}
}
}
return false
}
最后(这一步很重要!!!):在你的Info.plist