iOS判断网络代码

来源:互联网 发布:苏大网络接入认证系统 编辑:程序博客网 时间:2024/04/28 05:54

今天给大家分享oc如何判断网络,首先大家先去下载这个已经封装的好的类,到时咱们就用它来判断网络,很简单哦,这是链接http://pan.baidu.com/s/1o6vAUam

下载好了之后解压,然后拖入到你的项目之中,然后再AppDelegate.m中导入头文件

#import "Reachability.h"

之后在这个方法中写入代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    

Reachability *reachability = [ReachabilityreachabilityWithHostName:@"www.apple.com"];

    switch ([reachabilitycurrentReachabilityStatus]) {

        caseNotReachable:

        {

            UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"当前没有网络连接哦!"message:nildelegate:selfcancelButtonTitle:@"确定"otherButtonTitles: nil];

            

            [alertshow];

            [alertrelease];

        }

           break;

        caseReachableViaWiFi:

           NSLog(@"wifi连接");

           break;

        caseReachableViaWWAN:

           NSLog(@"蜂窝数据");

           break;

       default:

           break;

    }


    return YES;

}


当然这只是判断网络的代码,有了这段代码在,你的App就会判断个网络了哦,谢谢大家!

0 0