Advertisement
arkader

Singleton - Reachability.m

Aug 29th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  CRReachability.m
  3. //  ChloeRoses
  4. //
  5. //  Created by Emal Saifi [DAN-PARIS] on 13/08/13.
  6. //  Copyright (c) 2013 Emal Saifi [DAN-PARIS]. All rights reserved.
  7. //
  8.  
  9. #import "CRReachability.h"
  10.  
  11. static CRReachability *sharedMyCRReachability= nil;
  12.  
  13. @implementation CRReachability
  14. @synthesize reach = _reach;
  15. @synthesize isConnectionReachable = _isConnectionReachable;
  16. @synthesize delegate = _delegate;
  17.  
  18. #pragma mark Singleton Methods
  19. + (id)sharedManager {
  20.     @synchronized(self) {
  21.         if(sharedMyCRReachability == nil)
  22.             sharedMyCRReachability = [[super allocWithZone:NULL] init];
  23.     }
  24.     return sharedMyCRReachability;
  25. }
  26. + (id)allocWithZone:(NSZone *)zone {
  27.     return [[self sharedManager] retain];
  28. }
  29. - (id)copyWithZone:(NSZone *)zone {
  30.     return self;
  31. }
  32. - (id)retain {
  33.     return self;
  34. }
  35. - (unsigned)retainCount {
  36.     return UINT_MAX; //denotes an object that cannot be released
  37. }
  38. - (oneway void)release {
  39.     // never release
  40. }
  41. - (id)autorelease {
  42.     return self;
  43. }
  44. - (id)init {
  45.     if (self = [super init]) {
  46.        
  47.         [self reachability];
  48.     }
  49.     return self;
  50. }
  51.  
  52. -(void)reachability{
  53.    
  54.     self.reach = [Reachability reachabilityWithHostname:@"www.google.com"];
  55.    
  56.     self.reach.reachableBlock = ^(Reachability * reachability)
  57.     {
  58.         dispatch_async(dispatch_get_main_queue(), ^{
  59.             NSLog(@"Block Says Reachable");
  60.             self.isConnectionReachable = YES;
  61.            
  62.             if([self.delegate respondsToSelector:@selector(crReachability:getConnectionReachable:)])
  63.                 [self.delegate crReachability:self getConnectionReachable:self.isConnectionReachable];
  64.         });
  65.     };
  66.    
  67.     self.reach.unreachableBlock = ^(Reachability * reachability)
  68.     {
  69.         dispatch_async(dispatch_get_main_queue(), ^{
  70.            
  71.             NSLog(@"Block Says Unreachable");
  72.             self.isConnectionReachable = NO;
  73.            
  74.             if([self.delegate respondsToSelector:@selector(crReachability:getConnectionReachable:)])
  75.                 [self.delegate crReachability:self getConnectionReachable:self.isConnectionReachable];
  76.         });
  77.     };
  78.    
  79.    
  80. }
  81.  
  82. - (void)startReachability {
  83.    
  84.     NSLog(@"startReachability");
  85.     [self.reach startNotifier];
  86. }
  87.  
  88. - (void)stopReachability {
  89.    
  90.     if( [self.reach isReachable] == YES ) {
  91.         NSLog(@"stopReachability");
  92.         [self.reach stopNotifier];
  93.     }
  94.  
  95.  
  96. }
  97.  
  98.  
  99. - (void)dealloc {
  100.     // Should never be called, but just here for clarity really.
  101.     [_reach release];
  102.     [_delegate release];
  103.     [super dealloc];
  104. }
  105.  
  106. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement