UIWebview slow loading secured page - iphone

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]];

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"];

how to open url need username and password with uiwebview?

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.

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
}
}

Private browsing with UIWebView on the iPhone & iPad

How do existing apps implement this feature???
Can I store cookie only for certain sites, and only inside my app? It's my understand that the web view stores cookies in shared mode...so that they are shared with Safari and other apps that use UIWebView.
According to the NSHTTPCookieStorage docs, cookies are not shared between applications:
iPhone OS Note: Cookies are not shared
among applications in iPhone OS.
So it seems like they should be "private" by default. You can also use the [NSHTTPCookieStorage sharedHTTPCookieStorage] object to set the cookie storage policy to not store cookies at all, or you could use the deleteCookie: method to clean up after yourself if you needed to.
As for other content that is loaded by your UIWebview, when you create the NSURLRequest that is loaded by your webview, you can set a cache policy that controls if the content will be cached. For example:
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: url]
cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval: 60.0]
[webView loadRequest: request];
NSURLRequestReloadIgnoringLocalAndRemoteCacheData tells the request to ignore the cache and load the request from the network. I'm not sure if it also prevents the response from the network from being cached, but to be sure, you could alway remove it from the cache yourself:
[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];
If you're talking about Private Browsing, the UIWebView actually does not store history after the app is closed (only temporary for going back and forth). Instead you would have to implement storing history yourself, so it would be automatically Private Browsing.
Short answer: Don't do anything. Its already in Private Browsing mode.
EDIT: For handling cache check out this method:
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
And make cashedResponse return nil.

Spoofing the user agent of an embedded Safari browser on the iPhone?

Is there any way to spoof the user agent on Safari on the iPhone?
So for example, you would create an application on the iPhone that has the embedded Safari browser, however any website the user visits with this browser wouldn't know you were on Safari on the iPhone, it would think you are on something like Safari on a PC, or even IE/FireFox.
Thanks
Yes I think you could change this. It would require a bit of a work around to get it working.
You would need to manually manage all requests. By making your own data requests.
In this data request you can add a HTTPheader for User-Agent which will override the default headers.
NSMutableURLRequest* urlRequest = [[[NSMutableURLRequest alloc] initWithURL:requestURL] autorelease];
[urlRequest setHTTPMethod: #"POST"];
[urlRequest setHTTPBody: [nvpString dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest addValue:#"Your+User+Agent+String" forHTTPHeaderField:#"User-Agent"];
receivedData = [[NSMutableData alloc] retain];
[receivedData setLength:0];
[NSURLConnection connectionWithRequest: urlRequest delegate: self];
If you embed the Safari Web Browser in your app you can subscribe to its delegate methods. One of them will notify your application that safari would like to load a URL, this is where you catch this load and get the data your self.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
now you put your code in here to do the data load.
Once the data has loaded. Give it the data string back to the webView. I have set "baseURL:nil" but you might have to correctly set this to maybe the correct domain for this app.
[webView loadHTMLString:newString baseURL:nil]