12

我知道openParentApplication手表套件扩展中的 api 可以在后台打开主机应用程序,但不能在前台打开。

我也尝试使用如下openUrl()的api NSExtensionContext

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];

这里也没有启动主机应用程序。我错过了什么吗?还是无法从手表套件扩展启动主机应用程序?

4

3 回答 3

22

从 iOS 8.2 的 Beta 3 开始,目前无法将 iOS 应用程序打开到前台。如您所说openParentApplication,可以在后台打开应用程序。不幸的是,没有迹象表明可以在 iPhone 上打开应用程序的 API。

Apple Dev Forums 上的多个帖子提到这是不可能的

https://devforums.apple.com/message/1076125#1076125

正确,通知仍然可以声明 iPhone 应用程序将处理的后台操作,因此在这个意义上它可以启动 iPhone 应用程序。但是 iPhone 应用程序不能通过 WatchKit 应用程序被带到前台。

和其他帖子

https://devforums.apple.com/message/1082620#1082620

在设备上,它[Watch app] 不会 - 将您的 iOS 应用程序带到前台。

于 2015-01-02T12:25:55.253 回答
12

我希望Apple将在未来的测试版中提供用于从手表应用程序启动父应用程序的API,但现在我已经设法通过执行以下操作来实现它......

正常调用openParentApplication:reply:

- (void)openPhoneApp {
    [WKInterfaceController openParentApplication:[NSDictionary new] reply:nil];
}

application:handleWatchKitExtensionRequest:reply:在您的 AppDelegate 中实现,并使用自定义 URL 方案启动自身:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myappscheme://open"]];
}
于 2015-02-01T17:07:59.223 回答
9

如果您需要在前台打开您的父应用程序,请使用 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 上的 App Delegate 中:

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(s)

在此处输入图像描述

于 2015-04-04T16:50:53.957 回答