Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're looking to place this particular method in a location that can be accessed and called from the entirety of your application then this is simply a design decision. There are a number of ways to achieve this.</p> <p>I like to make use of the singleton paradigm when implementing global methods. John Wordsworth covers this in a nice succinct blog post here:</p> <p><a href="http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/" rel="nofollow">http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/</a></p> <p>Here's a quick chunk of code:</p> <blockquote> <p>InternetConnectionChecker.h</p> </blockquote> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface InternetConnectionChecker : NSObject // Accessor for singelton instance of internet connection checker. + (InternetConnectionChecker *)sharedInternetConnectionChecker; // Check to see whether we have a connection to the internet. Returns YES or NO. - (BOOL)connected; @end </code></pre> <blockquote> <p>InternetConnectionChecker.m</p> </blockquote> <pre><code>#import "InternetConnectionChecker.h" @implementation InternetConnectionChecker // Accessor for singelton instance of internet connection checker. + (InternetConnectionChecker *)sharedInternetConnectionChecker { static InternetConnectionChecker *sharedInternetConnectionChecker; @synchronized(self) { if (!sharedInternetConnectionChecker) { sharedInternetConnectionChecker = [[InternetConnectionChecker alloc] init]; } } return sharedInternetConnectionChecker; } // Check to see whether we have a connection to the internet. Returns YES or NO. - (BOOL)connected { NSURL *scriptUrl = [NSURL URLWithString:@"http://www.google.com/m"]; NSData *data = [NSData dataWithContentsOfURL:scriptUrl]; if (data) { NSLog(@"Device is connected to the internet"); return TRUE; } else { NSLog(@"Device is not connected to the internet"); return FALSE; } } @end </code></pre> <p>In my example I've amended your method to return a true/false so you can handle the result within the UI calling the method appropriately but you could continue to show a UIAlertView if you pleased.</p> <p>You would then use the singleton in the following way:</p> <pre><code>InternetConnectionChecker *checker = [InternetConnectionChecker sharedInternetConnectionChecker]; BOOL connected = [checker connected]; </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