James
James

Reputation: 1108

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.

Upvotes: 0

Views: 1068

Answers (1)

mattsven
mattsven

Reputation: 23253

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?

Upvotes: 2

Related Questions