Xcode Swift appleWatch read application UserDefaults?
Like Shunzhe mentioned, you can no longer share UserDefaults
between iOS and WatchOS.
You can use the sendMessage:replyHandler:errorHandler
method of WatchConnectivity
to wake up the iPhone, run some code, and send something back to the watch.
More info here.
UserDefaults between iOS and WatchOS not working
Communication between watchKit and app using userDefaults has been removed.
It can be done using watchConnectivity framework
WatchKit SDK not retrieving data from NSUserDefaults
+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.
Are there still any ways to use App Groups in watch?
You can't use App Groups to share data between phone and watch since WatchOS3.
You can use updateapplicationContext to guarantee the data will be passed to Apple Watch.
Related Topics
How to Make a Swiftui Gesture That Keeps Running Code While the View Is Pressed
Swift:Non-Nil Optional Value Raising a Nil Exception
Uiimageview Aspect Fit and Center
Making an Array of Integers in iOS
How to Use Autocorrection and Shortcut List in iOS8 Custom Keyboard
Uisearchdisplaycontroller's Searchresultstableview's Contentsize Is Incorrect. Bug in iOS 7
Xcode Info.Plist Build Variable ${Product_Name:Rfc1034Identifier} Seems Completely Undocumented
Mixing Static and Dynamic Sections in a Grouped Table View
Could Not Instantiate Class Named Ibnslayoutconstraint
How to Delete Object from Core Data in Swift 3
Missing Compliance Status in Testflight
Change the Font of a Uibarbuttonitem
Extracting H264 from Cmblockbuffer
Detecting Active Avaudiosessions on iOS Device
Profile Doesn't Match the Entitlements File's Value for the Application-Identifier Entitlement