简体   繁体   中英

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.

I have got the UIWebView to display the HTML via .loadHTMLString() , however the images, CSS and Javascript files are not loading. 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.

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.

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 . 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM