Wednesday, 28 December 2011

Making a connection with the Server from the app(Methods)



In Story board(Automatic reference counting)

-(IBAction)FetchData:(id)sender{
    
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://sweans.org/test-saf/rss/"]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    if (theConnection) {
        //responseData = [[NSMutableData data] retain];

        responseData = [NSMutableData data];
    } else {
        
        // Inform the user that the connection failed.
        NSLog(@"theConnection is NULL");
    }
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [responseData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [responseData appendData:data];
    
   // NSLog(@"Data from server%@",responseData);
    
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
   // [connection release];
  //  [responseData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [responseData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [responseData mutableBytes] length:[responseData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",theXML);
   // [theXML release];
}

No comments:

Post a Comment