ios – 从手表应用程序启动主机应用程序

我知道在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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...