Adding TwitPic to your Application

  • Twitter
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
February 23rd, 2010 Posted by: (ELC) - posted under:Snippets

Introduction

Back in July of last year. Brandon put up a post showing you how to integrate twitter into your application. Today I am going to take the class he made last year and add a new class which will let you post to twit pic. First lets do a little overview of what tools we need for this.

Required Tools

Twit pic is an awesome service. They have created a whole public API, that anyone can use to host a picture and post a tweet with a link to it. You can see the TwitPic API, and all its functionality here.The API is pretty simple, with only 2 methods.

  • uploadAndPost
  • upload

We are going to only be implement access to the uploadAndPost method. In order to use the API we need to use an HTTP POST method. While Apple provides NSURLConnection to take care of operations like this, we are going to use a better third party framework called ASIHTTPRequest. You can find ASIHTTPRequest to download here. I will go over the steps to get it installed. Just download the files for right now. You will also need to download some utility files that Apple created for users called Reachability. You can find those files here.

Preparing a project to use ASIHTTPRequest

  1. Before we add the method into out TwitterRequest class, we have to do a bit of preparation with a project we want to use this framework witb. First think to do is to add ASIHTTPRequest and the Reachability classes into your application.
  2. Now we have to add some frameworks to out project by “Editing the active target”. Go to Project ->  Edit Active Target “TwitPic”
  3. Add in the following targets: CFNetwork.framework, SystemConfiguration.framework and libz.1.2.3.dylib

Using ASIHTTPRequest to contact TwitPic

Now if we compile we should see no errors, and we will be able to use ASIHTTPRequest in our new method in out TwitterRequest class. The method to communicate with TwitPic is actually going to be very short. We need to create the method to send the picture and fill in the methods to handle the response. Lets take a look at what these methods look like.

Method to send picture to TwitPic

Here we are going to pass in the photo for twit pic along with the delegate that is using out class. This pertains back to the design decision made when developing the original TwitterRequest class. Look back at the first post for expansion on this. Here we create an instance of an ASIFormDataRequest. request is an instance variable I declared in the TwitterRequest header. Pass in the proper values for the proper keys, following along with the TwitPic API. We are going to start and Asynchronous request here, so the UI does not freeze while the picture is being uploaded.

-(void)statuses_update:(NSString *)status withPhoto:(NSData*)photoData delegate:(id)requestDelegate requestSelector:(SEL)requestSelector {
     isPost = YES;
     // Set the delegate and selector
     self.delegate = requestDelegate;
     self.callback = requestSelector;
     // The URL of the Twitter Request we intend to send
     NSURL *url = [NSURL URLWithString:@"http://twitpic.com/api/uploadAndPost"];
     // Now, set up the post data:
     request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
     [request setDelegate:self];
     [request setData:photoData forKey:@"media"];
     [request setPostValue:username forKey:@"username"];
     [request setPostValue:password forKey:@"password"];
     [request setPostValue:status forKey:@"message"];
     // Initiate the WebService request
     [request startAsynchronous];
}

Methods to handle response

We need to handle two types of response from TwitPic. Either the request will finish, or the request will error. If the request finishes the following will be called. We check that the delegate set for the TwitterRequest class is present and that is responds to the selector that was passed in. If it does, the TwitterRequest class will respond back to the class using it.

- (void)requestFinished:(ASIHTTPRequest *)request {
NSLog(@"%@", [request responseString]);
// do something with the data
     if(delegate && callback) {
          if([delegate respondsToSelector:self.callback]) {
               [delegate performSelector:self.callback withObject:receivedData];
          } else {
               NSLog(@"No response from delegate");
          }
     }
// release the connection, and the data object
     [request release];
}

This is the class that should be used to handle errors. This for example could display a UIAlertView saying that an error occurred.

- (void)requestFailed:(ASIHTTPRequest *)request {
     NSError *error = [request error];
}

You can download the Header and Main for the updated TwitterRequest class here.

  • http://maniacdev.com/2010/02/2-approaches-to-twitpic-integration/ 2 Approaches To Twitpic Integration | iPhone and iPad Development Tutorials and Programming Tips

    [...] first tutorial can be found here and the second can be found [...]

  • athanhcong

    Hi,
    When connection failed, and we don’t release the request, does the request object cause memory leak?

  • http://www.appstoremod.com Dewan Payroda

    Is the ASIHTTPRequest part of one of the frameworks we added or I need to find the classes? Because I am getting errors.

  • http://www.appstoremod.com Dewan Payroda

    Nevermind, I got it. I had to fix some stuff but it works now! This is awesome!

  • Jack

    I have integrated this into my app so that a user can input their name and password and it sends an image off to Twitpic. However, if the user inputs their name or password incorrectly, how can i check this? At the moment it takes their name and password and uses that in the request, then my app will just continue as if it has uploaded even if the details are wrong. How can I implement user authentication?

  • keith

    hi

    Is this the entire tutorial? i can get everything to work apart from i have no idea how to select the actual image. i can only do text status updates at mo. any help much appreciated.

  • MeHim

    thanks for this great tutorial but please make it complete by showing the new PostTweet method.

  • http://www.tftus.com Surendra

    Hi Dewan,

    Are you working in Payroda Technology.

    Thanks,
    Surendra

  • imweh

    After adding in all the files and frameworks, I did a test compile and received the following four errors.

    “_kUTTagClassFilenameExtension”, referenced from:
    _kUTTagClassFilenameExtension$non_lazy_ptr in ASIHTTPRequest.o

    “_UTTypeCreatePreferredIdentifierForTag”, referenced from:
    +[ASIHTTPRequest mimeTypeForFileAtPath:] in ASIHTTPRequest.o

    “_kUTTagClassMIMEType”, referenced from:
    _kUTTagClassMIMEType$non_lazy_ptr in ASIHTTPRequest.o

    “_UTTypeCopyPreferredTagWithClass”, referenced from:
    +[ASIHTTPRequest mimeTypeForFileAtPath:] in ASIHTTPRequest.o

    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    What did I miss?

  • imweh

    Nevermind, I needed to add the mobileCoreServices framework as well. After that it compiled.

  • david

    The request error answer is always empty for me. Any hints on that?

  • Bob McG

    Excellent article – well done!

  • http://www.tagcaving.org/ lasixonlin

    Aloha! lasix online

  • mike

    so is there any resources our there using twitpic with your latest tutorial on mcgtwitterengine

  • http://www.freejack.fr Freejack

    I had the same problem, now it works with the MobileCoreServices framework, thanks for the solution !

  • http://www.joycesalesgroup.com Kathey Schwanz

    This is a very fascinating post, I was looking for this knowledge. Just so you know I discovered your blog page when I was looking around for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.

  • Dandymike

    Does this still work?

  • http://twitter.com/Gurpartap Gurpartap Singh

    Just try GSTwitPicEngine if you want to deliver quick. http://github.com/Gurpartap/GSTwitPicEngine

  • Badescuga

    i’ve tried this method and it didn’t seem to work :-? tells me a lot of data isn’t there(like 60 and something errors )

  • rptwsthi

    @gurupratap : You explain entair process from scratch for tho please, some where in your post

blog comments powered by Disqus