[英]How to remove data after share between extension and host app in iOS
I have an iOS app, need to handle user share data from third app.我有一个 iOS 应用程序,需要处理来自第三个应用程序的用户共享数据。 To handle this, I cache the data by UserDefaults in extension module:
为了处理这个问题,我在扩展模块中通过 UserDefaults 缓存数据:
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"];
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.