[英]Swift - UIWebView baseURL resulting in blank screen
I need to show a web browser in my app. 我需要在我的应用中显示网络浏览器。 I can't use SFSafariViewController as I need to be able to authenticate with NTLM, so I'm currently using Alamofire to download the data
and then displaying it in a UIWebView. 我无法使用SFSafariViewController,因为我需要能够通过NTLM进行身份验证,因此我目前正在使用Alamofire下载data
,然后将其显示在UIWebView中。
I have got the UIWebView to display the HTML via .loadHTMLString()
, however the images, CSS and Javascript files are not loading. 我有UIWebView通过.loadHTMLString()
显示HTML,但是图像,CSS和Javascript文件未加载。 I tried setting the baseURL
to the website root, however, when I do this nothing at all loads in the UIWebView
, leaving it blank with no content or errors in the console. 我尝试将baseURL
设置为网站根目录,但是,当我这样做时, UIWebView
完全没有加载,将其留空,控制台中没有内容或错误。
Alamofire.request(.GET, "https://dev-moss.stpaulscatholiccollege.co.uk/sites/Students/default.aspx").authenticate(usingCredential: credential)
.response { request, response, data, error in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// check for errors or bad responsecode
if (error == nil) && (response?.statusCode == 200) {
// load the website
let gatewayString = "\(NSString(data: data!, encoding: NSUTF8StringEncoding))"
self.gatewayWebView.loadHTMLString(String(NSString(data: data!, encoding: NSUTF8StringEncoding)!), baseURL: nil)
print(gatewayString)
}
else {
print("There was an error loading the website.")
}
})
}
Even when setting the baseURL
to NSURL(string: "https://dev-moss.stpaulscatholiccollege.co.uk/")
or NSURL(string: "https://dev-moss.stpaulscatholiccollege.co.uk")
the print
of gatewayString
still spits out the website to the console however. 设置即使baseURL
来NSURL(string: "https://dev-moss.stpaulscatholiccollege.co.uk/")
或NSURL(string: "https://dev-moss.stpaulscatholiccollege.co.uk")
的print
但是, gatewayString
仍会将网站吐出到控制台。
Your code isn't working because the base URL isn't https://dev-moss.stpaulscatholiccollege.co.uk/
, it's https://dev-moss.stpaulscatholiccollege.co.uk/sites/Students/default.aspx
. 您的代码无法正常运行,因为基本网址不是https://dev-moss.stpaulscatholiccollege.co.uk/
,而是https://dev-moss.stpaulscatholiccollege.co.uk/sites/Students/default.aspx
。 Try that and it should work. 试试看,它应该可以工作。
Edit: 编辑:
It looks like it's not working because https://dev-moss.stpaulscatholiccollege.co.uk/sites/Students/default.aspx
uses HTTP Basic Authentication, which you have not provided credentials for (the UIWebView makes a separate requests for resources from your one made via Alamofire, which doesn't include your credentials.) See How to do authentication in UIWebView properly? 似乎无法正常工作,因为https://dev-moss.stpaulscatholiccollege.co.uk/sites/Students/default.aspx
使用了HTTP基本身份验证,而您尚未为此提供凭据(UIWebView会从是通过Alamofire制作的,其中不包括您的凭据。)请参阅如何在UIWebView中正确进行身份验证?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.