how to open url need username and password with uiwebview? - iphone

I want to open an URL which need password and username in a UIWebview. Such as open my local Wifi Router(192.168.1.1). But when I try following code, there is no popup as Safari to require password and username.
NSURL *url = [NSURL URLWithString:#"http://192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];
Since someone told me to use NSURLConnectionDelegate, I know this, but I donot know how to show the authorized page to the UIWebView.

This will help
NSURL *url = [NSURL URLWithString:#"http://username:password#192.168.1.1"];
NSURLRequest *httpReq = [NSURLRequest requestWithURL:url];
[self._webView loadRequest:httpReq];

You need to implement connection:didReceiveAuthenticationChallenge: in NSURLConnectionDelegate. For details, read Authentication Challenges chapter from Apple "URL Loading System Programming Guide".

UIWebView doesn't provide any mechanism to identify the response. So one solution is explained in UIWebViewHttpStatusCodeHandling github project which identifies the status code of the response (should be 404 in your case).
However, the main drawback is for each request you need to use NSURLConnection and load the request again. But in this case, you can cancel the NSURLConnection too.
The other solution should (might) be the use of Java-Script. Search for the Java-script, which provides the status code of the response.

Related

Get cookie from NSURLRequest to WebView

The goal of my program is to do the following:
Execute a url on my website, which 'logs' the user in using NSURLRequest. Basically my thought is to execute the request, get the cookie from the response and add the cookie to the WebView.
All I know how to do is execute the first part of this process, and the code is below for that:
NSString *fullURL = #"http://mysite.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
content is the name of my webview
[content loadRequest:theRequest];
Next up is parsing the response. Basically check for error (the page only outputs the word 'true' or 'false' for error. I'm guessing I can check for the existence of those words and act appropriately. And of course, getting a cookie from the response and sending it to 'content'. Would appreciate help, thanks!
Normally URL loading system automatically handles sending and storing all URL related cookies with your NSURLRequest. However if you want to add an extra cookie, you must use NSMutableURLRequest.
Cookies are part of request response headers, which you can modify like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:cookie forHTTPHeaderField:"Cookie"];

UIWebview slow loading secured page

I have a site I'm loading with a UIWebView that has some problems loading when secured with Basic Authtype of Apache:
NSString * myUrlAddress = [NSString stringWithFormat:#"http://user:pass#mysite.mydomain.com"];
NSURL *url = [NSURL URLWithString:myUrlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
[webView loadRequest:requestObj];
Initial loading works most of the times, but sometimes, especially on reloading of the app the callback
- (void)webViewDidFinishLoad:(UIWebView *)webViewloc
is not reached, and it is also not running into
-(void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error
If I use a different server without .htaccess to secure the page it all works fine.
I also see in the access log, that it sometimes just stops loading from the page.
Has this something to do with cachePolicy or timeoutInterval ?
You could try to manage the authentication challenge coming from the server on your own, since it seems that UIWebView is not able to handle it.
This requires setting up your own NSURLRequest/Connection and handle the challenge in the connection:didReceiveAuthenticationChallenge delegate method before trying to load the actual html page in your UIWebView.
You can find full sample code here for a GET request. You might also check into a third-party networking framework that makes handling the communication and the challenge easier (e.g., ASIHTTPRequest, although it is now in a "frozen" state it works well; or AFNetworking)
NSString * myUrlAddress = [NSString stringWithFormat:#"http://user:pass#mysite.mydomain.com"];
NSURL *url = [NSURL URLWithString:myUrlAddress];
[webView loadRequest:[NSURLRequest requestWithURL:url]];

UIWebView won't load links with certificate (https:// prefix)

I know this has been asked before but I have looked at every answer (there aren't many) and none help me.
The issue I am running into is dealing with certificates with my schools e-mail service. The links for the two e-mail services are here:
Main school e-mail:
https://marauder.millersville.edu/mail/index.pl
Computer Science e-mail:
https://cs.millersville.edu/cswebmail
University student portal (Marauder Mail button on the right doesn't open when clicked in my UIWebView)
http://myville.millersville.edu/
At first neither of the websites would load in my UIWebView using a standard NSURL and NSURLRequest. I looked
on the web for a solution and someone suggested using the NSURLProtocolClient delegate methods. After
implementing them, my UIWebView will now load the schools mail e-mail ONLY when I have a button that when
clicked opens the link directly, as opposed to trying to access the mail from the portal site (3rd link above), and it
still never loads the computer science e-mail link. I have scoured the iOS help sites, posted questions, tried
multiple open-source custom UIWebViews, but I have not found anything that works.
Most answers I have read around the web point to ASIHTTPRequest. I have tried this but I cannot implement it right, it doesn't load the links in my UIWebView; here is my code for how I am loading a link:
mURL = [self getURL:viewNumber];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:mURL];
[req setDelegate:self];
[req startSynchronous];
//NSURLRequest *req = [NSURLRequest requestWithURL:mURL];
//urlConnection=[[NSURLConnection alloc] initWithRequest:req delegate:self];
webView.scalesPageToFit = YES;
//[webView loadRequest:req];
Code in comments is how I load a link without ASIHTTPRequest.
Any help is appreciated!
Also, I have another issue with my UIWebView, link is below. I haven't had any answers so if you're bored please check it out:
UIWebView doesn't detect text box on website
Fixed my issue, I was able to load https with no problem with the following code I didn't think this would work but it does!:
NSURLRequest *req = [NSURLRequest requestWithURL:mURL];
urlConnection=[[NSURLConnection alloc] initWithRequest:req delegate:self];
webView.scalesPageToFit = YES;
[webView loadRequest:req];
Web view does not load https urls due to certificate mismatch. After writing this extension it would work as expected
Swift 2.2
extension NSURLRequest {
static func allowsAnyHTTPSCertificateForHost(host: String) -> Bool
{
return true
}
}

Newsletter and registration on iphone

I'd like to know if it was possible, if a user wishes to subscribe to updates of my applications, take a form that is automatically subscribed to this newsletter at this address http://www.gseo.it/lists/?p=subscribe&id=2 (this is my mailing list with double opt in) but I'd like to know that a user can subscibe this newsletter directly from my iphone app.
Thanks
You could do an HTTP POST to that form using ASIFormDataRequest.
This isn't working code, but it might look something like:
NSURL *url = [NSURL URLWithString:#"http://www.gseo.it/lists/?p=subscribe&id=2"];
ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
[request setPostValue:#"someone#example.com" forKey:#"email"];
[request startSynchronous];
You can get the library here.
Yes of course you can, open up a UIWebview with the the url provided. Don't forget that this may look not good in the iphone browser so providing a custom html code depending on the user agent may improve things.

Unable to POST data from IPhone using google account authentication

I'm working on an IPhone application that works with a Google App Engine application. I manage to get logged by using a google account and I get the authentication token. I'm also able to GET data from the GAE service (I did it after reading another question written here) but now I need to POST data so I need to send the authentication token in the header of the POST request. I tried several options but none of them worked.
Here is the code I use to put that auth into the header:
NSString* urlStr = [NSString stringWithFormat:#"%#%#", HOST, url];
NSMutableURLRequest* urlPost = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
NSString* authStr = [NSString stringWithFormat:#"GoogleLogin auth=%#", token];
[urlPost addValue:authStr forHTTPHeaderField:#"Authorization"];
but it doesn't work.
Any help?
You need to use [request setHTTPMethod: #"POST"] and [request setHTTPBody: postdata] to properly configure the POST components. See the NSMutableURLRequest docs for more details.
Whenever I'm troubleshooting a problem related to HTTP, the first tool I'll grab is Charles HTTP Proxy. It will show you the entire request and response for closer examination.
If you're authenticating against an App Engine app, you need to obtain and send an authentication cookie, rather than using the GoogleLogin authentication. The source of appengine_rpc.py in the Python SDK demonstrates how.