/* |
File: APLViewController.m |
Abstract: Application delegate class. |
Version: 3.5 |
|
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
Inc. ("Apple") in consideration of your agreement to the following |
terms, and your use, installation, modification or redistribution of |
this Apple software constitutes acceptance of these terms. If you do |
not agree with these terms, please do not use, install, modify or |
redistribute this Apple software. |
|
In consideration of your agreement to abide by the following terms, and |
subject to these terms, Apple grants you a personal, non-exclusive |
license, under Apple's copyrights in this original Apple software (the |
"Apple Software"), to use, reproduce, modify and redistribute the Apple |
Software, with or without modifications, in source and/or binary forms; |
provided that if you redistribute the Apple Software in its entirety and |
without modifications, you must retain this notice and the following |
text and disclaimers in all such redistributions of the Apple Software. |
Neither the name, trademarks, service marks or logos of Apple Inc. may |
be used to endorse or promote products derived from the Apple Software |
without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or |
implied, are granted by Apple herein, including but not limited to any |
patent rights that may be infringed by your derivative works or by other |
works in which the Apple Software may be incorporated. |
|
The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
|
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
POSSIBILITY OF SUCH DAMAGE. |
|
Copyright (C) 2014 Apple Inc. All Rights Reserved. |
|
*/ |
|
#import "APLViewController.h" |
#import "Reachability.h" |
|
|
@interface APLViewController () |
|
@property (nonatomic, weak) IBOutlet UILabel* summaryLabel; |
|
@property (nonatomic, weak) IBOutlet UITextField *remoteHostLabel; |
@property (nonatomic, weak) IBOutlet UIImageView *remoteHostImageView; |
@property (nonatomic, weak) IBOutlet UITextField *remoteHostStatusField; |
|
@property (nonatomic, weak) IBOutlet UIImageView *internetConnectionImageView; |
@property (nonatomic, weak) IBOutlet UITextField *internetConnectionStatusField; |
|
@property (nonatomic, weak) IBOutlet UIImageView *localWiFiConnectionImageView; |
@property (nonatomic, weak) IBOutlet UITextField *localWiFiConnectionStatusField; |
|
@property (nonatomic) Reachability *hostReachability; |
@property (nonatomic) Reachability *internetReachability; |
@property (nonatomic) Reachability *wifiReachability; |
|
@end |
|
|
|
|
@implementation APLViewController |
|
- (void)viewDidLoad |
{ |
self.summaryLabel.hidden = YES; |
|
/* |
Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the method reachabilityChanged will be called. |
*/ |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; |
|
//Change the host name here to change the server you want to monitor. |
NSString *remoteHostName = @"www.apple.com"; |
NSString *remoteHostLabelFormatString = NSLocalizedString(@"Remote Host: %@", @"Remote host label format string"); |
self.remoteHostLabel.text = [NSString stringWithFormat:remoteHostLabelFormatString, remoteHostName]; |
|
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName]; |
[self.hostReachability startNotifier]; |
[self updateInterfaceWithReachability:self.hostReachability]; |
|
self.internetReachability = [Reachability reachabilityForInternetConnection]; |
[self.internetReachability startNotifier]; |
[self updateInterfaceWithReachability:self.internetReachability]; |
|
self.wifiReachability = [Reachability reachabilityForLocalWiFi]; |
[self.wifiReachability startNotifier]; |
[self updateInterfaceWithReachability:self.wifiReachability]; |
|
} |
|
|
/*! |
* Called by Reachability whenever status changes. |
*/ |
- (void) reachabilityChanged:(NSNotification *)note |
{ |
Reachability* curReach = [note object]; |
NSParameterAssert([curReach isKindOfClass:[Reachability class]]); |
[self updateInterfaceWithReachability:curReach]; |
} |
|
|
- (void)updateInterfaceWithReachability:(Reachability *)reachability |
{ |
if (reachability == self.hostReachability) |
{ |
[self configureTextField:self.remoteHostStatusField imageView:self.remoteHostImageView reachability:reachability]; |
NetworkStatus netStatus = [reachability currentReachabilityStatus]; |
BOOL connectionRequired = [reachability connectionRequired]; |
|
self.summaryLabel.hidden = (netStatus != ReachableViaWWAN); |
NSString* baseLabelText = @""; |
|
if (connectionRequired) |
{ |
baseLabelText = NSLocalizedString(@"Cellular data network is available.\nInternet traffic will be routed through it after a connection is established.", @"Reachability text if a connection is required"); |
} |
else |
{ |
baseLabelText = NSLocalizedString(@"Cellular data network is active.\nInternet traffic will be routed through it.", @"Reachability text if a connection is not required"); |
} |
self.summaryLabel.text = baseLabelText; |
} |
|
if (reachability == self.internetReachability) |
{ |
[self configureTextField:self.internetConnectionStatusField imageView:self.internetConnectionImageView reachability:reachability]; |
} |
|
if (reachability == self.wifiReachability) |
{ |
[self configureTextField:self.localWiFiConnectionStatusField imageView:self.localWiFiConnectionImageView reachability:reachability]; |
} |
} |
|
|
- (void)configureTextField:(UITextField *)textField imageView:(UIImageView *)imageView reachability:(Reachability *)reachability |
{ |
NetworkStatus netStatus = [reachability currentReachabilityStatus]; |
BOOL connectionRequired = [reachability connectionRequired]; |
NSString* statusString = @""; |
|
switch (netStatus) |
{ |
case NotReachable: { |
statusString = NSLocalizedString(@"Access Not Available", @"Text field text for access is not available"); |
imageView.image = [UIImage imageNamed:@"stop-32.png"] ; |
/* |
Minor interface detail- connectionRequired may return YES even when the host is unreachable. We cover that up here... |
*/ |
connectionRequired = NO; |
break; |
} |
|
case ReachableViaWWAN: { |
statusString = NSLocalizedString(@"Reachable WWAN", @""); |
imageView.image = [UIImage imageNamed:@"WWAN5.png"]; |
break; |
} |
case ReachableViaWiFi: { |
statusString= NSLocalizedString(@"Reachable WiFi", @""); |
imageView.image = [UIImage imageNamed:@"Airport.png"]; |
break; |
} |
} |
|
if (connectionRequired) |
{ |
NSString *connectionRequiredFormatString = NSLocalizedString(@"%@, Connection Required", @"Concatenation of status string with connection requirement"); |
statusString= [NSString stringWithFormat:connectionRequiredFormatString, statusString]; |
} |
textField.text= statusString; |
} |
|
|
- (void)dealloc |
{ |
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil]; |
} |
|
|
@end |