开发者

Xcode: Validate a URL before Loading it

Im having some trouble findig a way to validate a url on my app.

My intention is to load a URL and at the same time see if other webpage exist for example.

Load http://mysite.com/folder1/1.pdf

validate http://mysite.com/folder1/2.pdf

if f开发者_开发技巧older1/2.pdf exists then load it, else validate /folder2/1.pdf

so far im loading the first pdf like this in order to be able to change the pdf number and the folder:

int numpag = 1;

NSString *baseUrl =@"http://www.cronica.com.mx/iphone/pdf_iphone/";

[pdfView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[baseUrl stringByAppendingFormat:@"folder1/%d.pdf", numpag]]]];

Thanks so much in advance!


how about this:

+ (BOOL)isValidURL:(NSURL*)url
{
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSHTTPURLResponse *res = nil;
    NSError *err = nil;
    [NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&err];
    return err!=nil && [res statusCode]!=404;
}

let me know if it works for you!

(keep in mind that this is a synchronous request and should not be executed on the main thread)


I had to change the line:

return err!=nil && [res statusCode]!=404;

to

return err==nil && [res statusCode]!=404;

for the correct Bool return. The error should remain nil.


This approach is NOT correct, You should avoid Synchronous calls as they are blocking. Apple says: simply try and wait down to wait for response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜