简体   繁体   English

在应用程序中检测WatchKit扩展的存在

[英]Detect WatchKit extension presence in app

I have an app that I want to behave a bit differently when it comes to updates and caching if a WatchKit extension is installed on your paired Apple Watch than if it is not. 我有一个应用程序,如果配对的Apple Watch上安装了WatchKit扩展程序,则我希望在更新和缓存方面的行为与其他应用程序有所不同。 If the watchkit extension has any chance of launching (you have paired a watch and the app is installed) then I want to do more heavy caching. 如果watchkit扩展程序有启动的机会(您已经将手表配对并安装了应用程序),那么我想进行更多的高速缓存。

Is there any way I can detect whether an Apple WatchKit Extension is installed on the Apple Watch from my iOS app? 有什么方法可以通过我的iOS应用程序检测Apple Watch上是否安装了Apple WatchKit Extension? Other than setting a flag first time it is launched and hope it isn't deleted thereafter? 除了在第一次启动时设置标志,并希望此后不将其删除?

Cheers 干杯

Nik 尼克

I'll reiterate the answer I gave here . 在这里重申我的答案。 There's no way to detect whether or not a Watch has been paired with the phone programmatically. 无法检测手表是否已通过编程方式与手机配对。

I don't know if this will work for you but what I have done is used userDefaults (group) to set a flag when the watch app is launched. 我不知道这是否对您有用,但是我执行的操作是在启动手表应用程序时使用userDefaults(组)设置标志。 Then I can check it in the main app. 然后,我可以在主应用程序中对其进行检查。 If the flag is set, I know the app has been installed. 如果设置了该标志,则表明该应用已安装。 Of course this is not bulletproof but it was the best I could come up with given the current limits of WatchKit. 当然这不是防弹的,但鉴于WatchKit的当前限制,这是我能想到的最好的方法。 It doesn't tell you if the phone has been paired but it tells you that your app has been installed on the watch and therefore the phone itself must be (or was at onetime) paired. 它不会告诉您手机是否已配对,但会告诉您您的应用程序已安装在手表上,因此手机本身必须(或曾经)配对过。

With WatchConnectivity framework you can check if your paired device is available and the app is running. 使用WatchConnectivity框架,您可以检查配对的设备是否可用并且该应用程序正在运行。

  • activate App Groups from "target -> Capabilities" 从“目标->功能”激活应用组
  • try this code on the device that needs to know the companion app is running. 在需要了解随播应用正在运行的设备上尝试使用此代码。

objective C: 目标C:

#import <WatchConnectivity/WatchConnectivity.h>

yourClass : Superclass <WCSessionDelegate>

WCSession* session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];

-(void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(NSError *)error
{
  if (activationState == WCSessionActivationStateActivated) {
      [[WCSession defaultSession] sendMessage:@{@"fromThisDevice":@"hello"} replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
          NSLog(@"reply %@", replyMessage[@"fromOtherDevice"]);
      } errorHandler:^(NSError * _Nonnull error) {
          NSLog(@"error %@", error.userInfo);
      }];
  }
}

- (void) session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler{
   NSLog(@"message %@", message[@"fromOtherDevice"]);
   replyHandler(@{@"fromThisDevice":@"hello"});
}

Swift: 迅速:

import WatchConnectivity

yourClass : Superclass, WCSessionDelegate

let session = WCSession.default()
session.delegate = self
session.activate()

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if activationState == WCSessionActivationState.activated{
        WCSession.default().sendMessage(["fromThisDevice" : "hello"], replyHandler: { (reply:[String : Any]) -> Void in
            print(reply["fromOtherDevice"] as Any)
        }, errorHandler: { (error) -> Void in
            print(error)
        })
    }
}

func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
    print(message["fromOtherDevice"] as Any)
    replyHandler(["fromThisDevice" : "hello"])
}

As of iOS 9, WCSession offers isWatchAppInstalled . 从iOS 9开始, WCSession提供了isWatchAppInstalled You can use it by importing WatchConnectivity ( @import WatchConnectivity; ), starting the session ( [[WCSession defaultSession] activateSession] ) and calling [[WCSession sharedSession] isWatchAppInstalled] . 您可以通过导入WatchConnectivity@import WatchConnectivity; ),启动会话( [[WCSession defaultSession] activateSession]并调用[[WCSession sharedSession] isWatchAppInstalled] See here: https://developer.apple.com/documentation/watchconnectivity/wcsession/1615623-iswatchappinstalled 请参阅此处: https//developer.apple.com/documentation/watchconnectivity/wcsession/1615623-iswatchappinstalled

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM