我有一个像这样的 NSURL:
NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
但如果没有互联网连接或电话信号,我的应用程序就会崩溃。有没有在使用前先测试连接?
我必须尝试并抓住吗?我一直在阅读关于NSURLConnection
,但我不清楚它是如何工作的。有任何想法吗?
我得到的错误是Thread 6:signal SIGABRT
我不知道这意味着什么,我已经尝试了下面的所有示例,但没有一个有效。
我添加了这个:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
// receivedData is declared as a method instance elsewhere
[connection release];
// inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];
[didFailWithErrorMessage release];
}
我从互联网上拔下我的 Mac 并在模拟器上运行我的应用程序,这出现了Thread 6:signal SIGABRT
我在下面使用了这段代码
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"No Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
});
当我连接到输入时工作,但是当我关闭我的互联网时,我收到这个错误并且没有出现 else 警报
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'