ASIHTTPRequest Integrate Twitter - iphone

I need your help. I have been switching from various libraries trying to find the suitable one to integrate Twitter in my application.
A while back i found out that i could use ASIHTTPRequest to send tweets from my app. I need to know if there's any tutorial or sample code available.
And i have already looked at share-kit and i prefer to integrate Twitter using ASIHTTPRequest
note: I have created my project using ARC, and it shoudl support both iOS4 and 5.

Twitter's documentation says that you have to make GET or POST HTTP requests with the appropriate form data (key-value pairs). For example, to post a status update, you can do:
NSURL *url = [NSURL URLWithString:#"http://api.twitter.com/1/statuses/update.json"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:#"Accept" value:#"*/*"];
[request addRequestHeader:#"onnection" value:#"close"];
[request addRequestHeader:#"Content-Type" value:#"application/x-www-form-urlencoded"];
[request addRequestHeader:#"Content-Length" value:#"17"];
[request addRequestHeader:#"Host" value:#"api.twitter.com"];
[request setPostValue:#"User's new status" forKey:#"status"];
// set here completion/error handlers
[request startAsynchronous];
More documentation on Twitter's REST/HTTP API: https://dev.twitter.com/docs
For authetication, you'll need some kind of OAuth library. Here's Karl Adam's MPOAuth, with few additions and corrections by me: https://github.com/H2CO3/MPOAuthiOS
On iOS 5.x, you can use Apple's default, and easy to use Twitter.framework: http://developer.apple.com/library/ios/#documentation/Twitter/Reference/TwitterFrameworkReference/_index.html

Related

Changes coming in Version 1.1 of the Twitter API in iPhone

I am using twitter API version V1.0 for my new iphone application .I am successfully able to do this using twitters v1.0 of the API and all works perfectly. Simply making a request to http://api.twitter.com/1/statuses/user_timeline.json?screen_name=userid retrieves all the information that I require.
since v1.0 has been deprecated and V1.1 requires authentication for each request, I get a bad authorization error (HTTP response status: 400)using this API.
What are the Changes i need to do in my appication .how to generate OAuth request headers,Do i need register my application ?How can i get authentication for new version?
I hope the above makes sense and any help would be appreciated.
Thanks in advance.
Visit https://dev.twitter.com/docs/ios/making-api-requests-slrequest
https://dev.twitter.com/docs/oauth/xauth
u can get the authentication using following method:
NSString *base64 = #"Basic NFRLWGlZY3l1aHJ4OVJaUWI5RW5BOmdVMXlvcVV6YzBNZUhUQmFXdVRZU2NtZHlIWDFOZXhwZmxqRE16bm01aw==";
//oauth_consumer_secret,oauth_consumer_key
NSURL *urlAPI = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.twitter.com/oauth2/token"]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlAPI];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Authorization" value:base64];
[request addRequestHeader:#"Content-Type" value:#"application/x-www-form-urlencoded;charset=UTF-8"];
[request setPostValue:#"client_credentials" forKey:#"grant_type"];
In response u will get the authtoken for your application like this
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMddRQAAAAAAXRP6Axur1RS%2Fv9YFtJ7TijyPAGo%3DpmNowwOGf3dZTHDiH2gnCcv7qNIyGZcyV2IW5YFTBs"}

How to use post manner to transmit username and password in order to log in a website on iphone or ipad platform?

How to use post manner to transmit username and password in order to log in a website on iphone or ipad platform?
Some one has suggest me that use ASIHTTPRequest,but I don't know how to use it.
Can somebody help me ?Thank you ........
ASIHTTPRequest has one of the best how to use pages of any library I have ever encountered. It is located here: http://allseeing-i.com/ASIHTTPRequest/How-to-use
If you need to post to a web form you could do something like this:
#define kURLString #"https://yourwebsite.com"
NSURL *url = [NSURL urlWithString:kURLString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"myname" forKey:#"username"];
[request setPostValue:#"l33td00d" forKey:#"password"];
[request setDelegate:self];
[request startSynchronous];
Although in real life you may wish to run the request asynchronous. The details on how to do that are on the page I linked. It is defiantly worth reading.

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

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

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