iOS-实现两个APP之间共享文件(从一个APP拷贝文件至另一个APP)

在项目Info.plist文件中添加如图所示字段(红色框框里是表示所有文件类型都可拷贝)


截屏2022-09-08 15.43.38.png

具体文件类型参考:

Apple documentation

到这一步还没完成:

文件拷贝到咱们App了,咱们需要处理一下(要么存本地,要么直接预览)我的处理是存本地.上代码:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)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(@"文件已存在");
            [SVProgressHUD showErrorWithStatus:@"文件已存在"];
            return YES;
        }
        [MRTools creatFilePathInManager:sourceName];
        BOOL isSuccess = [fileManager copyItemAtPath:string toPath:filePath error:nil];
        if (isSuccess == YES) {
            NSLog(@"拷贝成功");
            [SVProgressHUD showSuccessWithStatus:@"文件拷贝成功"];
        } else {
            NSLog(@"拷贝失败");
            [SVProgressHUD showErrorWithStatus:@"文件拷贝失败"];
        }
    }
    NSLog(@"application:openURL:options:");
    return  YES;
}

    CFBundleDocumentTypes
    
        
            CFBundleTypeName
            com.myapp.common-data
            LSHandlerRank
            Default
            LSItemContentTypes
            
                public.item
            
        
    
CFBundleDocumentTypes
    
        
            CFBundleTypeName
            com.myapp.common-data
            LSHandlerRank
            Default
            LSItemContentTypes
            
                com.microsoft.excel.xlsx
                com.microsoft.excel.xls
                com.microsoft.word.doc
                com.microsoft.word.docx
                com.adobe.pdf
                com.adobe.ppt
                com.adobe.pptx
                public.zip
                public.rar
                public.csv
                public.pps
                public.ppsx
                public.txt
                public.md
                public.markdown
                public.7z
                public.epub
                public.mobi
                public.audio
                public.movie
                public.image
                public.mp3
            
        
    
UTExportedTypeDeclarations
    
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.xlsx
            UTTypeTagSpecification
            
                public.filename-extension
                xlsx
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.jpeg
            UTTypeTagSpecification
            
                public.filename-extension
                jpeg
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.doc
            UTTypeTagSpecification
            
                public.filename-extension
                doc
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.zip
            UTTypeTagSpecification
            
                public.filename-extension
                zip
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.rar
            UTTypeTagSpecification
            
                public.filename-extension
                rar
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.docx
            UTTypeTagSpecification
            
                public.filename-extension
                docx
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.xls
            UTTypeTagSpecification
            
                public.filename-extension
                xls
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.ppt
            UTTypeTagSpecification
            
                public.filename-extension
                ppt
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.pptx
            UTTypeTagSpecification
            
                public.filename-extension
                pptx
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.csv
            UTTypeTagSpecification
            
                public.filename-extension
                csv
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.pps
            UTTypeTagSpecification
            
                public.filename-extension
                pps
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.ppsx
            UTTypeTagSpecification
            
                public.filename-extension
                ppsx
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.txt
            UTTypeTagSpecification
            
                public.filename-extension
                txt
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.md
            UTTypeTagSpecification
            
                public.filename-extension
                md
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.markdown
            UTTypeTagSpecification
            
                public.filename-extension
                markdown
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.7z
            UTTypeTagSpecification
            
                public.filename-extension
                7z
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.epub
            UTTypeTagSpecification
            
                public.filename-extension
                epub
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.mobi
            UTTypeTagSpecification
            
                public.filename-extension
                mobi
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.mp3
            UTTypeTagSpecification
            
                public.filename-extension
                mp3
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.m4a
            UTTypeTagSpecification
            
                public.filename-extension
                m4a
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.wav
            UTTypeTagSpecification
            
                public.filename-extension
                wav
            
        
        
            UTTypeConformsTo
            
                Owner
            
            UTTypeIdentifier
            saru2020.pdf
            UTTypeTagSpecification
            
                public.filename-extension
                pdf
            
        
    

参考:https://www.freesion.com/article/7099107074/

你可能感兴趣的:(iOS-实现两个APP之间共享文件(从一个APP拷贝文件至另一个APP))