This is part of an ELC Tech Network

Adding TwitPic to your Application

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.

Advertisement

-(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.

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

11 Comments

  1. athanhcong
    Posted March 3, 2010 at 10:09 pm | Permalink

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

  2. Posted March 11, 2010 at 10:49 pm | Permalink

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

    • Posted March 12, 2010 at 10:37 am | Permalink

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

      • Posted June 7, 2010 at 4:55 am | Permalink

        Hi Dewan,

        Are you working in Payroda Technology.

        Thanks,
        Surendra

  3. Jack
    Posted March 29, 2010 at 2:10 am | Permalink

    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?

  4. keith
    Posted April 16, 2010 at 6:34 pm | Permalink

    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.

  5. MeHim
    Posted May 23, 2010 at 5:41 am | Permalink

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

  6. imweh
    Posted June 11, 2010 at 11:23 am | Permalink

    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
      Posted June 11, 2010 at 12:02 pm | Permalink

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

  7. david
    Posted July 3, 2010 at 9:22 am | Permalink

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

  8. Bob McG
    Posted July 28, 2010 at 8:12 am | Permalink

    Excellent article – well done!

One Trackback

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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">