How to get Mobile data status in iPhone when WiFi is ON

不打扰是莪最后的温柔 提交于 2019-12-24 13:28:24

问题


Currently i am working on an iPhone Application which needs to display the mobile data status(ON/OFF). For network check im using Reachability class but it is giving me correct result when any one of the networks are enable i.e WiFi/Mobile Data, but if both are enable it is giving Wifi status, but my requirement is i just need to mobile data status also. It would be great if anyone suggest me solution.

Thankq


回答1:


Set some logic , like check , if network is connected to Wifi or Cellular-data , and then set your code in cellular data.

Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];

NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable) 
{
    //No internet
}
else if (status == ReachableViaWiFi)
{
    //WiFi
}
else if (status == ReachableViaWWAN) 
{
    //3G // Set your code here for cellular data
}

Find more detail on ios-detect-3g-or-wifi set according to your requirement.

Note :- I think when your iPhone is connected with wifi then it lefts cellular data automatically and if disconnect wifi then again connected with cellular data

Edit :- I am not sure but try this ,

if(status == NotReachable) 
    {
        //No internet
    }
   else if (status == ReachableViaWWAN) 
    {
        //3G // Set your code here for cellular data
       if (status == ReachableViaWiFi)
      {
        //WiFi // Keep Wifi and cellulardata both on.
      }
    }
    else if (status == ReachableViaWiFi)
    {
        //WiFi
    }


     else if (status == ReachableViaWWAN) 
    {
      // keep status only cellularadat on
     }



回答2:


Sadly, there seems to be no way to check that mobile data is turned on when WiFi is turned on. You can detect whether there is a WiFi connection, whether there is no WiFi connection but a mobile data connection, or whether there is no connection at all. But if there is a WiFi connection, there seems to be no way to detect whether there is a data connection as well.

Using the telephony classes, you can detect whether the device has a SIM card, and you can detect whether mobile data has been disabled in Settings for your application. You can not detect as far as I know whether mobile data is enabled for the whole device.

You can also not distinguish between Wifi turned off, WiFi turned on but no network nearby or selected, and Airplane mode turned on.



来源:https://stackoverflow.com/questions/35722673/how-to-get-mobile-data-status-in-iphone-when-wifi-is-on

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