|
| 1 | +/* |
| 2 | + |
| 3 | + File: AppDelegate.m |
| 4 | + Abstract: Main controller that houses the operation queue and |
| 5 | + initializes the LocalBonjour Controller. |
| 6 | + |
| 7 | + Version: 1.2 |
| 8 | + |
| 9 | + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. |
| 10 | + ("Apple") in consideration of your agreement to the following terms, and your |
| 11 | + use, installation, modification or redistribution of this Apple software |
| 12 | + constitutes acceptance of these terms. If you do not agree with these terms, |
| 13 | + please do not use, install, modify or redistribute this Apple software. |
| 14 | + |
| 15 | + In consideration of your agreement to abide by the following terms, and subject |
| 16 | + to these terms, Apple grants you a personal, non-exclusive license, under |
| 17 | + Apple's copyrights in this original Apple software (the "Apple Software"), to |
| 18 | + use, reproduce, modify and redistribute the Apple Software, with or without |
| 19 | + modifications, in source and/or binary forms; provided that if you redistribute |
| 20 | + the Apple Software in its entirety and without modifications, you must retain |
| 21 | + this notice and the following text and disclaimers in all such redistributions |
| 22 | + of the Apple Software. |
| 23 | + Neither the name, trademarks, service marks or logos of Apple Inc. may be used |
| 24 | + to endorse or promote products derived from the Apple Software without specific |
| 25 | + prior written permission from Apple. Except as expressly stated in this notice, |
| 26 | + no other rights or licenses, express or implied, are granted by Apple herein, |
| 27 | + including but not limited to any patent rights that may be infringed by your |
| 28 | + derivative works or by other works in which the Apple Software may be |
| 29 | + incorporated. |
| 30 | + |
| 31 | + The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
| 32 | + WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
| 33 | + WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 34 | + PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
| 35 | + COMBINATION WITH YOUR PRODUCTS. |
| 36 | + |
| 37 | + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
| 38 | + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 39 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 40 | + ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR |
| 41 | + DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF |
| 42 | + CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF |
| 43 | + APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 | + |
| 45 | + Copyright (C) 2008-2009 Apple Inc. All Rights Reserved. |
| 46 | + |
| 47 | + */ |
| 48 | + |
| 49 | +#import "AppDelegate.h" |
| 50 | +#import <SystemConfiguration/SystemConfiguration.h> |
| 51 | +#import <netinet/in.h> |
| 52 | + |
| 53 | +@implementation AppDelegate |
| 54 | + |
| 55 | +@synthesize window, navController, cryptoQueue; |
| 56 | + |
| 57 | +- (BOOL)isNetworkAvailableFlags:(SCNetworkReachabilityFlags *)outFlags { |
| 58 | + SCNetworkReachabilityRef defaultRouteReachability; |
| 59 | + struct sockaddr_in zeroAddress; |
| 60 | + |
| 61 | + bzero(&zeroAddress, sizeof(zeroAddress)); |
| 62 | + zeroAddress.sin_len = sizeof(zeroAddress); |
| 63 | + zeroAddress.sin_family = AF_INET; |
| 64 | + |
| 65 | + defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress); |
| 66 | + |
| 67 | + SCNetworkReachabilityFlags flags; |
| 68 | + BOOL gotFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags); |
| 69 | + if (!gotFlags) { |
| 70 | + return NO; |
| 71 | + } |
| 72 | + |
| 73 | + // kSCNetworkReachabilityFlagsReachable indicates that the specified nodename or address can |
| 74 | + // be reached using the current network configuration. |
| 75 | + BOOL isReachable = flags & kSCNetworkReachabilityFlagsReachable; |
| 76 | + |
| 77 | + // This flag indicates that the specified nodename or address can |
| 78 | + // be reached using the current network configuration, but a |
| 79 | + // connection must first be established. |
| 80 | + // |
| 81 | + // If the flag is false, we don't have a connection. But because CFNetwork |
| 82 | + // automatically attempts to bring up a WWAN connection, if the WWAN reachability |
| 83 | + // flag is present, a connection is not required. |
| 84 | + BOOL noConnectionRequired = !(flags & kSCNetworkReachabilityFlagsConnectionRequired); |
| 85 | + if ((flags & kSCNetworkReachabilityFlagsIsWWAN)) { |
| 86 | + noConnectionRequired = YES; |
| 87 | + } |
| 88 | + |
| 89 | + // Callers of this method might want to use the reachability flags, so if an 'out' parameter |
| 90 | + // was passed in, assign the reachability flags to it. |
| 91 | + if (outFlags) { |
| 92 | + *outFlags = flags; |
| 93 | + } |
| 94 | + |
| 95 | + return isReachable && noConnectionRequired; |
| 96 | +} |
| 97 | + |
| 98 | +- (void)applicationDidFinishLaunching:(UIApplication *)application { |
| 99 | + // Is WiFi network available? That's necessary to use Bonjour. |
| 100 | + if ([self isNetworkAvailableFlags:NULL] == NO) { |
| 101 | + // open an alert with just an OK button |
| 102 | + UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No WiFi network available." |
| 103 | + message:@"Exit this app and enable WiFi using the Settings application." |
| 104 | + delegate:self |
| 105 | + cancelButtonTitle:@"OK" |
| 106 | + otherButtonTitles: nil]; |
| 107 | + [alert show]; |
| 108 | + [alert release]; |
| 109 | + } |
| 110 | + else { |
| 111 | + // Add the controller's view as a subview of the window |
| 112 | + [self.window addSubview:navController.view]; |
| 113 | + |
| 114 | + NSOperationQueue * theQueue = [[NSOperationQueue alloc] init]; |
| 115 | + self.cryptoQueue = theQueue; |
| 116 | + [theQueue release]; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +- (void)dealloc { |
| 121 | + [cryptoQueue release]; |
| 122 | + [navController release]; |
| 123 | + [window release]; |
| 124 | + [super dealloc]; |
| 125 | +} |
| 126 | + |
| 127 | +@end |
0 commit comments