Bry*_*yko 5 objective-c ios apple-watch watchos-2 wcsession
我想让我的手表应用程序响应被杀死的手机上的父应用程序.当手表应用程序正在运行并且手机应用程序被杀死时,我得不到任何一个sessionReachabilityDidChange或的回叫sessionWatchStateDidChange.基于苹果文档:
调用此方法是为了让当前进程知道其对等会话的可达性已更改.
所以,似乎我应该得到一个回调.我在手表上将WCSession委托设置为我的班级.手表上的会话接收应用程序上下文的回调.为什么我没有获得可达性回调?
代码如下..
+ (SomeClass *)sharedInstance {
static dispatch_once_t pred;
static SomeClass *shared = nil;
dispatch_once(&pred, ^{
shared = [[SomeClass alloc] init];
[Model sharedInstance].delegate = shared;
});
return shared;
}
#pragma mark - setup
- (void)initializeSession {
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
[self sync];
}
}
-(BOOL) hasValidWCSession {
return ([WCSession isSupported] && [WCSession defaultSession].isReachable);
}
#pragma mark - WCSessionDelegate
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *, id> *)applicationContext {
NSLog(@"application context received on watch: %@", applicationContext);
[[Model sharedInstance] process:applicationContext];
}
- (void)sessionWatchStateDidChange:(WCSession *)session {
NSLog(@"wcession state changed on watch");
}
- (void)sessionReachabilityDidChange:(WCSession *)session {
NSLog(@"wcsession reachability changed on watch");
}
Run Code Online (Sandbox Code Playgroud)
reachable从Apple Watch应用程序的角度来看,iPhone应用程序不需要运行才能成为现实.如果iPhone本身已配对且可以访问,则使用手表应用程序中的WCSession发送消息将在后台启动iPhone应用程序.
因此,sessionReachabilityDidChange:当iOS应用程序被杀时,我不希望在监视应用程序上调用.这也意味着您的iPhone应用程序应该随时准备在后台启动,并立即激活WCSession,以处理来自监视应用程序的传入请求(类似于某些推送通知等可以启动iOS应用程序) .
但是,从iPhone应用程序的角度来看,只有当"配对且活跃的Apple Watch在范围内并且相关的Watch应用程序在前台运行"(文档)时才会认为手表应用程序对应物是可访问的.
另外,请注意,sessionWatchStateDidChange:当"WCSession对象的配对,watchAppInstalled,complicationEnabled或watchDirectoryURL属性中的值更改"时,将调用此方法.
| 归档时间: |
|
| 查看次数: |
1078 次 |
| 最近记录: |