[英]Communication with watch kit extension and iPhone app
我试图从iPhone应用程序发送数据到手表套件扩展但失败了。 并且不知道该怎么做。
我在模拟器上测试并通过应用程序组发送数据。
这是我的步骤:
在watchkit扩展中添加代码以读取NSUserDefaults数据。 ([NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier :)
运行手表套件应用程序
我想念什么必要的东西?
或者我应该用另一种方式来做到这一点?
我想做的就是每隔几秒钟从iPhone发送数据。 数据可以是字符串或图像。
谢谢
要设置iPhone和iWatch之间的通信,我一直将文件放入共享应用程序组文件夹,并从Watch扩展程序监视它。 如果iPhone应用程序中有任何更改,则会修改该文件,并会通知Watch扩展程序并对其执行操作。
你需要:
在两个目标(iPhone和Watch扩展程序)上创建一个应用程序组。 确保它已添加到Apple开发人员门户网站上的App ID上(有时它不能很好地从Xcode传递)。
从iPhone应用程序写入应用程序组:
NSString *destFolderPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"YOUR_GROUP_STRING"].path; [UIImageJPEGRepresentation(image, 1.0) writeToFile:[destFolderPath stringByAppendingPathComponent:@"Your_Shared_File_Name.jpg"] atomically:YES];
从扩展中读取文件:
UIImage *image; NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"YOUR_GROUP_STRING"]; NSString *imagePath = [groupURL URLByAppendingPathComponent:@"Your_Shared_File_Name.jpg"].path; } if( [[NSFileManager defaultManager] fileExistsAtPath: imagePath]){ image = [[UIImage alloc] initWithContentsOfFile: imagePath]; }
一般情况下,我建议您使用MMWormhole ,如果您没有任何太复杂的东西需要在Watch和Host应用程序之间传递,可以轻松使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.