【发布时间】:2013-07-12 19:00:11
【问题描述】:
我有一个这样的 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];
});
当我连接到 enter 时可以工作,但是当我关闭我的互联网时,我收到此错误并且不会出现 else 警报
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
【问题讨论】:
-
是什么导致您的应用程序崩溃?不应该是这样,事件是你没有互联网。
-
好吧,它并没有真正崩溃,我只是在寻找测试 NSURL 的连接,如果它失败则显示警报消息。
-
您应该只创建连接并正确处理错误(例如检查返回值、处理
didFailWithError等)。我认为写一个didFailWithError会为你做的。 -
我尝试了
didFailWithError并且弹出了这条消息Thread 6:signal SIGABRT我需要在某处致电connection吗?
标签: iphone