美文网首页
iOS开发实现调用原生分享面板分享pdf等文件

iOS开发实现调用原生分享面板分享pdf等文件

作者: 芷依儿 | 来源:发表于2022-08-12 15:22 被阅读0次

    需求:银行项目要实现调起iPhone原生面板实现转账流水等PDF文件的分享
    代码:
    1.首先要在项目的 Build Phases 中导入social.framework
    2.在要实现的类中导入头文件 #import <Social/Social.h>
    示例代码:

    - (void)showNormalActivityShareWithFilePath:(NSURL*)path{
    
        NSArray *activityItems = @[path];
        UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        //__weak typeof(self) weakself = self;
        activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
    //      if (activityType == nil && activityError == nil && completed == NO) {
    //          //关闭了分享弹框,给h5回调失败
    //       }else if (activityType != nil && completed) {
    //               // 分享成功,给h5回调
    //
    //      }else if (activityType != nil && activityError != nil) {
    //               // 操作失败,给h5回调;
    //      }else if (activityType != nil && completed == NO) {
    //               // 操作失败,给h5回调
    //               // 备忘录取消操作
    //      }else {
    //          //给h5回调
    //       }
            //上面判断是写的失败详细内容,若需求不需要这么详细,直接下面这段代码就可以
            if (activityType != nil && completed) {
                //分享成功,给h5回调成功
                [YNETAlertView showStatus:@"分享成功"];
            }else{
                //给h5回调失败
                [YNETAlertView showStatus:@"分享失败"];
            }
        };
            
        [self presentViewController:activityVC animated:YES completion:^{
            
        }];
    
    }
    
    - (void)testMethod{
        //1.通过url下载pdf,存入沙盒调起系统分享面板
        NSString    *fileStr = @"http://xxxxxxxxxx/12.pdf";
        //下载文件
        NSURLRequest * requst = [NSURLRequest requestWithURL:[NSURL URLWithString:fileStr]];
        [YNETProgressHUD show];
        NSURLSessionDownloadTask *download =  [[AFHTTPSessionManager manager] downloadTaskWithRequest:requst progress:^(NSProgress * _Nonnull downloadProgress) {
            NSLog(@"下载进度%f",1.0* downloadProgress.completedUnitCount/downloadProgress.totalUnitCount);
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
            NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
            return [NSURL fileURLWithPath:fullPath];
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
            NSLog(@"下载文件的本地地址%@",filePath);
            [YNETProgressHUD dismiss];
            if (filePath!=nil) {
                [self showNormalActivityShareWithFilePath:filePath];
            }
        }];
        //启动下载任务
        [download resume];
        
        //下面这段代码是模仿跟前端交互,前端传过来base64图片或者有其他文件转成的base64字符串进行解析模拟,一般文件都通过url下载后再调用系统面板,系统会自动解析分享的文件大小、格式,选择能打开文件的APP在面板上展示
        //2.通过base64字符串,转成nadata,再存入沙盒
        //图片转base64
        UIImage *img = [UIImage imageNamed:@"我的背景"];
        NSData  *imgData = UIImagePNGRepresentation(img);
        NSString    *base64Str = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
        //base64转nsdata
        NSData  *showData = [[NSData alloc]initWithBase64EncodedString:base64Str options:NSDataBase64DecodingIgnoreUnknownCharacters];
        //把data写入沙盒
        NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString    *path = [NSString stringWithFormat:@"%@/%@",documentDirectory,@"img.png"];
        NSFileManager   *fileManager = [NSFileManager defaultManager];
        BOOL isDir;
        NSLog(@"写入沙盒地址---%@",path);
        //先判断目录是否存在,不存在才创建
        if (![fileManager fileExistsAtPath:path isDirectory:&isDir]) {
            BOOL    res = [fileManager createFileAtPath:path contents:showData attributes:nil];
            if (res) {
                [self showNormalActivityShareWithFilePath:[NSURL fileURLWithPath:path]];
            }
        }
     
        
    }
    
    

    只要url正常下载文件成功,实现上面的方法就可以啦
    注意:pdf等文件只能给路径,不然系统面板不会显示微信、微博、QQ这些软件选项

    相关文章

      网友评论

          本文标题:iOS开发实现调用原生分享面板分享pdf等文件

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