I have an iOS app, need to handle user share data from third app. To handle this, I cache the data by UserDefaults in extension module:
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.app.share"];
// write data
[userDefaults setValue:[((NSURL*) item) absoluteString] forKey:@"shareData"];
//open app
[self openApp];
After that, app is opened and then can read and handle the data:
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.app.share"];
//read data
NSString *data = [userDefaults valueForKey:@"shareData"];
NSLog(@"%@", data);
Till now, everything is ok. App host can get the share data from extension correctly.
However, when I need to remove the data after handling:
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.app.share"];
//read data
NSString *data = [userDefaults valueForKey:@"shareData"];
NSLog(@"%@", data);
...use the data and then remove it
[userDefaults removeObjectForKey: @"shareData"]
Then strange thing happens as following steps:
That is to say, the data is missing during app launching. What's the reason then?
Find a way. Add a timer or delay, when time is out, remove the key in extension module. It works.
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.app.share"];
//write data
[userDefaults setObject:[((NSURL*) item) absoluteString] forKey:@"shareData"];
[self openApp];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[userDefaults removeObjectForKey:@"shareData"];
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.