Re: Checking if NSURL exists
Re: Checking if NSURL exists
- Subject: Re: Checking if NSURL exists
- From: Koen van der Drift <email@hidden>
- Date: Mon, 05 Aug 2013 11:25:46 -0400
After some more searching and trying out, I came up with the following:
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL];
[request setHTTPMethod: @"HEAD"];
NSData * result = [NSURLConnection sendSynchronousRequest: request
returningResponse: &response
error: &error];
if ([response statusCode] == 200)
{
NSLog(@"URL exists with data %@", result);
}
else
{
NSLog(@"URL does not exist");
}
Does this look ok? It seems to work for the few URLs I tried.
The goal is to dynamically create a menu that adds the url if it exists.
Thanks,
- Koen.
On Aug 5, 2013, at 10:50 AM, Koen van der Drift <email@hidden> wrote:
> How can I check if an URL on a remote server is valid and exists, without loading the website first?
>
> I tried [myURL checkResourceIsReachableAndReturnError: &error], but that always returns false, even for a very common URL such as http://www.apple.com/.
>
> That gives me @"NSCocoaErrorDomain" - code: 4 for the error.
>
> Thanks,
>
> - Koen.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden