美文网首页
ios笔记-判断手机网络状态

ios笔记-判断手机网络状态

作者: 骑在树上的骷髅怪 | 来源:发表于2017-11-16 15:24 被阅读23次

    引入 #import "AFNetworkReachabilityManager.h"

    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];

    // 提示:要监控网络连接状态,必须要先调用单例的startMonitoring方法

    [manager startMonitoring];

    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    if (status == -1) {

    NSLog(@"未识别网络");

    UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"未识别网络"

    message:@"请打开设置→网络/Wi-Fi"

    preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancel=[UIAlertAction actionWithTitle:@"取消"

    style:UIAlertActionStyleCancel handler:nil];

    UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定"

    style:UIAlertActionStyleDefault

    handler:^(UIAlertAction * _Nonnull action) {

    }];

    [alertview addAction:cancel];

    [alertview addAction:defult];

    [self presentViewController:alertview animated:YES completion:nil];

    }

    if (status == 0) {

    NSLog(@"未连接网络");

    UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"未连接网络"

    message:@"请打开设置-蜂窝移动网络/Wi-Fi"

    preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定"

    style:UIAlertActionStyleDefault

    handler:^(UIAlertAction * _Nonnull action) {

    }];

    [alertview addAction:defult];

    [self presentViewController:alertview animated:YES completion:nil];

    }

    if (status == 1) {

    NSLog(@"3G/4G网络");

    }

    if (status == 2) {

    NSLog(@"Wifi网络");

    }

    }];

    相关文章

      网友评论

          本文标题:ios笔记-判断手机网络状态

          本文链接:https://www.haomeiwen.com/subject/imwlvxtx.html