NSUserDefaults在Watch OS2的Xcode beta上不起作用 [英] NSUserDefaults not working on Xcode beta with Watch OS2

查看:79
本文介绍了NSUserDefaults在Watch OS2的Xcode beta上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了最新的Xcode Beta,以尝试 Swift 2 和对Apple Watch开发部分所做的改进.

I just installed the latest beta of Xcode to try Swift 2 and the improvements made to the Apple Watch development section.

我实际上很难弄清楚为什么这种基本的NSUserDefaults方法无法在 iOS Watch OS2 之间共享信息的原因.

I'm actually having an hard time figuring out WHY this basic NSUserDefaults method to share informations between iOS and Watch OS2 isn't working.

我遵循了分步指南教程来检查我是否在此过程中遗漏了一些东西,例如为电话应用程序和分机同时打开同一组,但这是我得到的:没有.

这是我在iPhone应用程序中为ViewController编写的内容:

Here's what I wrote for the ViewController in the iPhone app:

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中拥有的东西:

And here's what I have in the InterfaceController for WatchKit:

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 OS可以利用不同的组... 它们不共享信息,而是将其存储在本地.

I made some tests and it seems like the iOS app and the Watch OS one take advantage of different groups...they're not sharing information, they store them locally.

有人遇到同样的问题吗?知道如何解决吗?

Is someone having the same issue? Any idea how to fix it?

推荐答案

使用watch OS2,您将无法再使用共享组容器.

With watch OS2 you can no longer use shared group containers. Apple Docs:

观看使用共享组与其iOS应用共享数据的应用 必须重新设计容器以不同方式处理数据.在watchOS 2中, 每个进程都必须管理自己在本地的任何共享数据的副本 容器目录.对于实际共享和更新的数据 这两个应用程序,这都需要使用Watch Connectivity框架 在它们之间移动数据.

Watch 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.

这篇关于NSUserDefaults在Watch OS2的Xcode beta上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆