iOS中打开的文件如何用其他应用打开选择自己的app

效果如图:
iOS中打开的文件如何用其他应用打开选择自己的app_第1张图片
用其他应用打开
iOS中打开的文件如何用其他应用打开选择自己的app_第2张图片
选择某一应用
1、设置 Info.plist
CFBundleDocumentTypes

    
        CFBundleTypeIconFiles
        
            96.png
            [email protected]
        
        CFBundleTypeName
        com.myapp.common-data
        LSItemContentTypes
        
            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
            public.text
            public.data
        
    

2、设置 AppDelegate
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (self.window) {
        if (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编码问题

            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:@"FileNotification" object:nil userInfo:dict];

                return YES;
            }
        }
    }
    return YES;
}

// 当文件名为中文时,解决url编码问题
- (NSString *)URLDecodedString:(NSString *)str {
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)str, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    DLog(@"decodedString = %@",decodedString);
    return decodedString;
}
3、接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileNotification:) name:@"FileNotification" object:nil];

- (void)fileNotification:(NSNotification *)notifcation {
    NSDictionary *info = notifcation.userInfo;
    // fileName是文件名称、filePath是文件存储在本地的路径
    // jfkdfj123a.pdf
    NSString *fileName = [info objectForKey:@"fileName"];
    // /private/var/mobile/Containers/Data/Application/83643509-E90E-40A6-92EA-47A44B40CBBF/Documents/Inbox/jfkdfj123a.pdf
    NSString *filePath = [info objectForKey:@"filePath"];

    NSLog(@"fileName=%@---filePath=%@", fileName, filePath);

你可能感兴趣的:(iOS中打开的文件如何用其他应用打开选择自己的app)