【问题标题】:Is it possible to open your iPhone App using the Apple Watch?是否可以使用 Apple Watch 打开您的 iPhone 应用程序?
【发布时间】:2015-04-01 18:55:07
【问题描述】:

我想快速显示用户在 Apple Watch 上收到的文本摘要。然后,他们可以通过单击 iWatch 应用程序内的按钮来阅读 iPhone 上的其余文本。我认为目前这是不可能的,但如果是的话,请告诉我我需要做什么才能实现这一点。

【问题讨论】:

标签: ios cocoa-touch watchkit apple-watch


【解决方案1】:

您可以使用

在后台打开您的 iOS 应用
+ (BOOL)openParentApplication:(NSDictionary *)userInfo
                        reply:(void (^)(NSDictionary *replyInfo,
                                        NSError *error))reply

您的 iOS 应用将获得

- (void)application:(UIApplication *)application
handleWatchKitExtensionRequest:(NSDictionary *)userInfo
              reply:(void (^)(NSDictionary *replyInfo))reply

因此,如果您的 iOS 应用程序已经在运行,您可以使用它向应用程序提供信息。但是,您不能让您的 iOS 应用程序在前台运行,这必须由用户启动。您可以将其余文本保存到共享设置和/或使用openParentApplication 在后台将其传递给您的应用程序。然后,当用户打开您的 iOS 应用程序时,您可以向他们显示其余的文本。

【讨论】:

    【解决方案2】:

    如果您需要在前台打开父应用,请使用 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) 中

    【讨论】:

    • Apple Watch 没有 Handoff。它是 iOS 和 OS X 功能。
    • 我不能谈论它,但相信我:您可以从 Watch 切换到其他 Apple 设备。
    【解决方案3】:

    不,无法通过 Watch 在 iPhone 上打开您的应用,但您可以在后台打开您的应用。

    假设您想在手表上查看新闻,为此您需要数据。您可以通过 iPhone 应用获取该数据:openParentApplication:handleWatchKitExtensionRequest:

    如何做到这一点在这个答案中进行了解释: How to send data from iphone to watchkit in swift

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多