美文网首页数据共享shmily-iOS/Mac
IOS 两个或多个App文件数据共享方法App Groups

IOS 两个或多个App文件数据共享方法App Groups

作者: 小江9527 | 来源:发表于2018-05-03 12:01 被阅读90次

数据共享有很多种,大部分好心人都已经写了。这里我就借用一下链接。
iOS应用程序间共享数据

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获取这个目录,再读取共享的文件,然后该干哈就干哈。
最后上代码下载地址
代码下载地址

相关文章

  • IOS 两个或多个App文件数据共享方法App Groups

    数据共享有很多种,大部分好心人都已经写了。这里我就借用一下链接。iOS应用程序间共享数据 AppA写入端代码,很简...

  • App Groups简单使用

    iOS8 之后添加了App Groups,应用可以通过App Groups共享数据,原文如下: AppGroup ...

  • iOS【NotificationContent-App Grou

    App Groups可以使两个不同的APP进行数据共享同一个开发者账号下,多个APP共享数据,如登录A APP情况...

  • App Group Share

    App Group主要用在两个方面:1.多个App共享数据文件;2.App和其Extension共享数据 因为每个...

  • iOS widget设置 App Groups 数据共享debu

    最近在写iOS的 widget, 其中用到和主APP数据共享, 需要设置App groups细节不讲, 直说问题:...

  • 进程间通信-App Groups

    最简单的进程间的数据共享App Groups此事例对应屏幕共享数据传输、使用时请酌情处理 App Groups创建...

  • Widget 开通 App Groups 服务步骤

    Widget开通App Groups服务步骤App Groups的共享仅限于同一个开发账号下的 app 使用,因为...

  • App Groups 文件路径问题

    将数据库文件存在了 App Groups 中,用于共享,但做数据库大小计算时,发现获取的路径是错误的file://...

  • iOS App Groups

    iOS学习笔记-APP之间数据共享空间_APPGroup iOS应用程序间共享数据

  • 2021-03-04

    iOS App间文件共享 App间文件共享这里面包含两种需求: 1、注册App为可共享应用程序,接收应用程序发送方...

网友评论

    本文标题:IOS 两个或多个App文件数据共享方法App Groups

    本文链接:https://www.haomeiwen.com/subject/cbekrftx.html