Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView windows authentication in swift programming

I have a problem with the UIWebView. I cant open a Website with Windows Authentication... In the Safari Browser a pop-up appears, therefor in the WebView Control happened nothing.

Can someone help me with this problem?

Here my code

class MainViewController: UIViewController, UIWebViewDelegate, MFMailComposeViewControllerDelegate {

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad();

        webView.delegate = self;

        let url = NSURL (string: "http://test.raphaels.com/mobile/");
        let requestObj = NSURLRequest(URL: url);
        webView.loadRequest(requestObj);
    }
}

This piece code of works perfectly fine on non-secure server(URL) on iOS simulator, thats iPhone simulator/emulator on Xcode. But when I use the above URL(can only be accessed from intranet). It asks for username/password in browser and stops in iPhone emulator. Please help me, how can i supply username and password. I searched on internet and found many examples, unfortunately none of them was in swift programming.

I tried to write a solution which fails, so below is the failing code

override func viewDidLoad() {
    super.viewDidLoad();

    webView.delegate = self;


    let username = "username11";
    let password = "password22";
    let loginString = NSString(format: "%@:%@", username, password);
    let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!;
    let base64LoginString = loginData.base64EncodedStringWithOptions(nil);

    // create the request
    let url = NSURL (string: "http://test.raphaels.com/mobile/");
    let request = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")




    webView.loadRequest(request);
}
like image 502
Sandra Avatar asked Feb 26 '26 03:02

Sandra


1 Answers

And it worked :-) With below piece of code.

override func viewDidLoad() {
        super.viewDidLoad();

        webView.delegate = self;

        let urlComponents = NSURLComponents(string: "http://test.raphaels.com/mobile/");
        urlComponents.user = "username11";
        urlComponents.password = "password22";

        let url = urlComponents.URL;

        let requestObj = NSURLRequest(URL: url!);
        webView.loadRequest(requestObj);
    }
like image 114
Sandra Avatar answered Feb 28 '26 15:02

Sandra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!