Ios 监视连接不发送数据

Ios 监视连接不发送数据,ios,objective-c,watchkit,apple-watch,watchconnectivity,Ios,Objective C,Watchkit,Apple Watch,Watchconnectivity,我正在使用WatchConnectivity将一本简单的词典从iPhone发送到Apple Watch 在Apple Watch方面,为了避免在打开应用程序时上下文可能不会排队这一事实,在设置我的WatchKit表时,如果队列中没有任何内容,则最后接收的数据将保存到UserDefaults并检索。我已经在另一个WatchKit应用程序中实现了这一点,一切都运行得很好,但在这一个应用程序中,手表从未接收到数据 我只在模拟器中尝试过,因为我的应用程序在我的手表上永远旋转,而且从不加载(加载屏幕看起来

我正在使用WatchConnectivity将一本简单的词典从iPhone发送到Apple Watch

在Apple Watch方面,为了避免在打开应用程序时上下文可能不会排队这一事实,在设置我的WatchKit表时,如果队列中没有任何内容,则最后接收的数据将保存到UserDefaults并检索。我已经在另一个WatchKit应用程序中实现了这一点,一切都运行得很好,但在这一个应用程序中,手表从未接收到数据

我只在模拟器中尝试过,因为我的应用程序在我的手表上永远旋转,而且从不加载(加载屏幕看起来像WatchOs 1屏幕?)。WatchConnectivity框架包含在每个产品(扩展和iPhone应用程序)中。谢谢你的帮助

以下是iPhone代码(在ViewController中实现):

苹果手表代码:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    // Configure interface objects here.
    self.profiles = [[NSMutableArray alloc] init];

    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }

    [self setupTable];

}

- (void)session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *,id> *)applicationContext {
    NSDictionary *receieved = [[NSDictionary alloc] init];
    receieved = applicationContext;

    NSMutableArray *profiles = [[NSMutableArray alloc] init];
    profiles = [receieved objectForKey:@"profiles"];

    self.profiles = [[NSMutableArray alloc] init];
    self.profiles = profiles;

    NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:self.profiles];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:arrayData forKey:@"bookmarks"];

    [self setupTable];
    NSLog(@"new");
}

- (void)setupTable {

    ...
    After some setup code

    if (self.profiles.count == 0) {
        NSLog(@"Nothing in the queue, checking defaults");
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *got = [defaults objectForKey:@"bookmarks"];
        NSLog(@"Got Defaults!");
        self.profiles = [[NSMutableArray alloc] init];
        self.profiles = [NSKeyedUnarchiver unarchiveObjectWithData:got];
    }

    ...
    More setup code later
}
-(void)唤醒上下文:(id)上下文{
[super awakeWithContext:context];
//在此处配置接口对象。
self.profiles=[[NSMutableArray alloc]init];
如果([WCSession isSupported]){
WCSession*会话=[WCSession defaultSession];
session.delegate=self;
[会话激活会话];
}
[自设置表];
}
-(void)session:(WCSession*)session didReceiveApplicationContext:(NSDictionary*)applicationContext{
NSDictionary*received=[[NSDictionary alloc]init];
received=应用程序上下文;
NSMUTABLEARRY*配置文件=[[NSMUTABLEARRY alloc]init];
profiles=[received objectForKey:@“profiles”];
self.profiles=[[NSMutableArray alloc]init];
self.profiles=配置文件;
NSData*arrayData=[NSKeyedArchivedDatawithRootObject:self.profiles];
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
[默认设置setObject:arrayData forKey:@“书签”];
[自设置表];
NSLog(“新”);
}
-(无效)设置表{
...
经过一些设置代码
如果(self.profiles.count==0){
NSLog(@“队列中没有任何内容,正在检查默认值”);
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
NSData*got=[defaults objectForKey:@“bookmarks”];
NSLog(@“获得默认值!”);
self.profiles=[[NSMutableArray alloc]init];
self.profiles=[NSKeyedUnarchiver unarchiveObjectWithData:get];
}
...
更多的安装代码稍后
}
更改此行:

[[WCSession defaultSession] updateApplicationContext:toPass error:nil];
将是:

NSError *error = nil;
If (![[WCSession defaultSession] updateApplicationContext:toPass error:&error]) {
    NSLog(@"error: %@", error);
}
我打赌你会看到你得到了一个返回的错误


另外,
AppDelegate.profiles
包含什么类型的对象?

Aha!很好的调用-它的意思是“负载包含不支持的类型”。它是一个具有两个NSString属性的自定义对象数组。将阵列归档为NSData NSKeyedArchiver有效吗?尽管苹果的工程师们在Watch Connectivity WWDC会谈期间表示,特别建议不要使用NSKeyedArchiver,因为它没有针对空间效率进行优化。一个解决方案是将自定义对象转换为包含变量名=>NSStringMapping的字典谢谢您的帮助,了解空间优化非常好。我想在发送之前我会把它编成词典。
NSError *error = nil;
If (![[WCSession defaultSession] updateApplicationContext:toPass error:&error]) {
    NSLog(@"error: %@", error);
}