0

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.

James
  • 1,088
  • 3
  • 11
  • 29

1 Answers1

2

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?

Community
  • 1
  • 1
mattsven
  • 22,305
  • 11
  • 68
  • 104
  • Unfortunately still not working with that base URL; shows white screen. – James Feb 27 '16 at 16:13
  • Thanks for the reference Matt, didn't think of that. Looks like it's going to become a bit more complex - if only SFSafariViewController had an authentication prompt delegate. – James Feb 27 '16 at 20:31
  • @James There's also `WKWebView`, which does have one. – mattsven Mar 01 '16 at 19:48