2011-11-18 116 views
0

当我连接到测试timout的端口号时,我的应用程序冻结。 我打电话给[request setTimeoutInterval:10];,我认为这应该是10秒钟。但是,该应用程序挂起。它可能与它是一个本地服务器有关吗?iPhone [请求setTimeoutInterval:10]不工作,应用程序冻结

代码:

// call this when program first starts 
-(void) nSendMessage : (NSString *) name Password: (NSString *) password page: (NSString *) page 
{ 

    // set the url 
    NSString *address = @"http://localhost:1075/update"; 
    address=[ address stringByAppendingString: page]; 

    NSMutableURLRequest *request = 
    [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:address]]; 

    // post or get 
    [request setHTTPMethod:@"POST"]; 

    // data to send 
    NSString *postString = @"username=iusername&password=ipassword"; 
    NSString *sendString=[postString stringByReplacingOccurrencesOfString:@"iusername" withString: name]; 
    sendString=[sendString stringByReplacingOccurrencesOfString:@"ipassword" withString: password]; 

    [request setValue:[NSString 
         stringWithFormat:@"%d", [sendString length]] 
    forHTTPHeaderField:@"Content-length"]; 

    [request setHTTPBody:[sendString 
          dataUsingEncoding:NSUTF8StringEncoding]]; 

    [request setTimeoutInterval:10]; 
    [[NSURLConnection alloc] 
    initWithRequest:request delegate:self]; 

// 
//THE PROGRAM FREEZES HERE 
// 

    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    NSString *response = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding]; 

    // Phrase repl 
    [self nUpdateDisplay:response]; 

} 

回答

0

你的问题是这样的一个愚弄的人:

iPhone SDK: URL request not timing out

所以总结:超时下限至少为4分钟,即使你通过以较小的价值。

而且如上述链接所详述的,使用异步方法通常是最佳选择。如果你使用同步,就像你正在做的那样,并且你在主循环中,你将完全阻止你的用户界面,这将导致'挂起'的效果。