zoukankan      html  css  js  c++  java
  • iOS Reachability的基本用法

    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    #import "AppDelegate.h"
    #import "RootViewController.h"
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        self.window.rootViewController = [[RootViewController alloc] init];
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    /**
     *  注意: 要在工程里导入SystemConfiguration.framework框架
     *  Reachability下载地址:  https://github.com/tonymillion/Reachability
     */
    #import "RootViewController.h"
    #import "Reachability.h"
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        Reachability *reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
        
        NSLog(@"------%@",reach.currentReachabilityString);
    
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:reach.currentReachabilityString message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        reach.reachableBlock = ^(Reachability *reach){
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"网络可用");
               alertView.message = @"当前网络可用";
                [alertView show];
            });
        };
        
        reach.unreachableBlock = ^(Reachability *reach){
            alertView.message = @"当前网络不可用";
            [alertView show];
        };
        [reach startNotifier];
        
    }
    
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    
    @end
  • 相关阅读:
    程序员第一定律:关于技能与收入
    JS注册/移除事件处理程序
    关于程序猿,你不知道的15件事
    .NET后台输出js脚本的方法
    新项目经理必读:分析什么是项目经理
    项目如何开始:怎样和客户一起搞定需求
    【转】为什么程序员讨厌修改静态检查问题
    js的with语句使用方法
    软件版本号 详解
    PHP记忆碎片2投票汇总
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5164085.html
Copyright © 2011-2022 走看看