检测应用程序中是否存在 WatchKit 扩展
Detect WatchKit extension presence in app
我有一个应用程序,在更新和缓存方面,如果配对的 Apple Watch 上安装了 WatchKit 扩展程序,我希望它的行为与未安装时有所不同。如果 watchkit 扩展有任何启动的机会(你已经配对了手表并且安装了应用程序)那么我想做更多的重缓存。
有什么方法可以从我的 iOS 应用检测 Apple Watch 上是否安装了 Apple WatchKit 扩展?除了第一次启动时设置标志并希望此后不要删除它?
干杯
尼克
我会重申我给出的答案。无法通过编程方式检测手表是否已与 phone 配对。
我不知道这是否适合您,但我所做的是使用 userDefaults (group) 在手表应用程序启动时设置一个标志。然后我可以在主应用程序中查看它。如果设置了标志,我知道该应用程序已安装。当然,这不是万无一失的,但考虑到 WatchKit 的当前限制,这是我能想到的最好的方法。它不会告诉您 phone 是否已配对,但会告诉您您的应用程序已安装在手表上,因此 phone 本身必须(或曾经)配对。
使用 WatchConnectivity 框架,您可以检查您的配对设备是否可用以及应用是否可用 运行。
- 从 "target -> Capabilities"
激活应用组
- 在需要知道配套应用的设备上尝试此代码 运行。
objective 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"])
}
截至 iOS 9,WCSession
提供 isWatchAppInstalled
。您可以通过导入 WatchConnectivity
(@import WatchConnectivity;
)、启动会话 ([[WCSession defaultSession] activateSession]
) 和调用 [[WCSession sharedSession] isWatchAppInstalled]
来使用它。看这里:https://developer.apple.com/documentation/watchconnectivity/wcsession/1615623-iswatchappinstalled
我有一个应用程序,在更新和缓存方面,如果配对的 Apple Watch 上安装了 WatchKit 扩展程序,我希望它的行为与未安装时有所不同。如果 watchkit 扩展有任何启动的机会(你已经配对了手表并且安装了应用程序)那么我想做更多的重缓存。
有什么方法可以从我的 iOS 应用检测 Apple Watch 上是否安装了 Apple WatchKit 扩展?除了第一次启动时设置标志并希望此后不要删除它?
干杯
尼克
我会重申我给出的答案
我不知道这是否适合您,但我所做的是使用 userDefaults (group) 在手表应用程序启动时设置一个标志。然后我可以在主应用程序中查看它。如果设置了标志,我知道该应用程序已安装。当然,这不是万无一失的,但考虑到 WatchKit 的当前限制,这是我能想到的最好的方法。它不会告诉您 phone 是否已配对,但会告诉您您的应用程序已安装在手表上,因此 phone 本身必须(或曾经)配对。
使用 WatchConnectivity 框架,您可以检查您的配对设备是否可用以及应用是否可用 运行。
- 从 "target -> Capabilities" 激活应用组
- 在需要知道配套应用的设备上尝试此代码 运行。
objective 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"])
}
截至 iOS 9,WCSession
提供 isWatchAppInstalled
。您可以通过导入 WatchConnectivity
(@import WatchConnectivity;
)、启动会话 ([[WCSession defaultSession] activateSession]
) 和调用 [[WCSession sharedSession] isWatchAppInstalled]
来使用它。看这里:https://developer.apple.com/documentation/watchconnectivity/wcsession/1615623-iswatchappinstalled