How Application will get to know Availability of i

2019-06-14 20:08发布

I am new to Iphone app development so i am having some couple of questions. Help me out on this.

1) How, all the application on iphone will get to know that, there is internet connection available when user switches on wifi button or Cellular button in settings?

2) How to differentiated between wifi connection and cellular connection?

3) Are there any Broadcast receiver mechanism in iphone similar to android concepts as well?

I had googled many links for internet connectivity, and got to know about Reachibilty class in iPhone, but i was not able to get clear picture on how it works? if any once can give me link which explains me in detail about reachibility, that would be great. or any other mechanism to achieve above functionality.

I am writing an application, where i need to start uploading some data, when application will get to know that there is connection available, and this should happen even when application is in background or in foreground.

Thanks in advance

3条回答
Emotional °昔
2楼-- · 2019-06-14 20:16

You can use Rechability class for that provided in this link

also go thrugh this link this will help you am sure.

How to check for an active Internet connection on iOS or OSX?

or this simple line of code also usefull

Reachability *reachability = [Reachability reachabilityForInternetConnection];   
NetworkStatus networkStatus = [reachability currentReachabilityStatus];    
if (networkStatus == NotReachable) {        
    NSLog(@"There IS NO internet connection");        
} else {        

     NSLog(@"There IS internet connection");        


    }        
}
查看更多
仙女界的扛把子
3楼-- · 2019-06-14 20:32

You should use the class Reachability

 NetworkStatus netStatus = [curReach currentReachabilityStatus];

NSString* statusString= @"";
switch (netStatus)
{
    case NotReachable:
    {
        statusString = @"Access Not Available";

        //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...

        break;
    }

    case ReachableViaWWAN:
    {
        statusString = @"Reachable WWAN";

        break;
    }
    case ReachableViaWiFi:
    {
         statusString= @"Reachable WiFi";

        break;
  }
}

[[Reachability reachabilityForInternetConnection] startNotifier];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(someMethod:) name:kReachabilityChangedNotification object:nil];
查看更多
女痞
4楼-- · 2019-06-14 20:33

Try with this sample code Sample and
https://github.com/tonymillion/Reachability

It will also allow you to check if WiFi is enabled:

Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:@"www.google.com"];    // set your host name here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];

if(remoteHostStatus == NotReachable) { }
else if (remoteHostStatus == ReachableViaWiFiNetwork) { }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { }
查看更多
登录 后发表回答