i have developed a radio application which uses network connection for online streaming, and also i am checking the condition whether network is available or not. if no network connection, it shows an alert "their is no network available" .my code is here
- (void)viewDidLoad
{
[super viewDidLoad];
//checking network reachability statys, this will show one alert view if no network available
Reachability* reachabile = [Reachability reachabilityWithHostName:#"www.apple.com"];
NetworkStatus remoteHostStatus = [reachabile currentReachabilityStatus];
if(remoteHostStatus == NotReachable)
{
NSLog(#"not reachable");
UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:#"NO INTERNET CONNECTION" message:#"This Application Need Internet To Run" delegate:self cancelButtonTitle:#"Okay Buddy" otherButtonTitles:nil];
notReachableAlert1.delegate=self;
[notReachableAlert1 show];
[notReachableAlert1 release];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayerPlaybackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];
// Do any additional setup after loading the view from its nib.
}
also i checked the condition on notifications
-(void) moviePlayerPlaybackStateDidChange:(NSNotification*)notification
{
Reachability* reachabile = [Reachability reachabilityWithHostName:#"www.apple.com"];
NetworkStatus remoteHostStatus = [reachabile currentReachabilityStatus];
NSLog(#"playbackDidChanged");
MPMoviePlayerController *moviePlayer = notification.object;
player=notification.object;
MPMoviePlaybackState playbackState = moviePlayer.playbackState;
if(playbackState == MPMoviePlaybackStateStopped)
{
NSLog(#"MPMoviePlaybackStateStopped");
}
else if(playbackState == MPMoviePlaybackStatePlaying) {
NSLog(#"MPMoviePlaybackStatePlaying");
} else if(playbackState == MPMoviePlaybackStatePaused) {
NSLog(#"MPMoviePlaybackStatePaused");
if(remoteHostStatus == NotReachable)
{
NSLog(#"not reachable");
UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:#"NO INTERNET CONNECTION" message:#"This Application Need Internet To Run" delegate:self cancelButtonTitle:#"Okay Buddy" otherButtonTitles:nil];
notReachableAlert1.delegate=self;
[notReachableAlert1 show];
[notReachableAlert1 release];
}
} else if(playbackState == MPMoviePlaybackStateInterrupted)
{
NSLog(#"MPMoviePlaybackStateInterrupted");
if((remoteHostStatus == NotReachable)&&(remoteHostStatus != ReachableViaWiFi))
{
NSLog(#"not reachable");
UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:#"NO INTERNET CONNECTION" message:#"This Application Need Internet To Run" delegate:self cancelButtonTitle:#"Okay Buddy" otherButtonTitles:nil];
notReachableAlert1.delegate=self;
[notReachableAlert1 show];
[notReachableAlert1 release];
}
my problem is, when app goes out of range from wifi connection without without 3g and normal data connection, it freezes for some time. and when i came back to range only it goes to active state and shows alert.
is their anything wrong i did with network availability checking?
this is sample u can edit based on ur application
#class Reachability;
#interface urAppDelegate : NSObject <UIApplicationDelegate>
{
Reachability* internetReachable;
Reachability* hostReachable;
BOOL hostActive;
BOOL internetActive;
}
#property (nonatomic, assign) BOOL hostActive;
#property (nonatomic, assign) BOOL internetActive;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.internetActive=NO;
self.hostActive=NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: #"www.apple.com"] retain];
[hostReachable startNotifier];
// now patiently wait for the notification
[self.window makeKeyAndVisible];
return YES;
}
-(void) checkNetworkStatus:(NSNotification *)notice{
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(#"The internet is down.");
self.internetActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(#"The internet is working via WIFI.");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(#"The internet is working via WWAN.");
self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(#"A gateway to the host server is down.");
self.hostActive = NO;
break;
}
case ReachableViaWiFi:
{
NSLog(#"A gateway to the host server is working via WIFI.");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(#"A gateway to the host server is working via WWAN.");
self.hostActive = YES;
break;
}
}
if (internetActive && hostActive)
{
// Net work Available.......
}
else
{
UIAlertView *netWorkAlert=[[UIAlertView alloc]initWithTitle:#"Network Connection Error" message:#"Please Check Connection" delegate:nil cancelButtonTitle:#"Close" otherButtonTitles:nil, nil];
[netWorkAlert show];
[netWorkAlert release];
}
}
Related
I am currently writing a game which needs an internet connection (TCP to server). I'm attempting to utilize the Reachability class from Apple, but it doesn't work. I'm new to NotificationCenter and Reachability, so please excuse me if this is a stupid question.
Reachability related props in LoginViewController.h
#property (nonatomic) Reachability *hostReachability;
#property (nonatomic) Reachability *internetReachability;
#property (nonatomic) Reachability *wifiReachability;
#property BOOL internetActive;
#property BOOL hostActive;
In the LoginViewController.m
//Reachability
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];
NSString* hostIP = #"<my server IP is here, removed for obvious reasons>";
hostReachability = [Reachability reachabilityWithHostname:hostIP];
[hostReachability startNotifier];
NetworkStatus hostStatus = [internetReachability currentReachabilityStatus];
if(hostStatus == NotReachable) {NSLog(#"no");}
else if (hostStatus == ReachableViaWiFi) {NSLog(#"wifi"); }
else if (hostStatus == ReachableViaWWAN) {NSLog(#"cell"); }
hostReachability = [Reachability reachabilityForInternetConnection];
[internetReachability startNotifier];
NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];
if(internetStatus == NotReachable) {NSLog(#"no");}
else if (internetStatus == ReachableViaWiFi) {NSLog(#"wifi"); }
else if (internetStatus == ReachableViaWWAN) {NSLog(#"cell"); }
handleNetworkChange: procedure
- (void)handleNetworkChange:(NSNotification*)notification{
NetworkStatus internetStatus = [internetReachability currentReachabilityStatus];
switch (internetStatus) {
case NotReachable:
{
NSLog(#"No internet connection");
self.internetActive = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No internet connection"
message:#"Could not connect to the internet. Please try again later."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
break;
case ReachableViaWiFi:
{
NSLog(#"The internet is working via WIFI.IN AGENDA");
self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(#"The internet is working via WWAN.IN AGENDA");
self.internetActive = YES;
break;
}
default:
break;
}
NetworkStatus hostStatus = [hostReachability currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
NSLog(#"A gateway to the host server is down.IN AGENDA");
self.hostActive = NO;
NSLog(#"No internet connection");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No connection to host"
message:#"Could not connect to the host server. Please try again later."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
break;
}
case ReachableViaWiFi:
{
NSLog(#"A gateway to the host server is working via WIFI.IN AGENDA");
self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
NSLog(#"A gateway to the host server is working via WWAN.IN AGENDA");
self.hostActive = YES;
break;
}
}
When running the application, I always get "no no" in the terminal, regardless of whether or not I'm actually connected to the internet, and whether or not the server software is running.
Thanks in advance.
The main problem here is a typo in your code; the line reading hostReachability = [Reachability reachabilityForInternetConnection]; in LoginViewController.m should be internetReachability = [Reachability reachabilityForInternetConnection];. The way it currently is, you never instantiate internetReachability, and then I guess [internetReachability currentReachabilityStatus] will always return nil.
Below is a basic example you can test with:
#interface LoginViewController ()
// ...
#property (strong, nonatomic) Reachability *hostReachability;
#property (strong, nonatomic) Reachability *internetReachability;
#property (strong, nonatomic) Reachability *wifiReachability;
#end
#implementation LoginViewController
// ...
- (void)setupReachabilityMonitoring
{
[NSNotificationCenter.defaultCenter addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
_hostReachability = [Reachability reachabilityWithHostName:#"8.8.8.8"];
[_hostReachability startNotifier];
_internetReachability = [Reachability reachabilityForInternetConnection];
[_internetReachability startNotifier];
_wifiReachability = [Reachability reachabilityForLocalWiFi];
[_wifiReachability startNotifier];
[self logReachabilityStatus];
}
- (void)reachabilityChanged:(NSNotification *)notification
{
if ([notification.object isKindOfClass:Reachability.class]) {
[self logReachabilityStatus];
}
}
- (void)logReachabilityStatus
{
NSString *hostStatus = _hostReachability.currentReachabilityStatus ? #"up" : #"down";
NSString *internetStatus = _internetReachability.currentReachabilityStatus ? #"up" : #"down";
NSString *wifiStatus = _wifiReachability.currentReachabilityStatus ? #"up" : #"down";
NSLog(#"Internet is %#, wifi is %#, host is %#", internetStatus, wifiStatus, hostStatus);
}
#end
I am using this libarary using blocks so its doest not freeze the app and works in background
https://github.com/tonymillion/Reachability
I think the problem is in your "host name". It's not your server's IP, but your host name, e.g www.apple.com. You may check Apple's example code. At line 87, they set the host name to #"www.apple.com".
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
hi I’m trying check internet connection in my application. so for that i have already imported the reachability h file and m file in project. I’m getting some issues with that now. its working only if internet connection available its not working in without the net connection..
here this is the code which have used..
-(BOOL)reachable {
Reachability *reach = [Reachability reachabilityWithHostName:#"https://www.google.co.in/"];
NetworkStatus internetStatus = [reach currentReachabilityStatus];
if(internetStatus == NotReachable) {
UIAlertView *alertOne = [[UIAlertView alloc]initWithTitle:#"Internet" message:#"You dont have internet connection to send message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"ok", nil];
[alertOne show];
[alertOne release];
}
return YES;
}
i have already used this same code in other projects its working their but here its showing the alert message when internet connected its not showing when its not connected ...
this viewdidload code...
[super viewDidLoad];
[self reachable];
pls can any tell me how to resolve this...
thanks
Try below code
Reachability *reach = [Reachability reachabilityForInternetConnection];
//(or)
Reachability *reach = [Reachability reachabilityWithHostName:#"http://www.google.com"];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus != NotReachable)
{
//Reachable ..Network connection is available
}
else
{
//NSLog(#"Network Error No Network Available ");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"" message:#"Please connect to an Internet connection to Register" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil , nil];
[alertView show];
}
It works for you...
This may not be the cause of the problem, but it is and always was wrong to call reachable in viewDidLoad. The reason is that viewDidLoad is way too early to call code that might put up an alert view. In viewDidLoad, your view is not even in the interface yet.
Another very odd thing is that your reachable method both detects reachability and puts up the alert. It returns a BOOL but you are throwing it away. You should be saying
if (![self reachable]) { // ...
and putting up the alert view there, not inside the reachable method.
Still another very weird thing is that your reachable method always returns YES. That's nuts. You should return YES if the URL is reachable and NO if it is not. Otherwise, what is the point of returning a BOOL at all?
Please follow this steps,
1>make property of Reachability and NetworkStatus;
in your appDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>{
Reachability* reachability;
NetworkStatus remoteHostStatus;
}
#end
2>Create Notification method when reachability changed in applicationDidBecomeActive
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
if(DEBUG_MODE){NSLog(#"no");}
}
else if (remoteHostStatus == ReachableViaWiFi) {if(DEBUG_MODE)
{NSLog(#"wifi");}
}
else if (remoteHostStatus == ReachableViaWWAN) {if(DEBUG_MODE){NSLog(#"cell");}
}
}
3> declare reachabilityChanged
-(void)reachabilityChanged:(SCNetworkReachabilityFlags)flags
{
if([self isReachableWithFlags:flags])
{
if(self.reachableBlock)
{
self.reachableBlock(self);
}
}
else
{
if(self.unreachableBlock)
{
self.unreachableBlock(self);
}
}
// this makes sure the change notification happens on the MAIN THREAD
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification
object:self];
});
}
Thanks.
Im new in iphone and I want know that how to use Reachability in xcode.
I go on to Reachability example and read about it but understand about it.
I create one application and put Reachability.m and Reachability.h in it but I dont know how to use from it.
please guide me. I want when run application check my network Connection any time and run this code :
if (isConnection)
{
NSLog(#"Connection Success")
}
else
NSLog(#"Connection has been lost")
You can do this:
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
And now check the internetStatus var, by inspecting its value. The values are defined as:
typedef enum
{
// Apple NetworkStatus Compatible Names.
NotReachable = 0,
ReachableViaWiFi = 2,
ReachableViaWWAN = 1
} NetworkStatus;
So, in your case:
if (internetStatus == NotReachable)
{
NSLog(#"Bazinga!");
}
else
{
NSLog(#"Houston we have ignition");
}
Download Reachability Classes and Follow this code
internetReach = [[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];
Then we’ll set the NetworkStatus variable created in Reachability.
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
And finally we’ll use the netStatus in a switch block.
switch (netStatus)
{
case ReachableViaWWAN:
{
break;
}
case ReachableViaWiFi:
{
break;
}
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
}
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case ReachableViaWWAN:
{
break;
}
case ReachableViaWiFi:
{
break;
}
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"We are unable to make a internet connection at this time. Some functionality will be limited until a connection is made." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
}
}
i want to check internet connection in my App. While doing that, i use following code.
What is the difference between internetStatus and host status. Which should i use to check whether ipad has internet connection or not.
-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
//NSLog(#"The internet is down.");
//self.internetActive = NO;
//NSLog(#"A gateway to the host server is down.");
//self.hostActive = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Failed"
message:#"No internet connection"
delegate:nil
cancelButtonTitle:#"Exit"
otherButtonTitles:nil];
[alert setDelegate:self];
[alert show];
[alert release];
break;
}
case ReachableViaWiFi:
{
//NSLog(#"The internet is working via WIFI.");
//self.internetActive = YES;
break;
}
case ReachableViaWWAN:
{
//NSLog(#"The internet is working via WWAN.");
//self.internetActive = YES;
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
//NSLog(#"A gateway to the host server is down.");
//self.hostActive = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Connection Failed"
message:#"No internet connection"
delegate:nil
cancelButtonTitle:#"Exit"
otherButtonTitles:nil];
[alert setDelegate:self];
[alert show];
[alert release];
break;
}
case ReachableViaWiFi:
{
//NSLog(#"A gateway to the host server is working via WIFI.");
//self.hostActive = YES;
break;
}
case ReachableViaWWAN:
{
//NSLog(#"A gateway to the host server is working via WWAN.");
//self.hostActive = YES;
break;
}
}
}
internetStatus or hostStatus just the instance name you can use any name for status. but specifically if you don't set the host then you can use internetStatus for internet access if it is reachable or not, by default apple checks for internet availability to the internet gateway or internet connection, in this case we don't know the hostname for checking the internet connectivity , however you can use hostStatus or any instance name for a particular host, you could set the random host yourself e.g www.google.com and check if it is reachable. procedure is similar.
basically status is depending on your hostname which can be default or you can set it yourself like this;
hostReachable = [Reachability reachabilityWithHostName: #"www.google.com"];
[hostReachable startNotifier];
Hey guys i'm having a few problems with Apple's reachability code.
What i've found out is that, even if the device is correctly connected to the internet, initially the reachability code will send out 1 false notification(Networkstatus = NotReachable) followed by a couple of correct notifications (Networkstatus = ReachableViaWiFi).
Therefore, as i am displaying an UIAlertView when i get a "NotReachable" notification, even if the device is connected to the internet, the app still outputs an uialertview informing the user that the device is not connected.
Is there anyway to avoid this inconvenience?
Any help would be really appreciated.
This is my code:
In my .h file:
#property (nonatomic, retain) Reachability *hostReach;
In my .m file:
- (void)viewDidLoad
{
self.hostReach = [Reachability reachabilityWithHostname:#"www.google.com"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
[_hostReach startNotifier];
NetworkStatus netStatus = [self.hostReach currentReachabilityStatus];
if(netStatus == NotReachable && _alertShowing==NO){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"No internet connection found"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
_alertShowing = YES;
[alert show];
}
...
}
-(void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if(netStatus == NotReachable && _alertShowing==NO){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"No internet connection found"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
_alertShowing = YES;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
_alertShowing = NO;
}
Why do you use reachabilityWithHostname:#"www.google.com"? This method checks the reachability of particular host (in your case google.com). And you receive notifications if Google available or not. Google may block you and you will receive NotReachable status.
Try to use:
//reachabilityForInternetConnection- checks whether the default route is available.
// Should be used by applications that do not connect to a particular host
+ (Reachability*) reachabilityForInternetConnection;
And also take a look on methods description here.