简体   繁体   English

NSUserDefaults不在Apple Watch上更新

[英]NSUserDefaults not updating on Apple Watch

I have added a group to both my iphone target and apple watch target: 我已将一组添加到我的iphone目标和Apple Watch目标:

在此处输入图片说明

And in my AppDelegate I have this code: 在我的AppDelegate中,我有以下代码:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let userDefaults = NSUserDefaults(suiteName: "group.tripicons")
    userDefaults!.setObject("English", forKey: "languageOfCurrentCountry")
    userDefaults!.setObject("English", forKey: "languageOfDevice")
    userDefaults!.synchronize()
    return true
  }

However, when it says it's nil in my method: 但是,当它说在我的方法中nil时:

class LanguageManager: NSObject {

  ...
  let userDefaults = NSUserDefaults(suiteName: "group.tripicons")

  func watchTranslation(word: String) -> String {
    print(userDefaults?.dictionaryRepresentation())

    // BREAKS HERE
    let language = userDefaults?.objectForKey("languageOfCurrentCountry") as! String // <---- BREAKS HERE
    ...
  }
}

The print statement prints an object without the key that I'm looking for: print语句打印一个没有我要查找的键的对象:

Optional(["MSVLoggingMasterSwitchEnabledKey": 0, "AppleKeyboardsExpanded": 1, "AppleLanguages": (
    en
), "AppleLanguagesDidMigrate": 2.1, "NSInterfaceStyle": macintosh, "NSLanguages": (
    en
), "AppleKeyboards": (
    "en_US@hw=US;sw=QWERTY",
    "emoji@sw=Emoji",
    "en_US@hw=US;sw=QWERTY"
)])

This is even when I open up the iphone app before I open the apple watch app. 即使在我打开Apple Watch应用程序之前打开iphone应用程序时,也是如此。 Am I missing something? 我想念什么吗?

== UPDATE == ==更新==

from AppDelegate:
["languageOfCurrentCountry": English, "AppleKeyboards": (
    "en_US@hw=US;sw=QWERTY",
    "emoji@sw=Emoji",
    "en_US@hw=US;sw=QWERTY"
), "AppleKeyboardsExpanded": 1, "AddingEmojiKeybordHandled": 1, "AppleLanguages": (
    "en-US"
), "ApplePasscodeKeyboards": (
    "en_US@hw=US;sw=QWERTY",
    "emoji@sw=Emoji",
    "en_US@hw=US;sw=QWERTY"
), "AppleLocale": en_US, "NSInterfaceStyle": macintosh, "MSVLoggingMasterSwitchEnabledKey": 0, "NSLanguages": (
    "en-US",
    en
), "AppleITunesStoreItemKinds": (
    audiobook,
    "tv-episode",
    booklet,
    software,
    "software-update",
    "itunes-u",
    ringtone,
    "tv-season",
    movie,
    mix,
    wemix,
    song,
    tone,
    artist,
    "podcast-episode",
    podcast,
    eBook,
    document,
    album,
    "music-video"
), "AppleLanguagesDidMigrate": 9.2, "languageOfDevice": English]

The exchange of data using NSUserDefaults between iPhone and Apple Watch is only available for watchOS 1. 在iPhone和Apple Watch之间使用NSUserDefaults进行数据交换仅适用于watchOS 1。

If you are developing for watchOS 2, you should use the WatchConnectivity framework. 如果您正在开发watchOS 2,则应使用WatchConnectivity框架。 See Apple's documentation here . 请参阅此处的 Apple文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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