从观看应用启动主机应用

时间:2014-12-30 06:55:08

标签: ios iphone watchkit apple-watch

我知道监视工具包扩展中的openParentApplication api可以在后台打开主机应用程序,但不能在前台打开。

我还尝试使用openUrl()的{​​{1}} api,如下所示:

NSExtensionContext

此处主机应用程序也未启动。我错过了什么吗?或者是不可能的 从手表套件扩展程序启动主机应用程序?

3 个答案:

答案 0 :(得分:21)

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

Apple Dev论坛上的多篇帖子提到它不可能

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

  

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

和其他帖子

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

  

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

答案 1 :(得分:12)

我希望Apple能够在未来的测试版中提供用于从手表应用程序启动父应用程序的API,但是现在我已经通过以下方式实现了它...

正常致电openParentApplication:reply:

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

在AppDelegate中实施application:handleWatchKitExtensionRequest:reply:,并使用自定义网址方案启动自己:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"myappscheme://open"]];
}

答案 2 :(得分: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)
在App Delegate中的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中

enter image description here

相关问题