How to send Asynchronous URL Request?

后端 未结 5 2002
孤独总比滥情好
孤独总比滥情好 2020-11-27 17:30

I would like to know how do I get a return value 1 or 0 only.... back from an URL request asynchronously.

currently I do it in this way:

NSString *UT         


        
5条回答
  •  隐瞒了意图╮
    2020-11-27 17:44

    try this :

    .h:

    NSMutableData *responseData;
    

    .m:

    - (void)load 
    {
      NSURL *myURL = [NSURL URLWithString:@"http://www.example.com"];
      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];
    }
    

提交回复
热议问题