[英]Communication with watch kit extension and iPhone app
I tried to send data from iPhone app to watch kit extension but fail. 我试图从iPhone应用程序发送数据到手表套件扩展但失败了。 And have no idea how to do it.
并且不知道该怎么做。
I tested on simulator and send data via app groups. 我在模拟器上测试并通过应用程序组发送数据。
Here is my steps: 这是我的步骤:
Add code at watchkit extension to read NSUserDefaults data. 在watchkit扩展中添加代码以读取NSUserDefaults数据。 ([NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:)
([NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier :)
Run watch kit app 运行手表套件应用程序
What necessary things I miss? 我想念什么必要的东西?
Or I should use another way to do this? 或者我应该用另一种方式来做到这一点?
All I want to do is to send data from iPhone to watch every seconds. 我想做的就是每隔几秒钟从iPhone发送数据。 The data could be string or image.
数据可以是字符串或图像。
Thanks 谢谢
To set communication between iPhone and iWatch I have been putting a file into the shared app group folder and monitor it from the Watch extension. 要设置iPhone和iWatch之间的通信,我一直将文件放入共享应用程序组文件夹,并从Watch扩展程序监视它。 If there is any change in iPhone app it modifies the file and the Watch extension will be notified and can act on it.
如果iPhone应用程序中有任何更改,则会修改该文件,并会通知Watch扩展程序并对其执行操作。
You need to: 你需要:
Create an app group on both targets (iPhone and Watch extension). 在两个目标(iPhone和Watch扩展程序)上创建一个应用程序组。 Make sure it is added on your App ID on apple developer portal (sometimes it doesn't pass well from Xcode).
确保它已添加到Apple开发人员门户网站上的App ID上(有时它不能很好地从Xcode传递)。
Write to the app group from the iPhone app: 从iPhone应用程序写入应用程序组:
NSString *destFolderPath = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: @"YOUR_GROUP_STRING"].path; [UIImageJPEGRepresentation(image, 1.0) writeToFile:[destFolderPath stringByAppendingPathComponent:@"Your_Shared_File_Name.jpg"] atomically:YES];
Read the file from the extension: 从扩展中读取文件:
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]; }
On general I suggest you to use MMWormhole which can be used easily if you don't have any too complex things you need to pass between the Watch and the Host app. 一般情况下,我建议您使用MMWormhole ,如果您没有任何太复杂的东西需要在Watch和Host应用程序之间传递,可以轻松使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.