
本文档旨在指导开发者在使用 WKWebView 加载 PHP 生成的文件时,如何在 iOS 应用中实现文件下载并保存到应用沙盒。针对 iOS 14.5 及以上版本,可以使用 WKDownloadDelegate 实现便捷下载。对于旧版本 iOS,则需要手动处理下载过程。本文将提供 Objective-C 代码示例,详细说明两种方案的实现方法,并提供相关注意事项。
iOS 14.5 及以上版本:使用 WKDownloadDelegate
自 iOS 14.5 起,WKWebView 引入了 WKDownloadDelegate,使得处理文件下载变得更加简单。以下是如何使用 WKDownloadDelegate 来下载 PHP 生成的文件:
-
设置 WKNavigationDelegate 和 WKDownloadDelegate:
首先,需要将 WKWebView 的 navigationDelegate 设置为自身,以便接收导航事件。同时,需要实现 WKDownloadDelegate 协议的方法。
立即学习“PHP免费学习笔记(深入)”;
- (void)viewDidLoad { [super viewDidLoad]; self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds]; self.webView.navigationDelegate = self; NSURL* url = [NSURL URLWithString: @"https://your-domain.com/download.php"]; NSURLRequest* request = [NSURLRequest requestWithURL: url]; [self.webView loadRequest:request]; [self.view addSubview:self.webView]; } -
实现 decidePolicyForNavigationResponse 方法:
在此方法中,判断 navigationResponse 是否可以显示 MIME 类型。如果不能显示,则表示需要下载。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(nonnull WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler { if (navigationResponse.canShowMIMEType) { decisionHandler(WKNavigationResponsePolicyAllow); } else { decisionHandler(WKNavigationResponsePolicyDownload); } } -
实现 navigationResponse:didBecomeDownload: 方法:
当导航响应触发下载时,此方法会被调用。在此处,需要将 download 的 delegate 设置为自身。
- (void)webView:(WKWebView *)webView navigationResponse:(WKNavigationResponse *)navigationResponse didBecomeDownload:(WKDownload *)download { download.delegate = self; } -
实现 download:decideDestinationUsingResponse:suggestedFilename:completionHandler: 方法:
此方法允许你决定下载文件的保存路径。在此处,你可以将文件保存到应用沙盒的 Documents 目录。
淄博分类信息港程序seo特别版下载seo特别版程序介绍:注意:普通用户建议使用淄博分类信息港程序普通版本。主要针对seo需要增加了自定义功能:自定义文件路径;自定义文件名;自定义关键字。这些功能的作用,只有自己体会了。以下是淄博分类信息港程序的介绍:淄博分类信息港程序一套现成的城市分类信息网站发布系统。发布管理房屋、人才、招租、招聘、求购、求租、搬迁、运输、二手交易、招生培训、婚介交友等各类信息的发布和查询。淄博分类信息港发布程序
- (void)download:(WKDownload *)download decideDestinationUsingResponse:(NSURLResponse *)response suggestedFilename:(NSString *)suggestedFilename completionHandler:(void (^)(NSURL * _Nullable))completionHandler { // Save to Documents NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [documentPath stringByAppendingPathComponent:suggestedFilename]; NSURL* url = [NSURL fileURLWithPath:filePath]; completionHandler(url); } -
实现 downloadDidFinish: 方法:
当下载完成时,此方法会被调用。可以在此处进行一些清理工作或通知用户下载完成。
- (void)downloadDidFinish:(WKDownload *)download { // Downloaded }
iOS 14.5 之前版本:手动下载
对于 iOS 14.5 之前的版本,需要手动处理下载过程。以下是如何实现:
-
设置 WKNavigationDelegate:
与上述方法类似,需要将 WKWebView 的 navigationDelegate 设置为自身。
- (void)viewDidLoad { [super viewDidLoad]; self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds]; self.webView.navigationDelegate = self; NSURL* url = [NSURL URLWithString: @"https://your-domain.com/download.php"]; NSURLRequest* request = [NSURLRequest requestWithURL: url]; [self.webView loadRequest:request]; [self.view addSubview:self.webView]; } -
实现 decidePolicyForNavigationResponse 方法:
在此方法中,判断 navigationResponse 是否可以显示 MIME 类型。如果不能显示,则发起手动下载。
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(nonnull WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler { if (navigationResponse.canShowMIMEType) { decisionHandler(WKNavigationResponsePolicyAllow); } else { NSURL* downloadUrl = navigationResponse.response.URL; NSURLSessionDataTask* dataTask = [NSURLSession.sharedSession dataTaskWithURL:downloadUrl completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { if (data != nil) { // Save to Documents NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *filePath = [documentPath stringByAppendingPathComponent:navigationResponse.response.suggestedFilename]; [data writeToFile:filePath atomically:YES]; } }]; [dataTask resume]; decisionHandler(WKNavigationResponsePolicyCancel); } }在此方法中,我们使用 NSURLSessionDataTask 来下载文件。下载完成后,将数据保存到应用沙盒的 Documents 目录。注意,decisionHandler 需要设置为 WKNavigationResponsePolicyCancel,以阻止 WKWebView 加载该 URL。
注意事项
- 权限: 确保你的应用具有访问 Documents 目录的权限。
- 错误处理: 在下载过程中,需要处理可能发生的错误,例如网络连接失败、文件写入失败等。
- 线程: NSURLSessionDataTask 的 completionHandler 在后台线程执行,如果需要更新 UI,需要切换到主线程。
- 文件名: navigationResponse.response.suggestedFilename 可能为空,需要进行判断和处理。
总结
本文档介绍了两种在使用 WKWebView 加载 PHP 生成的文件时,实现文件下载的方法。对于 iOS 14.5 及以上版本,可以使用 WKDownloadDelegate 实现便捷下载。对于旧版本 iOS,则需要手动处理下载过程。选择哪种方法取决于你的应用需要支持的 iOS 版本。希望本文档能帮助你解决相关问题。










