root/apps/iphone/superbook/trunk/DotTel_SDK/Classes/NetworkUtility.m
| Revision 893, 3.9 kB (checked in by henri, 21 months ago) |
|---|
| 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 | |
| 31 | static NetworkUtility *sharedNetworkUtility = nil; |
| 32 | |
| 33 | @implementation NetworkUtility |
| 34 | |
| 35 | @synthesize internetConnectionStatus; |
| 36 | |
| 37 | #pragma mark ------ init |
| 38 | |
| 39 | - (id) init { |
| 40 | self = [super init]; |
| 41 | networkActivityCounter = 0; |
| 42 | return self; |
| 43 | } |
| 44 | |
| 45 | - (void) dealloc { |
| 46 | [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 47 | [super dealloc]; |
| 48 | } |
| 49 | |
| 50 | #pragma mark ------ network methods |
| 51 | |
| 52 | - (void)networkwillActivate:(BOOL)activate { |
| 53 | if (activate) { |
| 54 | networkActivityCounter++; |
| 55 | } else { |
| 56 | networkActivityCounter--; |
| 57 | } |
| 58 | //NSLog(@"Network activity counter: %d", networkActivityCounter); |
| 59 | [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:(networkActivityCounter > 0)]; |
| 60 | } |
| 61 | |
| 62 | - (NetworkStatus)startNetwork { |
| 63 | // Hack to force start the networking (wifi or edge/3G) |
| 64 | NSMutableURLRequest *urlReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]; |
| 65 | [urlReq setHTTPMethod:@"HEAD"]; |
| 66 | [self networkwillActivate:YES]; |
| 67 | NSURLConnection *theConnection=[[[NSURLConnection alloc] initWithRequest:urlReq delegate:self startImmediately:YES] autorelease]; |
| 68 | [self networkwillActivate:NO]; |
| 69 | if (!theConnection) { |
| 70 | return NotReachable; |
| 71 | } |
| 72 | |
| 73 | [self updateStatus]; |
| 74 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 75 | selector:@selector(reachabilityChanged:) |
| 76 | name:@"kNetworkReachabilityChangedNotification" |
| 77 | object:nil]; |
| 78 | return self.internetConnectionStatus; |
| 79 | } |
| 80 | |
| 81 | - (void)reachabilityChanged:(NSNotification *)note { |
| 82 | [self updateStatus]; |
| 83 | } |
| 84 | |
| 85 | - (void)updateStatus { |
| 86 | self.internetConnectionStatus = [[Reachability reachabilityForInternetConnection] currentReachabilityStatus]; |
| 87 | //NSLog(@"Reachability changed, values are: %d - %d", remoteHostStatus, internetConnectionStatus); |
| 88 | } |
| 89 | |
| 90 | #pragma mark ---- singleton object methods ---- |
| 91 | |
| 92 | // See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info |
| 93 | |
| 94 | + (NetworkUtility *)sharedInstance { |
| 95 | if (sharedNetworkUtility == nil) { |
| 96 | sharedNetworkUtility = [[super allocWithZone:NULL] init]; |
| 97 | } |
| 98 | return sharedNetworkUtility; |
| 99 | } |
| 100 | |
| 101 | + (id)allocWithZone:(NSZone *)zone { |
| 102 | return [[self sharedInstance] retain]; |
| 103 | } |
| 104 | |
| 105 | - (id)copyWithZone:(NSZone *)zone |
| 106 | { |
| 107 | return self; |
| 108 | } |
| 109 | |
| 110 | - (id)retain { |
| 111 | return self; |
| 112 | } |
| 113 | |
| 114 | - (unsigned)retainCount { |
| 115 | return NSUIntegerMax; // denotes an object that cannot be released |
| 116 | } |
| 117 | |
| 118 | - (oneway void)release { |
| 119 | // do nothing |
| 120 | } |
| 121 | |
| 122 | - (id)autorelease { |
| 123 | return self; |
| 124 | } |
| 125 | |
| 126 | @end |
Note: See TracBrowser
for help on using the browser.








