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
  • 相关阅读:
    基于ABP落地领域驱动设计-04.领域服务和应用服务的最佳实践和原则
    基于ABP落地领域驱动设计-03.仓储和规约最佳实践和原则
    基于ABP落地领域驱动设计-02.聚合和聚合根的最佳实践和原则
    基于ABP落地领域驱动设计-01.全景图
    Es6-find&map&filter&reduce
    vue之监听事件
    list map互相转换
    springcloud 返回实体类忽略属性
    Apache NetBeans IDE 12.3 双击无反应怪事
    前端--- 前端调试经验总结
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5164085.html
Copyright © 2011-2022 走看看