首页 > 移动平台 > 详细

iOS Reachability的基本用法

时间:2016-01-27 18:52:40      阅读:190      评论:0      收藏:0      [点我收藏+]
#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

 

iOS Reachability的基本用法

原文:http://www.cnblogs.com/lantu1989/p/5164085.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!