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.
Related
I'm using AFNetworking to determine whether the user has an internet connection. It returns false on both wifi and 4G LTE and I have checked to make sure they're operational.
I have the following code:
-(void)viewDidLoad{
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
if ([self connected])
[self doSomething];
else
[self noConnection];
}
- (BOOL)connected {
NSLog(#"This returns 0 %hhd", [AFNetworkReachabilityManager sharedManager].reachable);
return [AFNetworkReachabilityManager sharedManager].reachable;
}
-(void)noConnection{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Internet!"
message:#"Sorry, but you don't have an internet connection at this time. Please try again later."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
This is not a solution using AFNetworking but using sample code from apple
Once you have downloaded and imported Reachbility.m and Reachbility.h files
create a helper function:
-(BOOL)isConnected{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
Then use it
if([self isConnected]){
//connected!
}
else{
//not connected to internet!
}
Very important
If your project is not using arc
go to target >
Build Phase >
double click the Reachability file
add -fno-objc-arc
Hope this helps
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 trigger an action once the connection is available. There are solutions available which allows manually checking internet connection. One way i find is using NSTimer to check for internet connection during fixed intervals. But is it the most effective way to check for it? if Not, What is the right solution for this?
Here how you can register the observer and listen to it, Your application will be listening to kReachabilityChangedNotification & prompt you whenever status of network changes.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(reachabilityHasChanged:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
-(void) reachabilityHasChanged:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(#"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(#"The internet is working via WIFI.");
break;
}
case ReachableViaWWAN:
{
NSLog(#"The internet is working via WWAN.");
break;
}
}
}
Check reachability code provided by apple.In appdelegate.m you can see this method.It will notify the network change.Work on it
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
}
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.
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];
}
}