分享pdf iOS
点击某个分享按钮,调起系统的 UIActivityViewController,实现如下效果
// // PDFShareViewController.m // QuFangApp // // Created by huangzengsong on 2025/4/17. // #import "PDFShareViewController.h" @interface PDFShareViewController () @property (nonatomic, copy) NSString *webURLString; @property (nonatomic, copy) NSString *webTitleString; @end @implementation PDFShareViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.webURLString = @"https://testapi08.fadada.com/api/downLoadContract.api?contract_id=769679609632403456&v=2.0&msg_digest=MjNCMkE0RDIyRkVCQzk5QjI3M0MzQ0JBRDc5RDNFMzdGRTYwNjUzNQ==&app_id=405812×tamp=20250417105827&"; self.webTitleString = @"盛景花园9-1-203号[4].pdf"; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self sharePdf]; } /// 分享pdf -(void)sharePdf{ /// 先查询是否已经下载如果已经下载 直接分享 如果未下载,现在pdf再分享到直接位置 [self downloadAndShare]; } - (void)downloadAndShare { /// 获取pdf名字 NSString *pdfFileName = self.webTitleString; /// 查询文件是否已经存在 if (![self pdfFileExistsWithName:pdfFileName]) { /// 获取下载链接 NSURL *pdfURL = [NSURL URLWithString:self.webURLString]; NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; __weak typeof(self) weakself =self; NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:pdfURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { if (error) { NSLog(@"PDF文件下载失败: %@", error); } else { NSString *destinationPath = [self pathForPDFFileWithName:pdfFileName]; NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath]; NSError *moveError; [[NSFileManager defaultManager] moveItemAtURL:location toURL:destinationURL error:&moveError]; if (moveError) { NSLog(@"下载PDF完成移动在指定路径失败: %@", moveError); } else { NSLog(@"PDF文件下载成功并保存到指定路径: %@", destinationURL); /// 发起分享 dispatch_async(dispatch_get_main_queue(), ^{ // 这里写需要回到主线程执行的代码(通常是 UI 操作) [weakself startToSharePdf:pdfFileName]; }); } } }]; [downloadTask resume]; }else{ // 发起分享 dispatch_async(dispatch_get_main_queue(), ^{ // 这里写需要回到主线程执行的代码(通常是 UI 操作) [self startToSharePdf:pdfFileName]; }); } } -(void)startToSharePdf:(NSString *)pdfFileName{ NSArray *activityItems = @[[NSURL fileURLWithPath:[self pathForPDFFileWithName:pdfFileName]]]; UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil]; [self presentViewController:activityViewController animated:YES completion:nil]; } - (NSString *)pathForPDFFileWithName:(NSString *)fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths firstObject]; return [documentsDirectory stringByAppendingPathComponent:fileName]; } - (BOOL)pdfFileExistsWithName:(NSString *)fileName { NSString *filePath = [self pathForPDFFileWithName:fileName]; return [[NSFileManager defaultManager] fileExistsAtPath:filePath]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
调整节目样式 可参考
https://blog.csdn.net/liushuo19920327/article/details/124208972
在北京的灯中,有一盏是我家的。这个梦何时可以实现?哪怕微微亮。北京就像魔鬼训练营,有能力的留,没能力的走……