Is it wise to use Reachability to check for a remote host's availability?

前端 未结 6 1045
不知归路
不知归路 2021-01-01 05:59

Is it wise to use Reachability Class(from Apple) to check for a remote host\'s availability ? say for example, www.google.com

or should I use

NSStri         


        
6条回答
  •  臣服心动
    2021-01-01 06:36

    Here is a good way to check if host is reachable:

        NSURLResponse *response=nil;
        NSError *error=nil;
        NSData *data = nil;
        NSURLRequest *request = [NSURLRequest requestWithURL:your_url];
    
        data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    

    If host is offline you will receive some error code in error, nil in data and nil in response variables.
    If host is online and responds there will be some response, data, and error==nil.

提交回复
热议问题