Authentication for NSURLConnection when it is not required - iphone

I'm trying to download a file from google trends. I'm using NSURLConnection to get the file.
I want to let google know I'm logged in (ie authenticate the connection) in order to have no download limit. I tried using:
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
The problem is since the login is not required, the method is never being called. Is there a way I can make sure the connection is authenticated? Thx!
Note: I asked this question before here, but I didn't get a reply so I'm asking again - hope that's ok
EDIT: Maybe I can use - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse ? Can I somehow authenticate through the cache?

you have to set the correct header fields of the NSURLRequest. Take a look at the answer of this post. You will have to look for base64 encoding too.

Related

Sending data to a website and getting results of search iOS

I am very new to iOS and I've just begun reading about HTTP requests and POST and GET methods. Let's say, for example, I want to have the user input a string, and then send that data to a website, (for this example, say www.rhymezone.com), search with that string, and get the results of that search within my application. Is this done with an HTTP post method? Or what? Any help / examples would be greatly appreciated. Also, if there are tutorials for this stuff, that would be appreciated as well.
For sake of example, here is what I've tried:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.rhymezone.com/r/rhyme.cgi?Word=test&typeofrhyme=perfect&org1=syl&org2=l&org3=y"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *dataAsString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data: %#",dataAsString);
}
This outputs the entire source of the website (searching for rhymes of the word test). While I can certainly write a method to go through the source of the website and extract the words it returns, I feel like this is not correct. My way of getting rhymes of different words is simply to change the URL here, so where it says 'test' I change it to whatever the user inputs.
Thanks
Look into AFNetworking and RestKit.
It's easiest if you're calling a public API that uses JSON/XML, and then use a built in parser or a parser library to extract the data you want.
Simply downloading the contents of a URL is an HTTP GET request, such as going to a website.
This link talks a bit more about the difference between GET and POST.
When do you use POST and when do you use GET?
If I understand correctly what you are trying to do, I fear that the only option for you is sending the HTTP request (GET or POST according to what the website expects, just like you are doing) and then parse the result to filter all the information that is not relevant.
An alternative approach would be possible if you were using a website offering a REST API, or a JSON API so that you send the query and you get back just the information you need (in a specific format).
So, it depends strongly on the website you are using, but for the generic case, the only option you have is parsing.
(Or, you could display the full content of the page through UIWebView. This would not require explicitly setting up a connection, but I am not sure it is what you are trying to do.)
You are looking for a way to communicate with your website from your iOS application. The common approach is to get the string entered by the user, encode and send it as http request to a sort of script (webservice). This script will do all the stuff you want (search with this string). Then re-send the result to the client (your iOS app) as a http response which will be parsed in your iOS app(with a JSON parser for instance).
There is good resources around that, as an example, you may read this: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

iOS NSURLConnection POST error handling

So for an iOS client of mine, I'm initiating a POST to upload a big chunk of data (say, 30MB), via:
[[NSURLConnection connectionWithRequest:req delegate:self] retain];
However, I need to handle the case where the URL is bad - say, one that returns a 404. The problem is, my didSendBodyData delegate method is called over and over, just like it should be assuming the URL is correct, in chunks of 32k worth of data, until it hits the 30MB total file size. I'm actually doing calculations in this method to show and display a progress bar, and even with an invalid URL, it looks and acts like it's transferring the whole file (I'm not sure what's really going on under the covers - it can't actually be doing anything, the URL resolves to a 404!)
THEN, after 20 seconds of supposed uploading, my didReceiveResponse delegate method is finally called, where I can check the NSURLResponse and see that a 404 was served up. Why does it seem like it transfers the entire file before I'm able to get any kind of error? Using [NSURLConnection connectionWithRequest], how can I know this was a 404 before it actually does anything? Is there anyway to tell in the didSendBodyData method, or any other way to tell earlier than what I'm seeing?
An even bigger problem is, I let you limit the upload time, so it stops after x number of seconds, at which point I cancel the connection (in the middle of the didSendBodyData method), so doing that there's seemingly no way to know anything even went wrong with the URL.
It seems like this should have a ridiculously easy solution, but I'm not seeing it. Thanks for any help.
Based on the spec, I don't believe that an HTTP server sends back an HTTP response until the HTTP request has been completely received. This means that even though you are sending a bunch of data to nowhere, the behavior you are seeing is expected.
You might try sending the HTTP Expect Header in your request. Otherwise, have you considered sending out a preliminary ping request to check if the url endpoint exists? Maybe a POST with no data? Then you can find out about the 404 before making the costly request.
Have you implemented the NSURLConnectionDataDelegate ?
As the following delegate methods could be what you're after:
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
if you just want to examine the response code and perform some action then I would add the following to your code
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 404){
NSLog(#"Epic Fail");
}
...
...
}

How to identify whether response coming from server is pdf or url or image?

In my app, when I send request to server either I will get url or pdf or image in response and based on that I will display the result (url->webview, image->imageview etc).But I do not know how to identify whether the response is url or pdf or image?
Please anyone know how I can achieve this?
Thanks in advance!
I assume you do use NSURLConnection to download your file. So, implement this delegate method ...
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
... cast response to NSHTTPURLResponse and get Content-Type value from allHeaderFields.
But it depends what do you really use to download your files. If you do use another class/framework/..., you get an idea.
I would like to suggest you to use UIWebview for both case either it is PDF or image url from server.
[imgwebVw loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strImgURL]]];
If you really must do this you can read the file headers but I wouldn't suggest it. here is a category I wrote to get the size of a remote image, you can adapt it to detect if the file is indeed an image or not. And you can do similar stream parsing to search for the PDF Header: %PDF-1.0 etc. Otherwise you would assume it is a url.

How To create and Send a JSON object

I have a in app purchase store in my app.
Now I need to take the SKPaymentTransaction and store the receipt data in to a JSON object.
After that I have to send it to apple to validate and get the response from:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
I know how to get the receipt, but I need help from that step.
Almost everyone uses the fine JSON framework here:
JSON Framework
You can encode and decode JSON with a minimum of fuss.

post redirected url accessed by objective-c message

I wanted to know if it is possible and if so how to know the url you are accessing via objective-c takes you to the correct webpage. To elaborate further:
If a user is using an app that connects to a webpage to get it's data, and if this user is say at an airport. The internet connection at airport in many cases will redirect u to their webpage (regardless of what url u maybe type) where u have to either pay or something to get any further internet access. Thus if a app needing data from a webpage is used without user's knowledge of this limited access to internet, i want to display a popup message that informs the user saying something that the webpage it tried to access was required to a different webpage thus cannot access data. As oppose to getting false data and the user thinking the app is buggy.
Is there a way to do this. Thanks in advance for all of your help.
If you are using NSURLConnection you can implement the NSURLConnectionDelegate method
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
which is called if it is determined that the request must redirect to a new location. The delegate can the choose to allow the redirect, modify the destination, or deny the redirect.
You can read more about NSURLConnectionDelegate and its methods here