美文网首页
iOS 通过系统自带分享组件,下载并分享网络文件

iOS 通过系统自带分享组件,下载并分享网络文件

作者: d4d5907268a9 | 来源:发表于2022-05-20 18:38 被阅读0次

要把本地文件分享出去,一定是要把本地文件的FileURL分享出去

            NSString *origUrl = url;
            url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            NSLog(@"附件下载地址:%@", url);
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showWithStatus:@"正在加载..."];
            });
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                NSURL *tempurl = [NSURL URLWithString:url];
                NSData *fileData = [NSData dataWithContentsOfURL:tempurl];
                NSString *path = [NSTemporaryDirectory() stringByAppendingFormat:@"%@", origUrl.lastPathComponent];
                NSURL *tmpURL = [NSURL fileURLWithPath:path];
                [fileData writeToURL:tmpURL atomically:YES];
                NSData *tempdata = [NSData dataWithContentsOfURL:tmpURL];
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (tempdata.length > 0) {
                        [SVProgressHUD dismiss];
                        UIActivityViewController *shareVC = [[UIActivityViewController alloc] initWithActivityItems:@[tmpURL] applicationActivities:nil];
                        [self presentViewController:shareVC animated:YES completion:nil];
                    } else {
                        [SVProgressHUD showErrorWithStatus:@"文件下载失败"];
                    }
                });
            });

相关文章

网友评论

      本文标题:iOS 通过系统自带分享组件,下载并分享网络文件

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