iOS realizes file sharing between two apps (copying files from one app to another)

Direct solution:

Add the fields as shown in the figure to the project Info.plist file (the red box indicates that all file types can be copied)

Specific document type reference:

Apple documentation

It's not finished yet:

The file has been copied to our App. We need to deal with it (save locally or preview directly). My process is save locally

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths lastObject];
    if (url != nil) {
        NSString *path = [url absoluteString];
        path = [path stringByRemovingPercentEncoding];
        NSMutableString *string = [[NSMutableString alloc] initWithString:path];
        if ([path hasPrefix:@"file:///private"]) {
            [string replaceOccurrencesOfString:@"file:///private" withString:@"" options:NSCaseInsensitiveSearch  range:NSMakeRange(0, path.length)];
        NSArray *tempArray = [string componentsSeparatedByString:@"/"];
        NSString *fileName = tempArray.lastObject;
        NSString *sourceName = options[@"UIApplicationOpenURLOptionsSourceApplicationKey"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",sourceName,fileName]];
        if ([fileManager fileExistsAtPath:filePath]) {
            NSLog(@"file already exist");
            [SVProgressHUD showErrorWithStatus:@"file already exist"];
            return YES;
        [MRTools creatFilePathInManager:sourceName];
        BOOL isSuccess = [fileManager copyItemAtPath:string toPath:filePath error:nil];
        if (isSuccess == YES) {
            NSLog(@"Success copy");
            [SVProgressHUD showSuccessWithStatus:@"File copied successfully"];
        } else {
            NSLog(@"Copy failure");
            [SVProgressHUD showErrorWithStatus:@"File copy failed"];
    return  YES;

At this end, you can refer to the following

Posted on Fri, 10 Jan 2020 10:34:48 -0500 by omanush