美文网首页
iOS WKWebView将文件存储到手机文件中

iOS WKWebView将文件存储到手机文件中

作者: anny_4243 | 来源:发表于2022-02-15 16:51 被阅读0次
@interface WebViewController()<UIDocumentInteractionControllerDelegate>
@property (nonatomic, copy) NSString *fileName; //文件名
@property (nonatomic, strong) UIDocumentInteractionController *documentInteractionController;
@end

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
  
    self.fileName = navigationResponse.response.suggestedFilename; //获取文件名称        
    decisionHandler(WKNavigationResponsePolicyAllow);
    
}

//保存到文件(弹出菜单点击存储到“文件”,即可把文件保存到手机文件中)
-(void)saveToFile{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *URL = [NSURL URLWithString:self.currentURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
    //判断是否存在
    if([self isFileExist:self.fileName]) {
        
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:self.fileName];
        self.documentInteractionController = [UIDocumentInteractionController
                                                  interactionControllerWithURL:url];
        [self.documentInteractionController setDelegate:self];
        [self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
        
        [self.documentInteractionController presentPreviewAnimated:YES];
        
    }else {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        weakify(self);
        NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){
        } destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
            NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
            NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:weakSelf.fileName];
            return url;
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            
            [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
            weakSelf.documentInteractionController = [UIDocumentInteractionController
                                                      interactionControllerWithURL:filePath];
            [weakSelf.documentInteractionController setDelegate:weakSelf];
            [weakSelf.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:weakSelf.view animated:YES];
            
            [weakSelf.documentInteractionController presentPreviewAnimated:YES];

        }];
        [downloadTask resume];
    }
}

相关文章

  • iOS WKWebView将文件存储到手机文件中

  • iOS数据存储

    iOS开发中数据存储有两类:一、存储为文件 如归档、解归档、plist文件存储及偏好设置都是存储为文件二、存储到...

  • 第六章--数据存储方案

    1.文件存储 将数据存储到文件中Context类中提供一个openFileOutput方法,可存数据到文件中,接收...

  • iOS基础之数据存储

    iOS数据存储 1. 概论 在iOS开发中数据存储的方式可以归纳为两类: 存储文件 和 存储到数据库. 2.文件存...

  • 数据存储之归档

    归档 NSKeyedArchiver : 序列化,把对象转为字节码,存储到文件中,然后将文件存储到硬盘中,实现数据...

  • Android数据存储

    一、文件存储 1、将文件存储到文件中 Context类中提供了一个openFileOutput(String na...

  • iOS 数据存储

    1.plist文件存储 iOS中手机应用数据存储是保存在手机里的应用沙盒中的 plist文件存储一般都是存取字典和...

  • 《第一行代码》学习笔记 第 6 章

    第 6 章 数据存储全方案,详解持久化技术 一:文件存储 将数据存储到文件中(使用 Java 流的方式将数据写入到...

  • Android 数据存储

    一、文件存储 1.将数据存储到文件中 Android中的文件存储机制是一种基本的存储数据的方式,其不对数据进行任何...

  • 将数据存储到文件中

网友评论

      本文标题:iOS WKWebView将文件存储到手机文件中

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