我刚刚安装了Xcode的最新测试版,尝试Swift 2和对Apple Watch开发部分的改进。
我实际上很难想出为什么这个基本的NSUserDefaults方法可以分享iOS和Watch OS2之间的信息不起作用。
我跟着this step-by-step tutorial检查我是否错过了这个过程中的一些东西,就像打开同一个组的手机应用程序和扩展名,但这是我所得到的:没有。
这是我为iPhone应用程序中的ViewController编写的:
import UIKit class ViewController: UIViewController { @IBOutlet weak var lb_testo: UITextField! let shared_defaults:NSUserDefaults = NSUserDefaults(suiteName: "group.saracanducci.test")! var name_data:NSString? = "" override func viewDidLoad() { super.viewDidLoad() name_data = shared_defaults.stringForKey("shared") lb_testo.text = name_data as? String } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func upgrade_name(sender: AnyObject) { name_data = lb_testo.text shared_defaults.setObject(name_data,forKey: "shared") lb_testo.resignFirstResponder() shared_defaults.synchronize() } }
这就是我在WatchKit的InterfaceController中有的:
import WatchKit import Foundation class InterfaceController: WKInterfaceController { @IBOutlet var lb_nome: WKInterfaceLabel! let shared_defaults:NSUserDefaults = NSUserDefaults(suiteName: "group.saracanducci.test")! var name_data:NSString? = "" override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) } override func willActivate() { super.willActivate() if (shared_defaults.stringForKey("shared") != ""){ name_data = shared_defaults.stringForKey("shared") lb_nome.setText(name_data as? String) }else{ lb_nome.setText("No Value") } } override func didDeactivate() { super.didDeactivate() } }
我做了一些测试,看起来像iOS应用程序和Watch操作系统一个利用不同的组…他们不共享信息,他们在本地存储。
有人有同样的问题吗?任何想法如何解决?
使用手表OS2,您将无法再使用共享组容器。
Apple Docs:
原文链接:https://www.f2er.com/swift/320628.htmlWatch apps that shared data with their iOS apps using a shared group container must be redesigned to handle data differently. In watchOS 2,each process must manage its own copy of any shared data in the local container directory. For data that is actually shared and updated by both apps,this requires using the Watch Connectivity framework to move that data between them.