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.
Related
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.
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
For an iPhone app, I am downloading files using URL:
From this type of url:: http://xxxxxxxxxxxxxxxxxxx.com/xxxxxxx//xxx/ipad.html?operation=getFile&contentId=61768b16-6b44-4d0b-bdcf-d10107d1f328
I am downloading files from server but it may be of type .pdf or .docx or .doc
Is there any way to identify file type from url?
No, since the filename is not in the URL you can not make an assumption.
But after you received the header for the URL they may tell you what kind of file you will receive.
In the follwing NSURLConnectionDelegate method
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSString *mimeType = [response MIMEType]
NString *suggestedFilename = [response suggestedFilename];
}
Of course it works only if you get them from server.
There's no indication of type in the URL you provided, and even if the URL did appear to specify a certain type of file (e.g. http://foo.com/bar.pdf) there's no guarantee that the server would actually return a PDF file.
If you don't want to download the file you can always make a HEAD request to get just the HTTP headers and look at the value of the Content-Type header. Or, you can put an Accept header in your request to restrict responses to a certain type of data.
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");
}
...
...
}
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.