如何从 iOS Objective-C 中 WKWebView 中加载的 URL 获取 NSData?

How to get NSData from URL loaded in WKWebView in iOS Objective-C?

提问人:Yogendra Patel 提问时间:8/1/2020 更新时间:8/1/2020 访问量:342

问:

我已经将UIWebView迁移到WKWebView。我已经成功完成了迁移。但是在我的应用程序中有一个功能。该功能是用户获取加载的 URL 数据,并在单击按钮时发送到我们的服务器。此功能适用于 UIWebView,但不适用于 WKWebView。 法典:

NSURL *fileURL = [self.webView URL];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:fileURL completionHandler:^(NSData *data,
                                                          NSURLResponse *response,
                                                          NSError *error)
{

}] resume];

使用 UIWebView 时返回上述代码的值:

数据:

<OS_dispatch_data: data[0x10bb8c000] = { composite, size = 1184733, num_records = 1037 record[0] = { from = 0, length = 769, data_object = 0x174c66c40 }, record[1] = { from = 0, length = 2352, data_object = 0x17506cb80 }, record[2] = { from = 0, length = 711, data_object = 0x17467fb00 }, record[3] = { from = 0, length = 3361, data_object = 0x17427b400 }, record[4] = { from = 0, length = 4072, data_object = 0x174e79b80 }, record[5] = { from = 0, length = 4072, data_object = 0x174e7ec80 }, record[6] = { from = 0, length = 4072, data_object = 0x174c7ca40 }, record[7] = { from = 0, length = 771, data_object = 0x174e60ac0 }, record[8] = { from = 0, length = 1024, data_object = 0x17506bfc0 }, record[9] = { from = 0, length = 2277, data_object = 0x174c61c80 }, record[10] = { from = 0, length = 786, data_object = 0x17127dac0 }, record[11] = { from = 0, length = 309, data_object = 0x174a71400 }, record[12] = { from = 0, length = 1730, data_object = 0x174c72c00 }, record[13] = { from = 0, length = 1247, data_object = 0x17506bf40 }, record[14] = { from = 0, length = 792, data_object = 0x174a7d180 }, record[15] = { from = 0, length = 2048, data_object = 0x171261780 }, record[16] = { from = 0, length = 1024, data_object = 0x171278fc0 }, record[17] = { from = 0, length = 208, data_object = 0x170275800 }, record[18] = { from = 0, length = 807, data_object = 0x17106a6c0 }, record[19] = { from = 0, length = 1024, data_object = 0x175067c80 }, record[20] = { from = 0, length = 2241, data_object = 0x174e68840 }, record[21] = { from = 0, length = 4072, data_object = 0x174271680 }, record[22] = { from = 0, length = 1047, data_object = 0x171466300 }, record[23] = { from = 0, length = 90, data_object = 0x170e7ad40 }, record[24] = { from = 0, length = 2935, data_object = 0x174e76bc0 }, record[25] = { from = 0, length = 1143, data_object = 0x170e7e8c0 }, record[26] = { from = 0, length = 2048, data_object = 0x170a7ce80 }, record[27] = { from = 0, length = 881, data_object = 0x17506ad80 }, record[28] = { from = 0, length = 2182, data_object = 0x175067640 },>

响应:

<NSHTTPURLResponse: 0x174e2f1e0> { URL: https://student.globalpay.wu.com/geo-buyer/services/buyer/downloadInstruction } { status code: 200, headers {
    "Cache-Control" = "no-cache, no-store, must-revalidate";
    "Content-Disposition" = "file; filename = instructions.pdf";
    "Content-Type" = "application/pdf";
    Date = "Fri, 31 Jul 2020 14:12:45 GMT";
    Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
    Pragma = "no-cache";
    Server = none;
    "Strict-Transport-Security" = "max-age=31536000";
    "x-edgeconnect-midmile-rtt" = 207;
    "x-edgeconnect-origin-mex-latency" = 706;
    "x-powered-by" = "Servlet/3.0 JSP/2.2";
} }

错误:

nil

使用 WKWebView 时上述代码的返回值:

数据:

<OS_dispatch_data: data[0x170e637c0] = { leaf, size = 113, buf = 0x1702f8480 }>

响应:

<NSHTTPURLResponse: 0x170a3de00> { URL: https://student.globalpay.wu.com/geo-buyer/services/buyer/downloadInstruction } { status code: 500, headers {
    "Cache-Control" = "no-cache, no-store, must-revalidate";
    "Content-Length" = 113;
    "Content-Type" = "application/xml";
    Date = "Fri, 31 Jul 2020 10:03:55 GMT";
    Expires = "Thu, 01 Jan 1970 00:00:00 GMT";
    Pragma = "no-cache";
    Server = none;
    "Set-Cookie" = "JSESSIONID=ba2kU_-n1BeO_GDhBgzEds4kHpr8oDzGSFVEmYln5DAtTMYQVhCS!2050459021; path=/; secure; HttpOnly";
    "Strict-Transport-Security" = "max-age=31536000";
    "gpfs.invalid.session" = true;
    "x-edgeconnect-midmile-rtt" = 210;
    "x-edgeconnect-origin-mex-latency" = 51;
    "x-powered-by" = "Servlet/3.0 JSP/2.2";
} }

错误:

nil

点是两个响应值的状态码不同,在UIWebView响应状态码为200,WKWebView响应状态码为500。此外,数据值也不同。

我不知道为什么会得到不同的价值。

我想像UIWebView一样获取相同的数据和响应。

iOS Objective-C Swift UIWebView wkwebview

评论

0赞 Sylvan D Ash 8/1/2020
我怀疑这是一个问题,或者因为你只是使用它们来获得UIWebViewWKWebViewurl
0赞 Sylvan D Ash 8/1/2020
你能添加如何在 Swift 中获取数据的代码吗?
0赞 Yogendra Patel 8/1/2020
WKWebVew @SylvanDAsh问题可能是 cookie 相关问题。
0赞 Sylvan D Ash 8/1/2020
但是当你使用时,你会得到正确的网址,从你的代码来看,你似乎用来加载网址......因此,为什么它似乎不是问题,除非您包含其他参数?WKWebViewURLSessionWKWebView
0赞 Yogendra Patel 8/1/2020
@SylvanDAsh是的,你说得对。我也对此表示怀疑,但在 1 小时之前我得到了与积分相关的 cookie。WKWebView 不像 UIWebView 那样保存 cookie,因此这可能是当前问题的问题。我仍然不确定。

答: 暂无答案