美文网首页
如何接收三方APP分享的文件和分享文件到其他APP

如何接收三方APP分享的文件和分享文件到其他APP

作者: 一个默默无闻的程序猿 | 来源:发表于2018-09-18 15:15 被阅读0次

1、接收:在info.plist中保存相关设置

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>OFFICE Document</string>
            <key>LSHandlerRank</key>
            <string>Owner</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>com.microsoft.word.doc</string>  
                <string>com.microsoft.powerpoint.ppt</string>
                <string>com.microsoft.excel.xls</string>
                <string>com.adobe.pdf</string>
            </array>
        </dict>
    </array>

并在AppDelegate中实现方法

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    if ([url.scheme isEqualToString:@"file"]){
        NSString *fileNameStr = [url lastPathComponent];
        NSData *data = [NSData dataWithContentsOfURL:url];
    }
}

简单实现接收文件

2、只要其他APP完成以上info.plist设置,就可以分享文件到其他App
首先在info.plist中添加

<key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
                <string>public.composite-content</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>com.adobe.pdf</string>
            <key>UTTypeDescription</key>
            <string>PDF文档</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.mime-type</key>
                <string>application/pdf</string>
                <key>public.filename-extension</key>
                <array>
                    <string>pdf</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>com.microsoft.word.doc</string>
            <key>UTTypeDescription</key>
            <string>Word文档</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.mime-type</key>
                <string>application/msword</string>
                <key>public.filename-extension</key>
                <array>
                    <string>doc</string>
                    <string>docx</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.data</string>
            </array>
            <key>UTTypeIdentifier</key>
            <string>com.microsoft.excel.xls</string>
            <key>UTTypeDescription</key>
            <string>Excel Document</string>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.mime-type</key>
                <string>application/vnd.ms-excel</string>
                <key>public.filename-extension</key>
                <array>
                    <string>xls</string>
                </array>
            </dict>
        </dict>
    </array>

弹出UIDocumentInteractionController分享

        UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:self.url.path]];
        documentController.delegate = self;
        documentController.UTI = [self getUTI];
        [documentController presentOpenInMenuFromRect:CGRectZero
                                                    inView:self.view
                                                  animated:YES];
- (NSString *)getUTI
{
    NSString *typeStr = [self getFileTypeStr:self.url.path];
    if ([typeStr isEqualToString:@"PDF"]) {
        return @"com.adobe.pdf";
    }
    if ([typeStr isEqualToString:@"Word"]){
        return @"com.microsoft.word.doc";
    }
    if ([typeStr isEqualToString:@"PowerPoint"]){
        return @"com.microsoft.powerpoint.ppt";
    }
    if ([typeStr isEqualToString:@"Excel"]){
        return @"com.microsoft.excel.xls";
    }
    return @"public.data";
}


- (NSString *)getFileTypeStr:(NSString *)pathExtension
{
    if ([pathExtension isEqualToString:@"pdf"] || [pathExtension isEqualToString:@"PDF"]) {
        return @"PDF";
    }
    if ([pathExtension isEqualToString:@"doc"] || [pathExtension isEqualToString:@"docx"] || [pathExtension isEqualToString:@"DOC"] || [pathExtension isEqualToString:@"DOCX"]) {
        return @"Word";
    }
    if ([pathExtension isEqualToString:@"ppt"] || [pathExtension isEqualToString:@"PPT"]) {
        return @"PowerPoint";
    }
    if ([pathExtension isEqualToString:@"xls"] || [pathExtension isEqualToString:@"XLS"]) {
        return @"Excel";
    }
    return @"其它";
}

11537254840_.pic_hd.jpg

相关文章

网友评论

      本文标题:如何接收三方APP分享的文件和分享文件到其他APP

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