Are there complete examples that make use of all the NSURLConnection delegate methods?

前端 未结 1 771
无人及你
无人及你 2020-12-03 02:11

I have a hard time to find any examples for NSURLConnection delegate method implemenetations. The SeismicXML example from apple is incomplete. For instance, they don\'t inco

相关标签:
1条回答
  • 2020-12-03 02:42

    Here's an implementation I've been working with lately:

    .h:
        NSMutableData *responseData;
    
    .m:
        - (void)load {
            NSURL *myURL = [NSURL URLWithString:@""];
            NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                 timeoutInterval:60];
    
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
        responseData = [[NSMutableData alloc] init];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
        [responseData appendData:data];
    }
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        [responseData release];
        [connection release];
        [textView setString:@"Unable to fetch data"];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    {
        NSLog(@"Succeeded! Received %d bytes of data",[responseData
                                                       length]);
        NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];
    
    }
    
    0 讨论(0)
提交回复
热议问题