iOS14 Widget development stepping pit (3) data communication and user configuration

iOS14 Widget development stepping pit (3) data transfer and user configuration

foreword

This article is mainly the knowledge and experience I learned when writing the project. There may be mistakes. It is only for reference of learning ideas. Welcome to discuss and learn together.

data transfer method

The communication between the main program and the Extension belongs to the communication between the Targets , so there are still two types of local communication and server communication. Here I mainly record the two types of local data communication. Both NSUserDefault and NSFileManager have to depend on the AppGroup settings, because the data of the file needs to be stored in an area that both Targets can access. The difference between the two is as follows.

NSUserDefault

The essence of NSUserDefault is a plist file, which is suitable for storing some lightweight user configuration information.
Through the initWithSuiteName method, you can create a plist that members in the AppGroup can access. Its advantages are: thread safety, convenient reading and writing, and easy management.

// OC写入
NSUserDefault *ud = [[NSUserDefaults alloc] initWithSuiteName:@"这里是AppGroup的字符串"];
[ud setObject:@"需要保存的数据" forKey:@"数据的Key"];
[ud synchronize];

// Swift写入
let ud = UserDefaults(suiteName: "这里是AppGroup的字符串")!;
ud.setValue(@"需要保存的数据", forKey: @"数据的Key")
ud.synchronize()

// Swift读取
let ud = UserDefaults(suiteName: "这里是AppGroup的字符串")!
let obj = ud.value(forKey: "数据的Key")
if obj == nil {
    
    
	// 
} else {
    
    
	//
} 

NSFileManager

The principle is that the main program saves the required data in a file that can be accessed by both Targets , and the Widget reads the data in this file according to the set method and displays it when needed. The advantage of NSFileManager over NSUserDefault is that it can save a large amount of data.

// 设置文件的统一资源定位符
NSURL *fileURL = [[NSFileManager defaultManager]containerURLForSecurityApplicationGroupIdentifier:@"这里是AppGroup的字符串"];
fileURL = [fileURL URLByAppendingPathComponent:@"你的文件名"];
// 使用NSFileManager进行文件保存
xxxx

database

From the above two storage methods, we understand that we only need to store the file in the public area of ​​the AppGroup , and other Targets in the Group can access it, then we can put the database in this directory, as long as the database is guaranteedthread safety, the correct data can be obtained. The specific choice of database depends on personal preferences and project requirements. I use Realm to store data in the main program, and read data in Widget according to the data id .

user configuration

The user configuration interface of WidgetKit is configured using the intentdefinition file of SiriKit , and there is no need to design the interface display separately. Data selection can also be provided using an IntentExtension . Users only need to long press the widget and selectedit widgetoption to enter the editing interface. Operations such as location selection, data control, text input, options, switches, etc. can be performed. insert image description here
For specific configuration methods and settings, please refer to the following articles, so I won’t go into details here. Here I will focus on writing about the problems I encountered while writing this part.
[iOS14]WidgetKit development practice 3-widget user configuration .
[iOS14] imitation Netease cloud desktop widget (3) .
iOS14 WidgetKit small test - user configuration and intent .
Add configuration and intelligence to your widgets

problems encountered

1. In the Deployment Info of IntentExtension , you must check whether the iOS version is the version you need. I added it to iOS14.2 by default , but my project and mobile phone are iOS 14 , which leads to the problem that the data cannot be read .

2. IntentExtension must be in the same AppGroup as WidgetExtension and the main program , otherwise the data cannot be read.

3. IntentExtension sometimes cannot find the corresponding Intent , because Xcode needs to generate the corresponding code file after the Intent is created, and sometimes Xcode does not load it in time, you can try it after restarting Xcode . Or the Target MemberShip of the intentdefinition file does not check IntentExtension . When operating between multiple Targets , you must pay attention to the Target MemberShip of the file . It may also be that the Dynamic Options option is not set for this parameter, just check it.

references

[iOS14] WidgetKit development practice 3-widget user configuration .
iOS14 WidgetKit small test - user configuration and intent .
[iOS14] imitation Netease cloud desktop widget (3) .
The use of Swift-Realm database detailed
iOS widget Widget stepping pit
iOS Widget from 0 to 1 development
Add configuration and intelligence to your widgets

Guess you like

Origin blog.csdn.net/qq_38718912/article/details/109092438