Objective-C - 检查 URL 是否存在 [英] Objective-C - Checking if URL exists

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

问题描述

我有一个当前循环 4 次的 for 循环.

I have a for-loop that currently loops 4 times.

//Add all the URLs from the server to the array
    for (int i = 1; i <= 5; i++){
        NSString *tempString = [[NSString alloc] initWithFormat : @"http://photostiubhart.comoj.com/GalleryImages/%dstiubhart1.jpg", i];
        [myURLS addObject: [NSURL URLWithString:tempString]];
        [tempString release];
    }

正如您所看到的,URL 中有一个数字,每个循环都会增加 1,以创建一个新的 URL,下一个图像将在其中.但是,服务器上的图像数量不一定是 4,它可能更多甚至更少.我的问题是,有没有办法检查 URL 中是否存储了图像?如果没有,则中断循环并继续执行程序?

As you can see the URL has a digit in it that get incremented by 1 each loop to create a new URL where the next image will be. However, the amount of images on the server won't necessarily be 4, it could be a lot more or even less. My problem is this, is there a way I can check if there is an image stored at the URL? And if there is not, break the loop and continue program execution?

谢谢,

杰克

推荐答案

这里是检查 HTTP 响应的想法

Here is idea to check with HTTP response

NSURLRequest *request;
NSURLResponse *response = nil;
NSError **error=nil; 
NSData *data=[[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]];
NSString* retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// you can use retVal , ignore if you don't need.
NSInteger httpStatus = [((NSHTTPURLResponse *)response) statusCode];
NSLog(@"responsecode:%d", httpStatus);
// there will be various HTTP response code (status)
// you might concern with 404
if(httpStatus == 404)
{
   // do your job
}

while(httpStatus == 200){
    static int increment = 0;
    increment++;
    // check other URL yoururl/somthing/increment++
}

但它会很慢.我的建议是,如果您使用自己的网络服务器,那么您可以最初发送所有图像信息.我确定你是在匿名网站或其他网站上做这项工作的 :)让我知道它是否对您有帮助

but it will be slow. what my suggestion is, if you are using your own webserver, then, you can send all the image information initially. I'm sure you are doing this job on annonymous or other website :) Let me know if it helps you or not

这篇关于Objective-C - 检查 URL 是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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