我正在开发我们应用程序的 WatchOS 2 版本,并且坚持这样一个事实,即我只能发送一个带有函数“updateApplicationContext:(NSDictionary *)”的字符串。
我希望我可以制作一个带有一些变量的 NSObject 以作为此 NSDictionary 中的对象发送。几天后,我仍然没有找到解决这个问题的方法。无论如何要通过函数发送 NSObject 吗?
如果没有办法发送 NSObject 是否可以发送类似结构的东西?
我的代码如下:
-(void)updateWatchData
{
//Objective-C
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
if(session.isPaired) {
//Objective-C
DLog("Sending watch data");
NSMutableArray *kalenderData = [[NSMutableArray alloc] init];
for (int i = 0; i < self.dataContainerViews.count; i++) {
DataContainerView *container = self.dataContainerViews[i];
if(container.listType == ListGPKalender){
for(int j = 0; j < container.tableArray.count; j++){
GrandPrix *gp = container.tableArray[j];
WatchGrandPrix *watchGp = [[WatchGrandPrix alloc] init];
watchGp.gpnaam = gp.gpnaam;
[kalenderData addObject:watchGp];
}
}
}
NSArray *keys = [NSArray arrayWithObjects:@"kalenderData", nil];
NSArray *objects = [NSArray arrayWithObjects:kalenderData, nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSError *anyError;
if([[WCSession defaultSession] updateApplicationContext:dictionary error:&anyError]){
DLog("Data Send!");
}else{
DLog("Failed to send data with error: %@", anyError);
}
}
}
}
NSObject 只是:
@interface WatchGrandPrix : NSObject
@property(nonatomic,strong) NSString *gpnaam;
@end
我得到的错误是
Failed to send data with error: Error Domain=WCErrorDomain Code=7010 "Payload contains unsupported type." UserInfo={NSLocalizedDescription=Payload contains unsupported type., NSLocalizedRecoverySuggestion=Only pass valid types.}
谢谢!
最佳答案
我一直在通过将自定义对象序列化为 Json 字符串来发送自定义对象。我创建了一个 github project为此目的快速序列化任何对象。它包括一个演示如何进行序列化/反序列化的 Playground 。
关于ios - 通过 watchConnectivity 发送一个 NSObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34546253/