“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

NSFileManager
/ / write
NSError *err = nil;
NSURL *url = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"Your App Group"];
url = [url URLByAppendingPathComponent: @"Library/Caches/ widget"];
NSString *str = @"hahahaha";
BOOL result = [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&err];
if (!result)
{
    NSLog(@"% @",err);
}
else
{
    NSLog(@"save:%@ success.",str);
}
/ / read
NSError *err = nil;
NSURL *url = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"Your App Group"];
url = [url URLByAppendingPathComponent: @"Library/Caches/ widget"];
NSString *str = [NSString stringWithContentsOfURL:url encoding: NSUTF8StringEncoding error:&err];
Copy the code

This allows you to share data across apps and widgets.

Notify the communication
  • Let’s talk about another interaction. For example, when a button is clicked on a Widget, it changes in real time within the app. This is not the case for data sharing. Here you need to useCFNotificationCenter. NSNotificationCenter, KVO, Delegate don’t try it.
static ViewController *vc = nil;

// Send notifications
- (void)postNotificaiton
{
    CFStringRef keys[1];
    keys[0] = CFSTR("wiwi");
    CFStringRef values[1];
    values[0] = CFSTR("90");
    CFDictionaryRef dic = CFDictionaryCreate(NULL, (void*)keys,(void*)values, 1.&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter(a);CFNotificationCenterPostNotification(notification, CFSTR("widgetNoti"), NULL, dic, YES);
    
}
// Add a listener
- (void)addObserver
{
    // Assign values where appropriate
    vc=self
    CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter(a);CFNotificationCenterAddObserver(notification, (__bridge const void *) (self), observerMethod, CFSTR("widgetNoti"), NULL.CFNotificationSuspensionBehaviorDeliverImmediately);
}

void observerMethod (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    NSDictionary  *nsdic = (__bridge_transfer  NSDictionary*)userInfo;
    // Call the controller method to refresh after getting the passed value
    [vc method:nsdic];
}
// Remove the listener
- (void)removeObserver
{
    CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter(a);CFNotificationCenterRemoveObserver(notification, (__bridge const void *) (self), CFSTR("widgetNoti"), NULL);
}
Copy the code

It looks smooth, but it turns out the value doesn’t transfer at all. It’s embarrassing. If center is a Darwin Notification center, this value is ignored. But this is not a problem, you can receive notification, you can get data through the App Group. You can also do this with a third-party library, MMWormhole, which is also based on CFNotificationCenter.

Code sharing and so on
  • If you want files in your main app to work, the Widget works, too. It’s easy. Just check the Widget project for the target membership of the file.

  • If you want to use pod, add target to your podfile
target 'WidgetDD' do

end
Copy the code

Note your Configurations and Link Binary(though this should normally be set automatically)

  • If you want to make a multilingual Widget, you can also check the multilingual target membership file and share the multilingual configuration file in the main app.

Almost is this write so much, this thing remember to present the function is relatively simple, interactive is not strong things yo, after all, is a small extension program. (DO not know what did not leak, leak later to add, hehe).