Search code examples
iosswiftapple-watch

What is causing this to happen? NSUserDefaults couldn't share the data between the IOS and Watch in a watchkit Project


I could get the data from the IOS,but watch. If I save data by watchkit extension, IOS couldn't get the data.So it's so strange. And I have added the Group and create the profile. following is my Snippet:

class InterfaceController: WKInterfaceController {

@IBOutlet var outputLabel: WKInterfaceLabel!
override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    // Configure interface objects here.
}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}


// load the data from the IOS
@IBAction func loadData() {
    let userDefaults = UserDefaults.standard
    userDefaults.synchronize()
    let outputData = userDefaults.string(forKey: "share")
    self.outputLabel.setText(outputData)
}
}

class ViewController: UIViewController {

@IBOutlet weak var inputData: UITextField!
@IBOutlet weak var inputLabel: UILabel!
@IBOutlet weak var outputLabel: UILabel!

// input string into the fieldText and save them in Group by NSUserDefaults
@IBAction func saveData(_ sender: AnyObject) {
    let inputData1 = self.inputData.text
    self.inputLabel.text = inputData1
    let userDefaults = UserDefaults.standard
    userDefaults.setValue(inputData1, forKey: "share")

    userDefaults.synchronize()
    self.outputLabel.text = userDefaults.string(forKey: "share")

}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Solution

  • Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers.

    To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions. Next, register the app group in the portal and specify the app group to use in the containing app.

    After you enable app groups, an app extension and its containing app can both use the NSUserDefaults API to share access to user preferences. To enable this sharing, use the initWithSuiteName: method to instantiate a new NSUserDefaults object, passing in the identifier of the shared group

    Objective C

    // Create and share access to an NSUserDefaults object
    
    NSUserDefaults * sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"com.example.domain.MyShareExtension"];
    
    // Use the shared user defaults object to update the user's account
    
    [sharedDefaults setObject:@"Your Value" forKey:@"Your Key"];
    

    swift:

    func saveUserDefaults() {
    
    let sharedDefaults = NSUserDefaults(suiteName: "com.example.domain.MyShareExtension")
        sharedDefaults?.setObject("Your Value", forKey: "Your Key")
        sharedDefaults?.synchronize()
    
    }