ios 跳转第三方App文档预览

今天有个需求,从App上下载附件可预览,产品要求简单实现就是跳转第三方App预览即可

具体实现代码

- (IBAction)thirdPartAppAction:(UIButton *)sender {
    [self thirdAppPreview];
}

- (void)thirdAppPreview
{
    NSString *docPath = @"";
    // 测试用网络下载文件部分忽略
    // 数据库路径-沙盒路径
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
    NSString *fileName = [doc stringByAppendingPathComponent:@"test.xlsx"];
    
    // 复制本地数据到沙盒中
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:fileName]) {
        // 获得数据库文件在工程中的路径——源路径。
        docPath = [[NSBundle mainBundle] pathForResource:@"test"ofType:@"xlsx"];
        
        NSError *error ;
        
        if ([fileManager copyItemAtPath:docPath toPath:fileName error:&error]) {
            NSLog(@"数据库移动成功");
        } else {
            NSLog(@"数据库移动失败");
        }
    }
    
    //获取路径文件url
    NSURL*fileUrl = [NSURL fileURLWithPath:fileName];
    
    // 初始化UIActivityViewController
    NSArray *activityItems = @[fileUrl];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil];
    //不出现在活动项目
    activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:YES completion:nil];
    // 分享之后的回调
    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            NSLog(@"completed");
            //分享 成功
        } else  {
            NSLog(@"cancled");
            //分享 取消
        }
    };
}

demo 地址 https://github.com/destinyzhao/DocumentPreview

1530758958085.jpg

你可能感兴趣的:(ios 跳转第三方App文档预览)