iOS学习笔记iOS 开发每天分享优质文章ios实用开发技巧

iOS外界传入文件到App及预览

2019-11-07  本文已影响0人  BWLi420

本文以 Word、Excel、PPT、PDF 为例,其他格式类似处理

1. 在 info.plist 中添加以下设置,表示可以接收的文件类型

image.png
<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>org.openxmlformats.wordprocessingml.document</string>
                <string>com.microsoft.excel.xls</string>
                <string>org.openxmlformats.spreadsheetml.sheet</string>
                <string>com.microsoft.powerpoint.ppt</string>
                <string>org.openxmlformats.presentationml.presentation</string>
                <string>com.adobe.pdf</string>
            </array>
        </dict>
    </array>
    <key>UISupportsDocumentBrowser</key>
    <true/>

2. 在 AppDelegate 中实现 application:openURL:options: 方法,用以接收外界文件

#pragma mark - 外界文件导入
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    
    if (url == nil) {
        return NO;
    }
    NSLog(@"文件地址 - %@", url);
    //具体存储或其他处理在这里进行
}

3. 文档预览

3.1 UIDocumentInteractionController

//创建
UIDocumentInteractionController *documentVc = [UIDocumentInteractionController interactionControllerWithURL:url];
//设置代理 UIDocumentInteractionControllerDelegate
documentVc.delegate = self;
//显示
[documentVc presentPreviewAnimated:YES];
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}

//不实现此代理,默认系统样式,带分享功能,实现时隐藏系统分享
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.view;
}

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return self.view.bounds;
}

3.2 QLPreviewController

  1. 正常样式,显示系统分享按钮,继承于 QLPreviewController

    @interface PreviewViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate>
    
    /// 预览文档地址
    @property (nonatomic, copy) NSString *filePath;
    @end
    
    @implementation PreviewViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.delegate = self;
        self.dataSource = self;
    }
    
    - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
        return 1;
    }
    
    - (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
        
        return [NSURL fileURLWithPath:self.filePath];
    }
    
    //不打开文档内的链接
    - (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item {
        return NO;
    }
    
    @end
    
  2. 隐藏样式,隐藏系统分享按钮

    QLPreviewController *previewVC = [[QLPreviewController alloc] init];
    previewVC.dataSource = self;
    previewVC.delegate = self;
    
    // 将QLPreviewControler  添加到控制器上,隐藏系统分享按钮
    [self addChildViewController:previewVC];
    [previewVC didMoveToParentViewController:self];
    previewVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:previewVC.view];
    
    //其他设置代理等操作与上面一致
    
上一篇 下一篇

猜你喜欢

热点阅读