iOS 第三方app的文件用自己的app打开

比如微信有个文件,想要分享到自己的APP,如下图

image.png

步骤:

  • plist文件新增key: CFBundleDocumentTypes
image.png
CFBundleDocumentTypes
    
        
            LSItemContentTypes
            
                public.text
                public.data
                com.microsoft.powerpoint.ppt
                public.item
                com.microsoft.word.doc
                com.adobe.pdf
                com.microsoft.excel.xls
                public.image
                public.content
                public.composite-content
                public.archive
                public.audio
                public.movie
            
        
    
  • app delegate代理方法中

如果有SceneDelegate,在SceneDelegate处理

- (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts {
    //

如果没有SceneDelegate,在AppDelegate处理

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    //file:///private/var/mobile/Containers/Data/Application/00FF7BC3-24B3-4DCB-AF4F-587B619DE720/Documents/Inbox/newrecord-4.plist
    NSLog(@"url:%@", url);
}

这里贴下用AppDelegate处理的方式

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    //file:///private/var/mobile/Containers/Data/Application/00FF7BC3-24B3-4DCB-AF4F-587B619DE720/Documents/Inbox/newrecord-4.plist
    NSLog(@"url:%@", url);
    
    NSString *fileName = url.lastPathComponent; // 从路径中获得完整的文件名(带后缀)
    
    // path 类似这种格式:file:///private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
    NSString *path = url.absoluteString; // 完整的url字符串
    path = [self URLDecodedString:path]; // 解决url编码问题
    
    
    
    //复制文件
    NSString *docFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:fileName];
    NSLog(@"docFilePath:%@", docFilePath);
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:docFilePath]) {
        NSLog(@"文件存在,先移除");
        [fm removeItemAtPath:docFilePath error:NULL];
    }
    
    // 去除前缀 file:///private
    NSString *tmpPath = [path stringByReplacingOccurrencesOfString:@"file:///private" withString:@""];
    [fm copyItemAtPath:tmpPath toPath:docFilePath error:NULL];
    
    
    NSMutableString *string = [[NSMutableString alloc] initWithString:path];
    
    if ([path hasPrefix:@"file://"]) { // 通过前缀来判断是文件
        // 去除前缀:/private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
        [string replaceOccurrencesOfString:@"file://" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, path.length)];
        
        // 此时获取到文件存储在本地的路径,就可以在自己需要使用的页面使用了
        NSDictionary *dict = @{@"fileName":fileName,
                               @"filePath":string};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"" object:nil userInfo:dict];
        
        return YES;
    }
    return YES;
}

// 当文件名为中文时,解决url编码问题
- (NSString *)URLDecodedString:(NSString *)str {
    return [str stringByRemovingPercentEncoding];
    
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));

    return decodedString;
}

你可能感兴趣的:(iOS 第三方app的文件用自己的app打开)