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

2022-05-20  本文已影响0人  d4d5907268a9

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

猜你喜欢

热点阅读