Developer Area

root/apps/iphone/superbook/trunk/DotTel_SDK/Classes/NetworkUtility.m @ 496

Revision 363, 4.4 kB (checked in by henri, 5 years ago)

Updated copyright dates

Line 
1//
2//  NetworkUtility.m
3//  LocateThem
4//
5//  Created by Henri Asseily on 4/3/09.
6/*
7 Copyright (c) 2008-2009, Telnic Ltd. All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without modification,
10 are permitted provided that the following conditions are met:
11 
12 Redistributions of source code must retain the above copyright notice, this list of conditions
13 and the following disclaimer. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the documentation and/or other
15 materials provided with the distribution.
16 Neither the name of the Telnic Ltd. nor the names of its contributors may be used to endorse or
17 promote products derived from this software without specific prior written permission.
18 THIS DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
19 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27//
28
29#import "NetworkUtility.h"
30
31static NetworkUtility *sharedNetworkUtility = nil;
32
33@implementation NetworkUtility
34
35@synthesize remoteHostStatus;
36@synthesize internetConnectionStatus;
37
38#pragma mark ------ init
39
40- (id) init {
41        self = [super init];
42        networkActivityCounter = 0;
43        return self;
44}
45
46- (void) dealloc {     
47        [[NSNotificationCenter defaultCenter] removeObserver:self];
48        [super dealloc];
49}
50
51#pragma mark ------ network methods
52
53- (void)networkwillActivate:(BOOL)activate {
54        if (activate) {
55                networkActivityCounter++;
56        } else {
57                networkActivityCounter--;
58        }
59        //NSLog(@"Network activity counter: %d", networkActivityCounter);
60        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(networkActivityCounter > 0)];
61}
62
63- (NetworkStatus)startNetwork {
64        // Hack to force start the networking (wifi or edge/3G)
65        NSMutableURLRequest *urlReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
66        [urlReq setHTTPMethod:@"HEAD"];
67        [self networkwillActivate:YES];
68        NSURLConnection *theConnection=[[[NSURLConnection alloc] initWithRequest:urlReq delegate:self startImmediately:YES] autorelease];
69        [self networkwillActivate:NO];
70        if (!theConnection) {
71                return NotReachable;
72        }
73       
74        [[Reachability sharedReachability] setAddress:@"208.67.222.222"];
75        [self updateStatus];
76        [[NSNotificationCenter defaultCenter] addObserver:self
77                                                                                         selector:@selector(reachabilityChanged:)
78                                                                                                 name:@"kNetworkReachabilityChangedNotification"
79                                                                                           object:nil];
80        return self.remoteHostStatus;
81}
82
83- (void)reachabilityChanged:(NSNotification *)note {
84        [self updateStatus];
85}
86
87- (void)updateStatus {
88    self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
89    self.internetConnectionStatus = [[Reachability sharedReachability] internetConnectionStatus];
90        //NSLog(@"Reachability changed, values are: %d - %d", remoteHostStatus, internetConnectionStatus);
91}
92
93#pragma mark ---- singleton object methods ----
94
95// See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info
96
97+ (NetworkUtility *)sharedInstance {
98    @synchronized(self) {
99        if (sharedNetworkUtility == nil) {
100            [[self alloc] init]; // assignment not done here
101        }
102    }
103    return sharedNetworkUtility;
104}
105
106+ (id)allocWithZone:(NSZone *)zone {
107    @synchronized(self) {
108        if (sharedNetworkUtility == nil) {
109            sharedNetworkUtility = [super allocWithZone:zone];
110            return sharedNetworkUtility;  // assignment and return on first allocation
111        }
112    }
113    return nil; // on subsequent allocation attempts return nil
114}
115
116- (id)copyWithZone:(NSZone *)zone
117{
118    return self;
119}
120
121- (id)retain {
122    return self;
123}
124
125- (unsigned)retainCount {
126    return UINT_MAX;  // denotes an object that cannot be released
127}
128
129- (void)release {
130    // do nothing
131}
132
133- (id)autorelease {
134    return self;
135}
136
137@end
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us