8

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];
    }

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?

Thanks,

Jack

1

2 Answers 2

18

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
}

or

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

Sign up to request clarification or add additional context in comments.

Comments

-2

Here is simplest way to check if URL is valid:

NSURL *URL = [NSURL URLWithString:@"www.your.unvalidated.yet/url"];

if ( [[UIApplication sharedApplication] canOpenURL:[URL absoluteURL]] )
{
     //your link is ok
}

4 Comments

This will not check if the url is valid though. I could put "www.myurlofjustice.com" and doing it this way would return TRUE because it in theory it still opens a link. You need to be checking the HTTP Status response like in PravatMaskeys answer.
I just tested the url I provided and if you do [[UIApplication sharedApplication] openURL:@"www.myurlofjustice"]; it actual took me to google and entered that as the search parameters. So how can you handle this?
@skywinder This is because you haven't provided a scheme for your URL. The scheme is the "http" part of a URL which you've left out. Also, something like http://www.your.unvalidated.yet/url is actually valid (assuming there's a top level domain named "yet"). Either way this is not a valid check if a URL "exists" as the OP asked. It only checks if an application exists on the system that can actually open the URL provided.
@Popeye, mattyohe Ok. It's my fault. Sorry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.