WatchKit SDK not retrieving data from NSUserDefaults

佐手、 提交于 2019-11-27 08:15:57
Dave DeLong

+standardUserDefaults returns an NSUserDefaults object that only saves information for the current process.

If you want to create an NSUserDefaults object that shares data between your iOS app and one of its extensions, then you'll need to set up an app group container and use that as the basis for sharing information.

From the linked documentation:

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. For example, a Share extension might update the user’s most recently used sharing account, using code like this:

// Create and share access to an NSUserDefaults object.
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.example.domain.MyShareExtension"];

// Use the shared user defaults object to update the user's account.
[mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];

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

dredful

IMPORTANT

With watch OS2 you can no longer use shared group containers. You need to use this stack overflow answer.

"For OS2 - you will need to use the WatchConnectivity frameworks and implement the WCSessionDelegate."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!