Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Reachability class and <code>-reachabilityWithHostname:</code> is designed to be a quick, fail-fast mechanism to determine whether you have basic network connectivity to the host. If you need to verify that a particular URL can be downloaded, you need to be looking at using <code>NSURLConnection</code> to retrieve the contents of the URL in order to verify that it is truly available.</p> <p>Depending on whether you need to do this in the foreground or background, you can either use the simple-but-blocking:</p> <pre><code>+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error </code></pre> <p>or you can use the more complicated method of creating an NSURLConnection object, setting up a delegate to receive responses and wait for those responses to come in.</p> <p>For the simple case:</p> <pre><code> NSURL *myURL = [NSURL URLWithString: @"http://example.com/service.asmx"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL]; [request setHTTPMethod: @"HEAD"]; NSURLResponse *response; NSError *error; NSData *myData = [NSURLConnection sendSynchronousRequest: request returningResponse: &amp;response error: &amp;error]; </code></pre> <p>If you receive back a non-nil myData, you've got some kind of connectivity. <code>response</code> and <code>error</code> will tell you what the server responded to you (in the case of response and if you received a non-nil myData) or what kind of error occurred, in the case of a nil myData.</p> <p>For the non-trivial case, you can get good guidance from Apple's <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html" rel="noreferrer">Using NSURLConnection</a>.</p> <p>If you don't want to stall your foreground process, you can do this two different ways. The above documentation will provide information on how to implement the delegate, etc. However, a simpler implementation would be to use GCD to send the Synchronous request on a background thread, and then message yourself on the main thread when you are done.</p> <p>Something like this:</p> <pre><code> NSURL *myURL = [NSURL URLWithString: @"http://example.com/service.asmx"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL]; [request setHTTPMethod: @"HEAD"]; dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^{ NSURLResponse *response; NSError *error; NSData *myData = [NSURLConnection sendSynchronousRequest: request returningResponse: &amp;response error: &amp;error]; BOOL reachable; if (myData) { // we are probably reachable, check the response reachable=YES; } else { // we are probably not reachable, check the error: reachable=NO; } // now call ourselves back on the main thread dispatch_async( dispatch_get_main_queue(), ^{ [self setReachability: reachable]; }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload