How to detect wheter running on 3G or Wi-Fi on iPhone?

北城余情 提交于 2019-12-08 13:48:42

问题


Some lines of code? Any experience?


回答1:


You can use Apple's Reachability code to retrieve this information:

Example:

Reachability *reach = [Reachability reachabilityForLocalWiFi];
[reach startNotifier];

NetworkStatus stat = [reach currentReachabilityStatus];

if(stat & NotReachable) {
   //not reachable
}

if(stat & ReachableViaWiFi) {
   //reachable via wifi
}

if(stat & ReachableViaWWAN) {
   //reachable via wwan
}



回答2:


Apple's Reachability class will give you this information.

http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html




回答3:


Since than I made a pretty simple block based Reachability wrapper that strips all the outdated C-like Reachability code, poured into a much more Cocoa form.

Usage like:

[EPPZReachability reachHost:hostNameOrIPaddress
               completition:^(EPPZReachability *reachability)
{
    if (reachability.reachableViaCellular) [self doSomeLightweightStuff];
}];

See Reachability with blocks for everyday use at eppz!blog, or grab it directly from eppz!reachability at GitHub.

It also works with IP addresses, which turned out to be a pretty rare Reachability wrapper feature.



来源:https://stackoverflow.com/questions/5589324/how-to-detect-wheter-running-on-3g-or-wi-fi-on-iphone

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