简体   繁体   English

与手表套件扩展和iPhone应用程序通信

[英]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: 这是我的步骤:

  1. download watch kit catalog sample app from apple 从苹果下载手表套件目录样本应用程序
  2. Add App groups (XXX.XXX.XXX) for iPhone app and wtach kit extension 为iPhone应用程序和wtach套件扩展添加应用程序组(XXX.XXX.XXX)
  3. Add RequestOpenAccess: YES for NSExtensionAttributes (watch kit extension) 为NSExtensionAttributes添加RequestOpenAccess:YES(监视工具包扩展)
  4. Add a button to iPhone app that will write a value to NSUserDefaults with the specific Suite Name 向iPhone应用程序添加一个按钮,该按钮将使用特定的套件名称向NSUserDefaults写入值
  5. Add code at watchkit extension to read NSUserDefaults data. 在watchkit扩展中添加代码以读取NSUserDefaults数据。 ([NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:) ([NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier :)

  6. Run watch kit app 运行手表套件应用程序

  7. manual click the Catalog app in the iPhone simulator 手动单击iPhone模拟器中的目录应用程序
  8. click the button added at step 4 单击步骤4中添加的按钮
  9. add break point at watchkit extension to see if the NSUserDefaults value changed 在watchkit扩展中添加断点以查看NSUserDefaults值是否已更改
  10. Nothing happen 什么都没发生

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: 你需要:

  1. 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传递)。

  2. 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]; 
  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM