Ios and http requests

You can either use NSURLRequest or NSMutableURLRequest
the difference is that with NSMutableURLRequest you can change the url the method etc.

I will use the NSMutableURLRequest

Supposing you have a class implementation


#import "UrlFetcher.h"

@implementation UrlFetcher

-(void)setUrl:(NSString *)url
{
    twitterUrl = [NSURL URLWithString:url];
}

-(void)fetchandParse
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:twitterUrl];
    [request setHTTPMethod:@"GET"];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];    
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"got data");
    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(result);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
}
@end

Since you set as the delegate the class where the method is called you need to implement the
methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s