3

I am developing an app that target at minimum iOS4 using XCode 4.2. Never done it before, and this is my first month developing iPhone app.

I did some research and came accross ASIHTTPRequest - which is not maintained and even the developer suggested to use something else: http://allseeing-i.com/%5Brequest_release%5D;

From that list, I thought why not use NSUrlConnection since it's built in XCode. I know RESTKit seems quite popular out there, but I have heard it's bit cumbersome to setup - and I don't need anything fancy here, just connection to REST API services that returns JSON, so I have a feeling that NSURLConnection is more than enough.

Not quite sure how to do it, especially because I am targetting iOS4 and iOS5, and from what I understands iOS 5 SDK introduces NSURLConnectionDelegate (not sure how different they are?).

I was following this article initially https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html. But I have some doubt since it is clearly says on the top: MAC OSX Developer Library not iOS Developer Library.

Can anyone points me to the right direction? Any examples or tutorials?

abraham
  • 46,583
  • 10
  • 100
  • 152
friend
  • 1,909
  • 2
  • 21
  • 28
  • Just an additional comment but iOS 5 also induces native JSON parsing which is really helpful. – Allen Mar 06 '12 at 03:45

1 Answers1

5

I found similar article like above but for iOS Developer Library (not MAX OSX one) and they are both very similar (or could well have the same content). https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE That's what I did at the end rather than implementing NSURLConnectionDelegate.

That combines with JSONKit following this tutorial: http://www.techtraits.com/jsonkit/ does the job for me.

One word of caution: I have to turn off ARC since JSONKit does not support it at the moment.

Examples:

- (IBAction)callRest:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://www.example.com/Person/123"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];                                
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

    if(connection) {
        responseData = [[NSMutableData alloc] init];
    } else {
        NSLog(@"connection failed");
    }

}

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
    JSONDecoder *decoder = [[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone];
    NSDictionary* json = [decoder objectWithData:responseData];
    if(json != nil)
    {
        NSLog(@"First Name %@", [json objectForKey:@"FirstName"]);
        NSLog(@"Last Name %@", [json objectForKey:@"LastName"]);
    }
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [connection release];

    [responseData release];

    NSLog(@"connection error");
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connection success");
}

Also I have to declare responseData like following in .h file

@property (retain, nonatomic) NSMutableData *responseData;

And in .m file:

@synthesize responseData;
friend
  • 1,909
  • 2
  • 21
  • 28