我有这样的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];
});
当我连接到输入时工作,但是当我关闭我的互联网时,我收到此错误并且没有出现其他警报
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
答案 0 :(得分:0)
您可以使用Apple提供的示例代码来测试可达性:
http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html
使用所需的主机名初始化可达性,然后您可以:
启动/停止通知程序,您将收到通知,指示此主机的可访问性何时更改
或只是获取当前的可访问性状态
Reachability *reachability = [Reachability reachabilityForInternetConnection]; NetworkStatus internetStatus = [reachability currentReachabilityStatus]; if (internetStatus == NotReachable) { // You have internet } else { // You do not have internet }
答案 1 :(得分:0)
我喜欢做的一件事是检查与其他NSURL的连接并更改BOOL以进行连接。我做了这样的方法..
- (void)checkConnection
{
NSURL *checkURL = [NSURL URLWithString:@"http://www.apple.com"];
NSData *data = [NSData dataWithContentsOfURL:checkURL];
if (data)
{
_connected = YES;
}
else
{
_connected = NO;
}
}
现在我有一个BOOL _connected让我知道我是否已连接。然后在你需要确保你连接的任何方法中首先做这样的事情..
- (void)viewWillAppear:(BOOL)animated
{
[self checkConnection];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if (_connected == YES)
{
[self fetchTweets];
[self reFresh];
[self.tableView reloadData];
}else
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet"
message:@"You must have an internet connection."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
[self.refreshControl endRefreshing];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
return;
}
}
这就是我为我编写的一些应用程序所做的。它很快,你不需要大量的代码或进口。或者您可以查看NSURLConnection和NSURLRequest。它们结合使用的方法可以让您显示警告,如果没有数据则显示警告。这正是Rob所说的。只是一个想法!
答案 2 :(得分:0)
Tony Million's Apple的Reachability代码是一个很好的基于块的解决方案来解决这个问题: https://github.com/tonymillion/Reachability
// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];
// set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
NSLog(@"REACHABLE!");
};
reach.unreachableBlock = ^(Reachability*reach)
{
NSLog(@"UNREACHABLE!");
};
// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];