
本文档旨在指导开发者如何在 iOS 应用中使用 WKWebView 下载由 PHP 脚本动态生成的文件。针对 iOS 14.5 及以上版本,我们将介绍利用 WKDownloadDelegate 实现下载并指定保存路径的方法。对于更早的 iOS 版本,则提供手动下载数据并保存的替代方案。
自 iOS 14.5 起,苹果引入了 WKDownloadDelegate,使得在 WKWebView 中处理文件下载变得更加方便。通过实现该代理,我们可以拦截下载请求,并指定文件的保存路径。
以下是一个完整的示例,展示了如何使用 WKDownloadDelegate 下载 PHP 生成的文件:
#import <WebKit/WebKit.h>
@interface ViewController : UIViewController <WKNavigationDelegate, WKDownloadDelegate>
@property (nonatomic, strong) WKWebView *webView;
@end
@implementation ViewController
- (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];
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(nonnull WKNavigationResponse *)navigationResponse decisionHandler:(nonnull void (^)(WKNavigationResponsePolicy))decisionHandler {
if (navigationResponse.canShowMIMEType) {
decisionHandler(WKNavigationResponsePolicyAllow);
} else {
decisionHandler(WKNavigationResponsePolicyDownload);
}
}
- (void)webView:(WKWebView *)webView navigationResponse:(WKNavigationResponse *)navigationResponse didBecomeDownload:(WKDownload *)download {
download.delegate = self;
}
#pragma mark - WKDownloadDelegate
- (void)download:(WKDownload *)download decideDestinationUsingResponse:(NSURLResponse *)response suggestedFilename:(NSString *)suggestedFilename completionHandler:(void (^)(NSURL * _Nullable))completionHandler {
// 保存到 Documents 目录
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:suggestedFilename];
NSURL* url = [NSURL fileURLWithPath:filePath];
completionHandler(url);
}
- (void)downloadDidFinish:(WKDownload *)download {
// 下载完成
NSLog(@"Download finished");
}
- (void)download:(WKDownload *)download didFailWithError:(NSError *)error resumeData:(NSData *)resumeData {
// 下载失败
NSLog(@"Download failed with error: %@", error);
}
@end代码解释:
立即学习“PHP免费学习笔记(深入)”;
注意事项:
注意:请在linux环境下测试或生产使用 青鸟内测是一个移动应用分发系统,支持安卓苹果应用上传与下载,并且还能快捷封装网址为应用。应用内测分发:一键上传APP应用包,自动生成下载链接和二维码,方便用户内测下载。应用封装:一键即可生成app,无需写代码,可视化编辑、 直接拖拽组件制作页面的高效平台。工具箱:安卓证书生成、提取UDID、Plist文件在线制作、IOS封装、APP图标在线制作APP分发:
0
对于 iOS 14.5 之前的版本,我们需要手动下载数据并保存到本地。 以下是一个示例:
#import <WebKit/WebKit.h>
@interface ViewController : UIViewController <WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webView;
@end
@implementation ViewController
- (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];
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(nonnull WKNavigationResponse *)navigationResponse decisionHandler:(nonnull 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) {
// 保存到 Documents 目录
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentPath stringByAppendingPathComponent:response.suggestedFilename];
[data writeToFile:filePath atomically:YES];
NSLog(@"File downloaded to: %@", filePath);
} else {
NSLog(@"Download failed with error: %@", error);
}
}];
[dataTask resume];
decisionHandler(WKNavigationResponsePolicyCancel);
}
}
@end代码解释:
立即学习“PHP免费学习笔记(深入)”;
注意事项:
本文档提供了两种在 WKWebView 中下载 PHP 生成文件的方法,分别适用于 iOS 14.5 及以上版本和之前的版本。 使用 WKDownloadDelegate 可以更方便地管理下载过程,而手动下载数据则提供了对旧版本 iOS 的兼容性。 在实际开发中,应根据目标 iOS 版本选择合适的方法,并注意处理各种异常情况,以提供良好的用户体验。
以上就是使用 WKWebView 下载 PHP 生成的文件 (iOS)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号