优快云 的强制 VIP 越来越恶心了,强制文章开起 VIP,本来写文章就是分享技术,后续不再在此平台更新了
APP之间共享数据的方式约以下几种:
URL Scheme
跳转App Groups
共享一块存储空间KeyChain共享数据(keychain access group)
账号密码共用,不会随App删除UIDocumentInteractionController
文档分享,例如文件在QQ中打开的功能粘贴板UIPasteboard
复制粘贴
App Groups 用于两个app之间共享文件,开扩了一块共同的存储区域
!
此外扩展( Extension )也需要使用App Groups 的相关知识
此方法只能使用于同一个开发者账号,如果不同开发者账号请考虑剪切板 UIPasteboard
若需要存储共有的账号密码,请使用KeyChain
创建AppGroups
登陆开发者账号
https://developer.apple.com/account/
选择Certificates,IDS & Profiles
选择Identifiers
,添加一个
选择App Groups
然后填写描述和group标识
然后 continue
每个app id
需要打开App Groups
功能,并配置好
项目中配置
打开你的xcode项目工程,先选中根目录,在target中选中你的工程–>Capabilites
代码中使用
NSUserDefaults
//初始化一个供App Groups使用的NSUserDefaults对象
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.appGroupName"];
//写入数据
[userDefaults setValue:@"value" forKey:@"key"];
//读取数据
NSLog(@"%@", [userDefaults valueForKey:@"key"]);
NSFileManager
//获取分组的共享目录
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.company.appGroupName"];
NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"demo.txt"];
//写入文件
[@"abc" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
//读取文件
NSString *str = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str = %@", str);
利用NSFileManager
我们可以存取各种文件,数据库文件(.db),json文件
,framework
等等;我们可以复制各种文件到APPGroup
的存取区域,在另一个app
中拿出来
static NSString * const group = @"group.company.appGroupName";
- (BOOL)saveDataFromPath:(NSString *)originPath toFile:(NSString *)filename;
{
self.url = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:group];
NSURL *fileURL = [self.url URLByAppendingPathComponent:filename];
return [[NSFileManager defaultManager] copyItemAtPath:originPath toPath:[fileURL path] error