vue wkwebview 下载文件
首先:iOS 不支持 js 下载文件
vue 直接写 a标签 window.open 或者 是from表单 都不支持直接下载
但是:我们可一调用 safari 浏览器来实现,你可一通过js调用oc 方法在调用 safari 打卡你要下载文件的连接
wkwebview 实现WKScriptMessageHandler中userContentControllerdidReceiveScriptMessage方法
WKWebViewConfiguration * webConfiguration = [WKWebViewConfiguration new];
webConfiguration.preferences= [WKPreferencesnew];
webConfiguration.preferences.minimumFontSize=10;
webConfiguration.preferences.javaScriptEnabled=YES;
webConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
//注入js方法
[webConfiguration.userContentController addScriptMessageHandler:self name:@"file_url"];
webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds configuration:webConfiguration];
webView.scrollView.alwaysBounceHorizontal = NO;//scrollView
webView.scrollView.alwaysBounceVertical=NO;//scrollView
NSString *urlStr = @"http://xxxxxxx";
NSURL*url = [NSURLURLWithString:urlStr];
NSURLRequest*request = [[NSURLRequestalloc]initWithURL:url];
webView.UIDelegate=self;
webView.navigationDelegate = self;
[webViewloadRequest:request];
[self.view addSubview:webView];
#pragma mark - WKScriptMessageHandler 实现方法
- (void)userContentController:(WKUserContentController*)userContentControllerdidReceiveScriptMessage:(WKScriptMessage*)message{
NSLog(@"name:%@ \\\\n body:%@ \\\\n frameInfo:%@\\\\n",message.name,message.body,message.frameInfo);
if([message.nameisEqualToString:@"file_url"]){
NSString* string1 = [message.body stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];//中文转utf8 要不然不跳转哦。
NSURL* url= [NSURLURLWithString:string1];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
}]; }
}
上一篇: 下载和重命名文件的 JavaScript
下一篇: 下载文件的 js 实现
推荐阅读
-
vue3 导出、下载、预览、上传
-
msvcr100.dll 丢失解决方案,如何安全下载 msvcr100.dll 文件:完整指南
-
从远程 Windows 共享目录读取文件 + 解析后缀为 .mdb 的文件 - 两种下载文件的方法:jcifs、smbj
-
vue 实现文件预览和文件上传、下载、预览 - 多图、模型、dwg 图纸、文档(word、excel、ppt、pdf)
-
浏览器服务器端文件下载控制(安全拦截、文件浏览器打开或下载行为控制)
-
springboot+vue+elementui 分片上传大文件
-
前端使用 vue 点击上传文件,然后将文件传输到后端,后端接收文件 II。代码部分
-
主机文件和 Mac 版 switchhosts 下载
-
自动解压压缩文件并下载安装autodl工具
-
实现纯前端下载PDF链接文件的解决方案,不进行预览操作