2012-05-08 81 views
0

我关闭了我的网络并运行我的ios程序。 然后停留程序。使用NSURLConnection连接失败,如何重新连接?

当我打开我的网络后。我的程序仍然停止。

我想在ios中重新连接NSURLConnection。

-(void) applistDBdownload 
{ 
NSString *file = [NSString stringWithFormat:@"http://sok129.cafxxx.com/oneapplist.sqlite3"]; 
NSURL *fileURL = [NSURL URLWithString:file]; 

NSURLRequest *req = [NSURLRequest requestWithURL:fileURL]; 
connect= [NSURLConnection connectionWithRequest:req delegate:self]; 

fileData = [[NSMutableData alloc] initWithLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
NSLog(@"fail"); -> come in when network off. 
        -> no come in when network on 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
      -> no come in when network on 
      -> come in when network off 
} 

1.如何重新连接?

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
if(self.connect!=connection) 
{ 
    NSLog(@"noconnect"); 
    return; 
} 


[self.fileData appendData:data]; 
} 
    在此代码区
  1. ,如果网络关闭,我怎么处理这个代码?

回答

0

您通常应该告诉用户出了点问题(例如使用UIAlertView)。您可以要求用户尝试再次连接。您也可以对用户进行“透明”处理,并在用户不知道发生了什么的情况下重试。请再次致电applistDBdownload。虽然,我认为你应该尝试创建一些最适合你需要的逻辑(如果你知道你的网络不断下降,请在发出警告之前尝试3到4次)。最后,您可以尝试在服务器端进行更改(可能会收到较小的包,因此在您接收数据时网络的百分比会更小)。

0

当你的网络是反过来的, - (无效)连接:(NSURLConnection的*)连接didFailWithError:(NSError *)错误 会习惯这里称为U可以添加以下行请求阿恩 [mConnection开始]; 当你开始连接时,如果你打开网络,你可以得到正确的连接。

0

您可以使用处理重试HTTP请求的Apple MVCNetworkingMVCNetworking

下一个是从苹果公司获得的一种:MVCNetworking derived

这种方式意味着你首先应更多的代码创建请求时,但请求失败时,它将被赋予重尝试给定的时间间隔。