0
我正在開發一個iPhone應用程序,其中我通過網絡服務從另一個服務器加載大量數據。在iPhone中設置網絡超時和異常
我想在蘋果指南的某個地方閱讀,對於網絡感知應用程序,您必須設置網絡超時時間,然後您已提醒用戶「網絡不可用」。
我該怎麼做?
我正在開發一個iPhone應用程序,其中我通過網絡服務從另一個服務器加載大量數據。在iPhone中設置網絡超時和異常
我想在蘋果指南的某個地方閱讀,對於網絡感知應用程序,您必須設置網絡超時時間,然後您已提醒用戶「網絡不可用」。
我該怎麼做?
以下是調用Web服務的示例代碼,其中設置了request-timeout = 20。如果它在20時間內沒有響應,那麼它將停止連接,並且我們得到一個零數據。
NSString* str = [NSString stringWithFormat:@"http://ws.geonames.org/findNearbyPostalCodes?lat=%f&lng=%f",curr_latitude,curr_longitude];
NSMutableURLRequest* request2=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
[request2 setHTTPMethod:@"POST"];
[request2 setTimeoutInterval:20];
NSURLResponse *response=nil;
NSError *err=nil;
NSData *data1=[[NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&err] retain];
if(data1 == nil)
{
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// It will store all data to data1
// Here you can proceed with data1
}