數(shù)據(jù)共享有很多種,大部分好心人都已經(jīng)寫了。這里我就借用一下鏈接。
iOS應(yīng)用程序間共享數(shù)據(jù)
AppA寫入端代碼,很簡單的,如下:
#import "ViewController.h"
#define APP_GROUP_ID @"group.com.dacheng.AppGroupFileShare"
#define APP_FOLDER_NAME @"DachengShareFile"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSString *storagePath;
@property (nonatomic, strong) NSArray *fileNamesArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"主App共享File";
[self writeFirstFileToShare];
[self loadData];
}
- (void)writeFirstFileToShare
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL isOpened = [[defaults objectForKey:@"isOpened"] boolValue];
if(!isOpened){
[defaults setObject:@(YES) forKey:@"isOpened"];
NSString *file1Cont = @"Hello every one. I'm M0nk1y. My site: http://mkapple.cn";
NSString *file2Cont = @"new File2:";
[file1Cont writeToFile:[self.storagePath stringByAppendingPathComponent:@"File1.text"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
[file2Cont writeToFile:[self.storagePath stringByAppendingPathComponent:@"File2.text"] atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *file3Path = [[NSBundle mainBundle] pathForResource:@"battery" ofType:@"sqlite"];
NSString *file3ToPath = [self.storagePath stringByAppendingPathComponent:@"battery.sqlite"];
NSError *error;
BOOL success = [[NSFileManager defaultManager] copyItemAtPath:file3Path toPath:file3ToPath error:&error];
NSLog(@"success:%d==error:%@", success, error);
}
}
- (void)loadData
{
// self.fileNamesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.storagePath error:nil];
// for (NSString *fileName in self.fileNamesArray) {
// NSString *path = [self.storagePath stringByAppendingPathComponent:fileName];
// BOOL success = [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
// NSLog(@"success:%d", success);
// }
// NSLog(@"self.fileNamesArray:%@", self.fileNamesArray);
self.fileNamesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.storagePath error:nil];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.tableView reloadData];
NSLog(@"self.fileNamesArray:%@", self.fileNamesArray);
}
#pragma mark - 獲取共享容器文件夾路徑
- (NSString *)storagePath
{
if (_storagePath) {
return _storagePath;
}
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_ID];
NSString *groupPath = [groupURL path];
self.storagePath = [groupPath stringByAppendingPathComponent:APP_FOLDER_NAME];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:_storagePath]) {
[fileManager createDirectoryAtPath:_storagePath withIntermediateDirectories:NO attributes:nil error:nil];
}
return _storagePath;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.fileNamesArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.fileNamesArray[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *filePath = [self.storagePath stringByAppendingPathComponent:self.fileNamesArray[indexPath.row]];
NSLog(@"filePath:%@", filePath);
if (indexPath.row < 2) {
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"content:%@", fileContent);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
AppB端讀取代碼,更簡單,和上面代碼基本一樣。
#import "ViewController.h"
#define APP_GROUP_ID @"group.com.dacheng.AppGroupFileShare"
#define APP_FOLDER_NAME @"DachengShareFile"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSString *storagePath;
@property (nonatomic, strong) NSArray *fileNamesArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"App從共享目錄讀取文件";
[self loadData];
}
- (void)loadData
{
self.fileNamesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.storagePath error:nil];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
[self.tableView reloadData];
NSLog(@"self.fileNamesArray:%@", self.fileNamesArray);
}
#pragma mark - 獲取共享容器文件夾路徑
- (NSString *)storagePath
{
if (_storagePath) {
return _storagePath;
}
NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:APP_GROUP_ID];
NSString *groupPath = [groupURL path];
self.storagePath = [groupPath stringByAppendingPathComponent:APP_FOLDER_NAME];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:_storagePath]) {
[fileManager createDirectoryAtPath:_storagePath withIntermediateDirectories:NO attributes:nil error:nil];
}
return _storagePath;
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.fileNamesArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.textLabel.text = self.fileNamesArray[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *filePath = [self.storagePath stringByAppendingPathComponent:self.fileNamesArray[indexPath.row]];
NSLog(@"filePath:%@", filePath);
if (indexPath.row < 2) {
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"content:%@", fileContent);
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
解釋一下邏輯
通過打印共享文件路徑可以得到如下:
/private/var/mobile/Containers/Shared/AppGroup/F4578610-A8D5-4221-A37C-6F12CFEE8AD4/DachengShareFile/File2.text
路徑很明顯,Shared共享目錄AppGroup單獨開辟一個地址
邏輯就是:
AppA把文件通過AppGroupID存放到這個目錄
AppB再通過AppGroupID獲取這個目錄,再讀取共享的文件,然后該干哈就干哈。
最后上代碼下載地址
代碼下載地址