开发者

iPhone: Using NSMutableURLRequest with NSTimer to handle connection timeout

开发者 https://www.devze.com 2023-01-07 15:22 出处:网络
\"Forced\" to use sendSynchronousRequest (POST) for our application. Checking if \"response\" is nil (below) will catch a few of the connection errors that may occur, but I\'d like to invoke a timer a

"Forced" to use sendSynchronousRequest (POST) for our application. Checking if "response" is nil (below) will catch a few of the connection errors that may occur, but I'd like to invoke a timer as well to cancel the request after about 20 s. Not sure on how to do that and how to "wait" for time out. Any suggestions? Thanks in advance.

  data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    if(response开发者_StackOverflow社区 == nil) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Connection error"
                                              delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];
        [alert show];
        [alert resignFirstResponder];
        [alert release];

    } ...   


try this:

request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
0

精彩评论

暂无评论...
验证码 换一张
取 消