2009-09-08

How to make a web connection in iPhone application

- (void) buttonSYNC:(id) sender
{
    urlTemp=@"http://ruchirac.blogspot.com'";
    NSString *url=[urlTemp retain];
    NSURL *webserverURL = [NSURL URLWithString:url];
   
    // Create the request
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:webserverURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
   
    // Set the HTTP method to GET
    [theRequest setHTTPMethod:@"GET"];
    [theRequest setHTTPBody:@""];
   
    // Form parameters
    //data1=@"rtert";
    //[NSString stringWithFormat:@"Credentials=%@", [data1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
   
    recievedData = [[NSMutableData alloc] initWithLength:0];
   
    [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
   
    tmp = [[NSString alloc] initWithData:recievedData encoding:NSUTF8StringEncoding];      
   
    NSLog(@"22222=%@", tmp);
       
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the new data to the received data
    [self.recievedData appendData:data];
}

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

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    //[self.delegate responseRecieved:contentType :recievedData ];
    tmp =[[NSString alloc] initWithData:recievedData encoding:NSUTF8StringEncoding];
   
    //write your code here 
 
    NSLog(@"22222=%@", tmp);

   // Release the connection after finish loading
    [connection release];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    self.contentType = [response MIMEType];
}

No comments: