要把本地文件分享出去,一定是要把本地文件的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:@"文件下载失败"];
}
});
});
网友评论