How to enable cookies when POSTING with URLConnection in iPhone SDK? - iphone

I am trying to do a POST towards a site which utilizes secure session with cookies.
Ofcourse this won't work with the code I have posted below. It keeps responding with a non-authorized message.
Is there any way I can use cookies in my code or at least simulate cookie usage?
NSURL *url = [[NSURL alloc] initWithString:#"https://long_and_complicated_url"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Best regards
//Abeansits

The SDK has cookie support in the form of NSHTTPCookie and NSHTTPCookieStorage
Have you familiarized yourself with these?

Related

Using gzcompress in PHP; need to be able to uncompress on iPhone

I'm sure this isn't too difficult (and I'm surprised I can't figure it out), but here goes:
So. I'm using gzcompress() to compress JSON data in PHP so I can send it into an iPhone app.
Having some troubles figuring out how to uncompress this data on the iPhone (iOS).
Any thoughts? I'm grabbing the data via a NSMutableURLRequest.
Thanks!
Code making the request:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
Code processing the response:
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
And on the PHP side:
echo(gzcompress(json_encode($lines)));
Thanks!!!
This article suggests that NSMutableURLRequest supports gzip decompression out-of-the-box, but you need to add an Accept-Encoding: gzip header to the request.
Extending your example:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:theURL]];
[request setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
I'd also recommend testing the PHP script with cURL to make sure it's sending valid gzipped data.

Creating HTTPS login on iOS?

How do I build an https login on iOS? (Like this one: https://info.tam.ch/kks)
I only know how to solve a .htaccess.
Thank you.
I use ASIHTTPRequest in my iPhone project to communicate with the web application. For http authentication, they have a convenient ASIAuthenticationDialog class that takes care of that. Basic usage is as follows:
// Specifying a username and password to use in the URL
NSURL *url = [NSURL URLWithString:#"http://username:password#allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// Setting a username and password to use on the request
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:#"username"];
[request setPassword:#"password"];
You can visit here for detailed information or check out the sample source code here on github.

graph.facebook.com and FacebookConnect on iPhone: How can I remove a "like"

I'm developing an iPhone application using the Facebook Connect API. I arrived to set the "Like" on a post using the following code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/likes", discussion.postId]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:[self.facebook accessToken] forKey:#"access_token"];
[request setDelegate:self];
[request startAsynchronous];
It generates the following call:
HTTP POST https://graph.facebook.com/147512198609691_148877991806445/likes
But I'm not able to do the inverse operation "Unlike" (removing the like
from the post).
I tried the following:
HTTP POST https://graph.facebook.com/147512198609691_148877991806445/likes?access_token=....&method=delete
and also:
HTTP DELETE https://graph.facebook.com/147512198609691_148877991806445/likes?access_token=....
But those are not working, probably I'm using a wrong command.
[request setPostValue:#"DELETE" forKey:#"method"];

API image file upload iPhone to Ruby on Rails

I am a backend Rails developer of an API that services several iPhone clients. I'm not an iPhone dev.
I have a need to accept binary data (several image files in this case) from the client via a POST request to the API.
To get the file content (file metadata other than image type is not relevant here), what tools might be used by the iPhone developer? I've found ObjectiveResource (used by iPhone on Rails) and ASIHTTPRequest. In the pages I found for those, there's no indication of what form the uploaded file will have when the controller action is executed. Will it be a Ruby File object or Tempfile object? I don't control the iPhone code development, there are some cross-cultural communication difficulties there, and they haven't used those suggestions so far. If I can submit better information to them, I might be getting better data back.
The backend app is currently running Rails 2.3.10, and will soon (in the next few weeks) likely be converted into Rails 3.
Thanks,
Craig
ObjectiveResource does not natively support file uploads. Try instead using ASIHTTPRequest with this snippet:
NSURL *url = [NSURL URLWithString:#"http://localhost:3000/file"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"Sample" forKey:#"name"];
[request setFile:... forKey:#"file"];
[request startSynchronous];
For more details, see the example page here (sending data).
The post will be encoded as a standard multipart form post (just like if it came from an HTML form). If you are using paperclip to store your uploads, the magic will just happen!
Use JSON over HTTP
NSMutableURLRequest *request =
[[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:urlStr]];
[request setHTTPMethod: #"POST"];
[request setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:#"%d", [jsonMessageStr length]];
[request setValue:requestDataLengthString forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPBody:jsonData];
NSURLConnection *theConnection =
[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

Can I log in to my site from my iPhone app?

I'm trying to make a log in or sign up feature for my web site in my iPhone app. My website is a content management system, and like any other CMS, it has log in and registration features. It also has permmissions, dependent on the user account. I think I would have to use UIWebView for this.
Are there any examples or tutorials I can examine?
Check out the documentation for NSURLRequest (and NSMutableURLRequest): you can use it to make a POST request to your login and registration pages, just like a web browser. You can write the form UI in Cocoa/Objective-C and then send the data to the server.
As far as displaying the result to the user, you'll have to figure out a way to either parse the returned HTML (bad idea) or modify your CMS to return JSON or XML to iPhone requests (better idea).
Edit: Here's some sample code, taken from an app I'm working on (it submits data to Last.fm using POST):
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *str = #"This is my example data!";
// everything below here is directly from my app:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:kLastFMClientUserAgent forHTTPHeaderField:#"User-Agent"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPShouldHandleCookies:NO];
*connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];