您的位置:首页 > 其它

Watch 应用与 iPhone应用 之间传值

2015-10-15 22:30 204 查看
摘要: 本文简单介绍如何实现Apple watch 应用 与 iPhone应用之间进行值传递。

Apple Watch -----------» iPhone

[WKInterfaceController openParentApplication:@{@"type":@"song", @"para":@{@"channelID":channelID}} reply:^(NSDictionary *replyInfo, NSError *error) {
if(replyInfo){
// 对iPhone返回的数据进行解档
NSArray *array  = [NSKeyedUnarchiver unarchiveObjectWithData:[replyInfo objectForKey:@"songs"]];
}
}];


该方法是 WatchKit里面用来调用iPhone主体的接口,对应的要在iPhone端的AppDelegate中实现:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply NS_AVAILABLE_IOS(8_2);

*注意 ios8.2+才有该方法。

iPhone ---------------» apple watch

#pragma mark - WatchKit Data
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void(^)(NSDictionary *replyInfo))reply {
NSString *type = userInfo[@"type"];
NSDictionary *para = userInfo[@"para"];

NSDictionary *response = nil;
if ([type isEqualToString:@"song"])
{
//  response = @{@"songs":@"11111"};
NSArray *songArray = [NSArray arrayWithObjects: MySong1, Mysong2, nil];// Mysong 是自定义数据类型
respone = @{@"songs":[NSKeyedArchiver archivedDataWithRootObject:songArray]};
}
reply(respone);
}

*****注意******

此处数据如果是不可序列化的,需进行可序列化处理。数据归档和解档要对应。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: