0

I'm using the following implementation to check if there's s internet connection avaliable, and it's working fine, but because I'm gonna check for a internet connection a lot I think it's best for (BOOL)reachable method to be avaliable everywhere without rewriting it everytime. Because I'm new to iOS development I'm not sure how to do that. What's the best way to do that?

//
// SigninViewController.m
//

#import "Reachability.h"
#import "SigninViewController.h"

@implementation SigninViewController

...

- (IBAction)SigninTouchUpInside:(id)sender
{

    if ([self reachable])
    {
        NSLog(@"Reachable");
    }
    else
    {
        NSLog(@"Not Reachable");
    }

}

- (BOOL)reachable {
    Reachability *reachability = [Reachability reachabilityWithHostName:@"enbr.co.cc"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    if(internetStatus == NotReachable) {
        return NO;
    }
    return YES;
}

@end
4

3 に答える 3

3

Make it a plain C function:

BOOL reachable()
{
    // implementation here
}

Declare it in a separate header file and implement it independently from any other class, so it can be used from anywhere.

于 2012-08-31T21:13:46.630 に答える
2

You could make it a method of your app delegate class. Since the method doesn't need to access any class properties, it could be a class method (declare with "+") rather than an instance method (declare with "-").

In yourAppDelegate.h:

+ (BOOL)reachable;

In yourAppDelegate.m:

+ (BOOL)reachable {
    Reachability *reachability = [Reachability reachabilityWithHostName:@"enbr.co.cc"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    return (internetStatus == NotReachable);
}

To call the method:

#import "yourAppDelegate.h"
...
BOOL reachable = [YourAppDelegate reachable];
于 2012-08-31T21:13:05.313 に答える
2

The Singleton Design Pattern is exactly for that kind of usage

The best way to implement a singleton is by creating a class that inherits NSObject and declaring a sharedInstance class method that will return a unique instance of this class thanks to the GCD dispatch_once function (so that calling sharedInstance will allocate the object only the first time, returning always the same object/instance on further calls)

@interface ReachabilityService : NSObject
+ (id)sharedInstance;
@property(nonatomic, readonly) BOOL networkIsReachable;
@end


@implementation ReachabilityService
+ (id)sharedInstance
{
    static dispatch_once_t pred;
    static ReachabilityService *sharedInstance = nil;

    dispatch_once(&pred, ^{ sharedInstance = [[self alloc] init]; });
    return sharedInstance;
}
- (BOOL)networkIsReachable
{
    Reachability *reachability = [Reachability reachabilityWithHostName:@"enbr.co.cc"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    return (internetStatus != NotReachable);
}
@end

Another way to do it is to declare your method as a class method directly, as it won't use any instance variable or property. That is declaring directly + (BOOL)networkIsReachable instead of - (BOOL)networkIsReachable, and avoid the use of the Singleton Pattern and the sharedInstance method itself.

@interface ReachabilityService : NSObject
+ (BOOL)networkIsReachable;
@end

@implementation ReachabilityService
+ (BOOL)networkIsReachable
{
    Reachability *reachability = [Reachability reachabilityWithHostName:@"enbr.co.cc"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    return (internetStatus != NotReachable);
}
@end
于 2012-08-31T21:13:22.147 に答える