Check if NSURL returns 404

后端 未结 4 1662
孤独总比滥情好
孤独总比滥情好 2020-12-02 17:51

I need to check whether a URL (represented by a NSURL) is available or returns 404. What is the best way to achieve that?

I would prefer a way to check this without

4条回答
  •  清歌不尽
    2020-12-02 18:00

    You can achieve a synchronous connection by calling:

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSHTTPURLResponse* response = nil;
    NSError* error = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    

    Your thread will block until the request has beeen made.

提交回复
热议问题