1

我有以下代码

NSURL *url = [NSURL URLWithString:@"http://wallpaperswide.com/download/flattened_grass-wallpaper-2800x1050.jpg"];  //Line1
NSData *data = [[NSData alloc] initWithContentsOfURL:url];  //Line2
UIImage *tmpImage = [[UIImage alloc] initWithData:data];  //Line3
NSLog(@"%@",tmpImage);  //Line4

在第 2 行中,下载该 url 中的图像需要一些时间……在这之间我必须检查互联网连接……这怎么可能?

我会详细解释我的问题....

upto line1 我的 iphone 有互联网连接。现在 line2 正在执行......在下载过程中,在这个过程的中间,我失去了我的互联网连接......

现在我如何通知用户“您没有互联网连接”...

那么如何检查此过程之间的 Internet 连接。?

是否有任何委托方法...请帮助我

提前致谢.....

4

6 回答 6

2

你可以做这样的事情

Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Could not connect to the server, please check your internet connection !" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];


}
else
{

 }

}
于 2013-02-25T07:57:29.217 回答
2

我通常做的是使用NSURLConnectionDelegate. 如果连接出现错误,我将使用Reachability来查看互联网是否存在问题,或者是否是服务器错误。

于 2013-02-25T07:57:54.887 回答
1

NSURLConnection NSURLConnectionDelegate NSURLRequest *theRequest=[NSURLRequest requestWithURL:<br> [NSURL URLWithString:@“<a href="http://www.sina.com.cn/" rel="nofollow">http://www.sina .com.cn/”】
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConncetion=[[NSURLConnection alloc]
initWithRequest:theRequest delegate:self];
如果(连接)
{

receivedData=[[NSMutableData 数据] retain];
}

  • (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
于 2013-02-25T08:08:35.967 回答
1

这里可能的方法...

1) 使用 NSURLConnection 委托方法。

2) 使用 Rechability 类

于 2013-02-25T12:32:36.640 回答
1

您可以使用修改了 Tony Million 的 Reachability 类,您可以在Reachability找到

您可以使用它来定义可访问性更改时调用的块。所以你可以用它来显示一个 UIAlertView。

如果您想异步下载图像,我建议使用AFNetworking。它已经提供了向使用的 AFHttpClient 添加 ReachabilityStatusBlock 的可能性。

因此,如果将 AFNetworking.h 导入到以下位置,您可以重写代码:

AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://wallpaperswide.com"]];
[client registerHTTPOperationClass:[AFImageRequestOperation class]];

[client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    if(status == AFNetworkReachabilityStatusNotReachable){
        NSLog(@"Not reachable");
    }
}];

AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:[client requestWithMethod:@"GET" path:@"/download/flattened_grass-wallpaper-2800x1050.jpg" parameters:nil] success:^(UIImage *image) {
    NSLog(@"%@", image);
}];
[operation start];
于 2013-02-25T13:26:13.777 回答
0

最后我找到了我的问题的答案......我在这里粘贴以供将来参考

声明一个全局变量

NSMutableData *receivedData;

在 ViewDidLoad 中:

receivedData=[[NSMutableData alloc]init];
NSURLRequest *theRequest =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wallpaperswide.com/download/flattened_grass-wallpaper-2800x1050.jpg"]];


NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection)
{
}

else
{
    // Inform the user that the connection failed.
    UIAlertView *connectFailMessage = [[UIAlertView alloc] initWithTitle:@"NSURLConnection " message:@"Failed in viewDidLoad"  delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [connectFailMessage show];
}

NSURLConnection 委托方法..

#pragma mark NSURLConnection methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
[receivedData setLength:0];
 }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // inform the user
UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
[didFailWithErrorMessage show];

//inform the user
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
 UIImage *tmpImage = [[UIImage alloc] initWithData:receivedData];
  myImageView.image=tmpImage;
 NSLog(@"%@",tmpImage);
}
于 2013-02-25T12:25:51.900 回答