NSUserDefaults NSUserDefaults is a quick way to share information. It's good for storing small pieces of data that are quickly accessed and calculated, such as usernames and profile information. If you want to use UserDefaults, use it for static data so that users don't have to worry about changing values. You need to set up the App Group to allow devices to share data through a shared container, make sure both the watch extension and the ios target are set up this way. Basically, you create a unified App Group identifier for both devices. If you need to delete it, you can do it in a similar way. You can use defaults by the App Group name you created earlier, which basically sets an object for a specific key value. On iPhone, the user enters text, saves it, and the text is saved to the UserDefaults shared by the app. On Watch, you can get the defaults from the AppGroup and then update the watch display.
NSFileCoordinator For larger data, NSFileCoordinator is one way to manage files in a shared space between your app and the watch extension. It works well for limited lists of content, and also works well for image files. The following example is a simple to-do list app that adds tasks on the phone and then transfers them to the WatchKit extension and displays them on the watch. Your view controller needs to conform to the NSFilePresenter protocol, and other than implementing two required methods, the rest is not critical. The FilePresenter protocol has an item URL, which is where you fill in your AppGroup identifier. With the URL, you create a file in the corresponding directory. You can also control multi-threaded access through operation queues if necessary. In addition, the presentedItemDidChange delegate method notifies you in FilePresenter if an object has changed, allowing you to update app data without the user having to manually refresh. However, there is still a bug with NSFileCoordinator and NSFilePresenter that makes it inconvenient to use in extensions. For details, please refer to Natasha's website. Use FileCoordinator to write a file in the to-do array, and you can read and write files to pack and unpack the data of the items into the to-do array. Then you can calculate and generate a table based on the item data in the file. It should be noted that if you design a delete function, and both the watch extension and the iPhone app can modify the file, you will encounter thread synchronization problems.
Frameworks “If the code appears more than once, it probably belongs in a framework.” -WWDC 2014, Building Modern Frameworks Frameworks are great for business logic, CoreData, and reusable UI components. As mentioned in WWDC, you can put repeated code into frameworks. In the FileCoordinator example, our code for getting and reading and writing files appears twice, so we can extract them into a framework. Creating a framework is simple: create a new target, select Cocoa Touch framework, and name it. It will be automatically linked in your iOS app, so don't forget to link it in your WatchKit extension as well. The key point, especially for the Swift language, is that you should think of the framework as an API. It needs to be declared public because this is the interface that the iOS app and the watchkit extension share. So if you are creating an object class, make sure to add the public keyword as well. This way, you can access any public content in the phone and watch apps when you import the framework.
Keychain Sharing Keychain sharing is for data with higher security requirements. When the security provided by UserDefaults is not sufficient, you can use keychain sharing to ensure information security and cross-extension sharing capabilities. A big problem with WatchKit right now is that there is no authentication mechanism. Apple provides a sample KeychainIteamWrapper, but the API is too old and does not support ARC. I recommend using this version https://gist.github.com/dhoerl/1170641, which is based on ARC and has a clear interface. The question is how to initialize KeychainItemWrapper with access group. Similar to the concept of AppGroup, there is a shared space between devices. You need keychain to access user data in both iOS and WatchKit extension. Through the key-value storage system, you set the username and password and create the same type of keychain item with the same identifier. This example only shows the working process of WatchKit extension displaying data when the user fills in the username and password.
|
<<: How to use TypeScript to improve JavaScript programming
>>: What you need to know about adaptation - MobileWeb adaptation summary
Due to the city lockdown in Wuhan before the Spri...
[51CTO.com original article] The launch of the fi...
There is basically a consensus now: C-end product...
Today, I suddenly felt like talking to you about ...
Many people are asking how to add an “official” l...
Why run YouTube TrueView ads? YouTube TrueView di...
Solution: Let me share with you 10 methods to dis...
You have just taken over the marketing work of an...
Author: Fluent The highest temperature this week ...
A new market crisis is gradually approaching the ...
Google is shutting down its mobile shopping apps ...
The situation of overseas shopping products has b...
1. Background Main business: Business service ind...
Written by | Fanpu At 11:30 a.m. local time on Oc...