如何使用WKWebView检索文件? [英] How can I retrieve a file using WKWebView?

查看:154
本文介绍了如何使用WKWebView检索文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载一个文件(CSV)。它位于网站的登录屏幕后面。我想显示 WKWebView ,以允许用户登录,然后让应用程序在登录后下载文件。

There is a file (CSV) that I want to download. It is behind a login screen on a website. I wanted to show a WKWebView to allow the user to log in and then have the app download the file after they had logged in.

用户登录网站后,我尝试在 WKWebView 之外下载文件,但会话数据似乎是沙箱,因为它下载了使用登录表单而不是所需文件的html文档。

I've tried downloading the file outside of WKWebView after the user has logged in to the website, but the session data seems to be sandboxed because it downloads an html document with the login form instead of the desired file.

我还尝试将 WKUserScript 添加到 WKUserContentController object,但加载非HTML文件时脚本无法运行。

I've also tried adding a WKUserScript to the WKUserContentController object, but the script doesn't get run when a non-HTML file is loaded.

有没有一种方式让我访问这个文件,同时允许用户通过 WKWebView 登录?

Is there a way for me to access this file while allowing users to log in via the WKWebView?

推荐答案

目前,WKWebView实例将忽略任何默认网络存储(NSURLCache,NSHTTPCookieStorage,NSCredentialStorage)以及可用于自定义网络请求的标准网络类(NSURLProtocol,等等。)。

Right now, WKWebView instances will ignore any of the default networking storages (NSURLCache, NSHTTPCookieStorage, NSCredentialStorage) and also the standard networking classes you can use to customize the network requests (NSURLProtocol, etc.).

因此,WKWebView实例的cookie不存储在应用程序的标准Cookie存储中,因此仅使用标准Cookie存储的NSURLSession / NSURLConnection无法访问WKWebView的cookie(确切地说这可能是你遇到的问题:登录状态很可能存储在cookie中,但NSURLSession / NSURLConnection将看不到cookie)。

So the cookies of the WKWebView instance are not stored in the standard Cookie storage of your App, and so NSURLSession/NSURLConnection which only uses the standard Cookie storage has no access to the cookies of WKWebView (and exactly this is probably the problem you have: the „login status" is most likely stored in a cookie, but NSURLSession/NSURLConnection won’t see the cookie).

对于缓存,凭证等情况也是如此.WKWebView有自己的私有存储,因此不能很好地使用标准的Cocoa网络类。

The same is the case for the cache, for the credentials etc. WKWebView has its own private storages and therefore does not play well with the standard Cocoa networking classes.

您也无法自定义请求(添加自己的自定义HTTP标头,修改现有标头等),使用您自己的自定义URL方案等,因为也不支持NSURLProtocol通过WKWebView。

You also can’t customize the requests (add your own custom HTTP headers, modify existing headers, etc), use your own custom URL schemes etc, because also NSURLProtocol is not supported by WKWebView.

所以现在WKWebView对许多应用程序来说都没用,因为它不参与Cocoa的标准网络API。

So right now WKWebView is pretty useless for many Apps, because it does not participate with the standard networking APIs of Cocoa.

我仍然希望Apple能够在iOS 8发布之前对其进行更改,因为否则WKWebView对于许多应用程序来说都是无用的,而且我们可能会更长时间地使用UIWebView 。

I still hope that Apple will change this until iOS 8 gets released, because otherwise WKWebView will be useless for many Apps, and we are probably stick with UIWebView a little bit longer.

因此,向Apple发送错误报告,以便Apple了解这些问题是严重的,需要修复。

So send bug reports to Apple, so Apple gets to know that these issues are serious and needs to be fixed.

这篇关于如何使用WKWebView检索文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆