How Application will get to know Availability of internet connection in iPhone?

孤街醉人 提交于 2019-12-08 14:21:51

问题


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


回答1:


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");        


    }        
}



回答2:


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];



回答3:


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) { }


来源:https://stackoverflow.com/questions/17647477/how-application-will-get-to-know-availability-of-internet-connection-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!