Loading web view taking more time with ASIHTTPRequest in ios - iphone

In my browser based application, I need to set a proxy for each url and doing this with the help of ASIHTTPRequest.
The problem I am facing is that the web view is taking double the time to load the page, probably because I am loading the page twice in my code.
First I check the status with ASIHTTPRequest to determine if the page is allowed to load by ASIHTTPRequest and if so, then I load that url on web-view.
This is where I think the problem is as I think I am loading the url two times which is consuming time.
Can you make a suggestion on other ways to load page once, but in a way that supports checking for authenticated page with usage of proxy settings, or provide me with a link to guide relevant to this question?
NSString *response = [NSString stringWithContentsOfFile:
[theRequest downloadDestinationPath] encoding:[theRequest responseEncoding] error:nil];
int statusCode = [requestH responseStatusCode];
if (statusCode == 200) {
[webV loadRequest:[NSURLRequest requestWithURL:[requestH url]]];
}
else {
[webV loadHTMLString:response baseURL:[theRequest url]];
}

Implement the delegate methods of NSURLConnection (apple docu) and in the connectionDidFinishLoading save the content of the url to a local file and then in load this local file with loadHTMLString.

Related

GET and POST requests from ios5

I'm working on making a client for my REST service on the iPhone. I'm a little lost as to how I go about making the GET and POST requests. I make the url from a NSString, convert it to an NSURL and create the NSURLRequest based off of the url. After that I'm pretty lost. Also, sometimes I care about the response, other times I don't. For example, when making a request for a new id, I care about the response because it's the id I'll use to upload my file later, but when I upload the file I don't care because the server doesn't send a response.


Does anyone have some (hopefully)simple sample code that they could point me to / share?

What I have so far:
-(NSString *) makeGetRequest:(NSString *)url :(Boolean)careAboutResult
{
NSString *results = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSError *reqError;
NSURLResponse *response = nil;
if(careAboutResult == YES)
{
//get the result
}
return results;
}

In the code I'm testing with, the URL is
http://192.168.0.108:8081/TestUploadService/RestfulUpload.svc/id/test123_DOT_png
and I'm saying I do care about the result.

Any help would be greatly appreciated.
#nick its good you have created a NSURLRequest now you just need to create a connection to send this request and receive response, this request is GET request.
To make POST request you will need to use NSMutableURLRequest and set its method name and body content. Here in documentation you will find how you can do this.

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

Load image for UIImageView from the net with security

I'm trying to load an image into an UIImageView from the internet. This is no problem, but what I want is the image be secured with user credentials. So if you go to the direct link of the image, you get an login popup (secured directory).
How can I pass those user credentials so I can load the image from the net with user credentials?
The problem is that the image may not be accessed directly, but only via the iPhone app. If you have a better idea to maximize the security, let me know ;-)
Thanks in advance.
If you are using basic server authentication (which I think you are based on the "popup dialog"), I would recommend using ASIHTTPRequest http://allseeing-i.com/ASIHTTPRequest/How-to-use
It makes it very easy to download and make web requests. You can download the image and give the username and password by doing:
NSURL *url = [NSURL URLWithString:#"http://user:passwd#yoursite.com/secret/"];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url];
[req startSynchronous];
NSError *err = [req error];
if (!err) {
NSData *response = [req responseData];
UIImage* image = [UIImage imageWithData:response];
// do whatever you want with the image here, like put it into a UIImageView
}
I'm assuming you have access to the server side. Create a simple PHP script that takes in an HTTP POST request with the username and password, validates it and if it's valid, echoes back a JSON/XML/string with the link to the image or else echoes back a null value or something.
Call a POST request from your iphone using urlWithString and when you get a response, check to see if it's null, or if it has a link.
Handle the result accordingly (if null, tell the user that the login credentials are incorrect).
If not null, it's the image - display it. Hope this helps.

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.

Twitter profile image upload in objective-c

I want to upload an image to my twitter profile using objective-c. I saw in the twitter API that I need to send a HTML post to http://twitter.com/account/update_profile_image.format and send the picture as a parameter. I am done with the authentication. I am stuck with the uploading. Maybe somebody can help me with sending the picture as a parameter?
You should be using NSURLRequests and NSURLConnection to perform the API requests. If this is true, all you need to do is create an NSMutableURLRequest, set it's URL to the Twitter image upload API URL, set the method to POST.
Then you'll need to create an NSData object to represent your image, which you can do using
NSData *myImageData = [[NSData alloc] initWithData:[myImage CGImage]];
I don't know what the parameter name is for Twitter's upload API, so for arguments sake, lets call it "image". The next thing you need to do is set the image data as the request's body for the "image" parameter, like this
NSString *bodyString = [NSString stringWithFormat:#"image=%#", [[[NSString alloc] initWithData:myImageData encoding:NSStringUTF8Encoding] autorelease]];
[myRequest setBody:bodyString];
Then you can just start your NSURLConnection with the request and it should upload.
If you’ve managed to get started, then this post on CocoaDev should help you set the uploading up. There’s a sample linked at the top too.
I recommend using ASIHTTPRequest
What is ASIHTTPRequest?
ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.
It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The included ASIFormDataRequest subclass makes it easy to submit POST data and files using multipart/form-data.
See this blog post for an example
Somthing like this
// See http://groups.google.com/group/twitter-development-talk/browse_thread/thread/df7102654c3077be/163abfbdcd24b8bf
NSString *postUrl = #"http://api.twitter.com/1/account/update_profile_image.json";
ASIFormDataRequest *req = [[ASIFormDataRequest alloc] initWithURL:[NSURL
URLWithString:postUrl]];
[req addRequestHeader:#"Authorization" value:[oAuth oAuthHeaderForMethod:#"POST"
andUrl:postUrl andParams:nil]];
[req setData:UIImageJPEGRepresentation(imageView.image, 0.8)
withFileName:#"myProfileImage.jpg"
andContentType:#"image/jpeg" forKey:#"image"];
[req startSynchronous];
NSLog(#"Got HTTP status code from Twitter after posting profile image: %d", [req
responseStatusCode]);
NSLog(#"Response string: %#", [req responseString]);
[req release];