Reputation: 497
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);
}
Upvotes: 3
Views: 3771
Reputation: 21
A quick and easy solution is: use URL as
http://username:[email protected]/mobile/
No need for any special code.
Upvotes: 0
Reputation: 3436
Objective C version
NSURLComponents *urlComponents = [NSURLComponents componentsWithString:@"http://test.raphaels.com/mobile/"];
[urlComponents setUser:@"username11"];
[urlComponents setPassword:@"password22"];
NSURL *url = [urlComponents URL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
Upvotes: 0
Reputation: 497
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);
}
Upvotes: 6