如果url存在Objective-c [英] If url exists Objective-c

查看:150
本文介绍了如果url存在Objective-c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我有一个程序需要告诉是否存在在线图像,但是我已经得到这个工作的唯一方法是将图像加载到NSData指针中,并检查指针是否存在。

   - (BOOL)存在{
NSString * filePath = @http://couleeapps.hostei.com/BottomBox.png ;
NSURL * url = [NSURL URLWithString:filePath];
NSData * imageData = [NSData dataWithContentsOfURL:url];
if(imageData){
return YES;
}
return NO;
}

这对我有用,但我的问题是我有一个非常慢连接,并且它永远需要下载图像。所以我的问题是:有没有办法检查一个图像(比如说 http:// couleeapps。 hostei.com/BottomBox.png )可用,而不必以布尔记者方式下载?



帮助非常感谢



HiGuy

解决方案

创建一个 NSURLConnection 获取url。将 NSURLRequest HTTPMethod 替换为HEADGET。在委托方法 connection:didReceiveResponse:检查 NSHTTPURLResponse statusCode $ c> 200或其他成功回应。

   - (void)queryResponseForURL:(NSURL *)inURL {
NSMutableURLRequest request = [NSMutableURLRequest requestWithURL:inURL];

[请求setHTTPMethod:@HEAD];

NSURLConnection connection = [NSURLConnection connectionWithRequest:request delegate:self];
//连接自动启动
}

- (void)连接:(NSURLConnection *)连接didReceiveResponse :( NSURLResponse *)响应{
if([(NSHTTPURLResponse *)response statusCode] == 200){
// url exists
}
}

您可以将其他状态代码视为成功,例如



HTTP协议的一部分是设置请求方法。 GET和POST是最常见的两个,但还有其他几个包括HEAD。 HEAD说发送GET发送的相同响应,但不要发送正文。在你的情况下,身体是图像数据。因此,如果HEAD成功,您可以假设GET也将以相同的方式成功,至少在查找静态资源的情况下。


Hey, I have a program that needs to tell if an online image exists, but the only way that I've gotten this to work is by loading the image in a NSData pointer and checking if the pointer exists.

- (BOOL)exists {
      NSString *filePath = @"http://couleeapps.hostei.com/BottomBox.png";
      NSURL *url = [NSURL URLWithString:filePath];
      NSData *imageData = [NSData dataWithContentsOfURL:url];
      if (imageData) {
            return YES;
      }
            return NO;
}

This has worked for me, but my problem is that I have a very slow connection, and it takes forever to download the image. So my question is: is there a way to put checking if a image (say "http://couleeapps.hostei.com/BottomBox.png") is available without having to download it in a Boolean reporter method?

Help is much appreciated

HiGuy

解决方案

Create an NSURLConnection to fetch the url. Set the HTTPMethod of the NSURLRequest to "HEAD" instead of "GET". In the delegate method connection:didReceiveResponse: check the statusCode of the NSHTTPURLResponse for 200 or other success response.

-(void) queryResponseForURL:(NSURL *)inURL {
  NSMutableURLRequest request = [NSMutableURLRequest requestWithURL:inURL];

  [request setHTTPMethod:@"HEAD"];

  NSURLConnection connection = [NSURLConnection connectionWithRequest:request delegate:self];
  // connection starts automatically
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  if ( [(NSHTTPURLResponse  *)response statusCode] == 200 ) {
    // url exists
  }
}

There could be other status codes that you would treat as success, like 301.

Part of the HTTP protocol is setting the request method. GET and POST are the two most common, but there are several others including HEAD. HEAD says send the same response you would send for GET, but do not send the body. In your case the body is the image data. So if HEAD succeeds, you can assume that GET would also succeed in the same way, at least in the case of looking up a static resource.

这篇关于如果url存在Objective-c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆