在之前的iPhone中、我们可以根据导航栏上方的网络状态view、来判断网络状态。(这种方案本来就不太好)

并且,这种方案在iPhone X 手机上、不可使用。

那么,在iPhone X 或者之前的手机上面该怎么办呢?

我们可以通过 Reachability  来判断网络状态

Reachability github 地址:https://github.com/tonymillion/Reachability

使用方法超简单,Block方式和NSNotification方式 二选一即可。

1、添加SystemConfiguration.framework.

2、使用Block方式

  注意:当网络状态发生改变的时候、会在后台进程中触发Block、我们需要在主线程中进行UI更新操作。

Objective-C 代码如下:

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"]; // Set the blocks
reach.reachableBlock = ^(Reachability*reach)
{
// keep in mind this is called on a background thread
// and if you are updating the UI it needs to happen
// on the main thread, like this: dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"REACHABLE!");
});
}; reach.unreachableBlock = ^(Reachability*reach)
{
NSLog(@"UNREACHABLE!");
}; // Start the notifier, which will cause the reachability object to retain itself!
[reach startNotifier];

Swift  代码如下:

import Reachability

var reach: Reachability?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Allocate a reachability object
self.reach = Reachability.forInternetConnection() // Set the blocks
self.reach!.reachableBlock = {
(reach: Reachability?) -> Void in // keep in mind this is called on a background thread
// and if you are updating the UI it needs to happen
// on the main thread, like this:
DispatchQueue.main.async {
print("REACHABLE!")
}
} self.reach!.unreachableBlock = {
(reach: Reachability?) -> Void in
print("UNREACHABLE!")
} self.reach!.startNotifier() return true
}

3、NSNotification 方式

  此方式界面发生了变化时、将会通知。通知将在主线程上传递,所以可以从函数中进行UI更新。此外,它要求监控对象考虑WWAN(3G / EDGE / CDMA)作为一个不可到达的连接(比如正在写一个视频流的应用程序,或者下载视频等等)。

Objective-C 代码如下:

// Allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.baidu.com"]; // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
reach.reachableOnWWAN = NO; // Here we set up a NSNotification observer. The Reachability that caused the notification
// is passed in the object parameter
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil]; [reach startNotifier];

Swift  代码如下:

import Reachability

var reach: Reachability?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Allocate a reachability object
self.reach = Reachability.forInternetConnection() // Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
self.reach!.reachableOnWWAN = false // Here we set up a NSNotification observer. The Reachability that caused the notification
// is passed in the object parameter
NotificationCenter.default.addObserver(
self,
selector: #selector(reachabilityChanged),
name: NSNotification.Name.reachabilityChanged,
object: nil
) self.reach!.startNotifier() return true
} func reachabilityChanged(notification: NSNotification) {
if self.reach!.isReachableViaWiFi() || self.reach!.isReachableViaWWAN() {
print("Service avalaible!!!")
} else {
print("No service avalaible!!!")
}
}

iOS 网络状态判断方案(支持iOS11和iPhoneX)的更多相关文章

  1. iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)

    前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...

  2. iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G) by徐文棋

    前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...

  3. 利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)

    前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...

  4. Android杂谈--网络状态判断

    许多联网应用都在开始运行的时候检查当前网络状态,如果没有开启则去开启它,记录一下以前写程序时的网络检查,发现人的记忆力真是有限,总是隔段时间久忘记,所以记录下来是最好的记忆. 我们可以在一开始启动程序 ...

  5. 选择提示框UIAlertController 和网络状态判断AFNetworking

    // 选择提示框 DownloadView *vc = [[DownloadView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_ ...

  6. iOS完美的网络状态判断工具

    大多数App都严重依赖于网络,一款用户体验良好的的app是必须要考虑网络状态变化的.iOSSinger下一般使用Reachability这个类来检测网络的变化. Reachability 这个是苹果开 ...

  7. android 网络状态判断【转】

    import java.net.InetAddress; import android.app.Activity;import android.content.Context;import andro ...

  8. iOS获取手机当前的网络状态

    获取iOS网络状态,目前有两个办法. 1.通过监听手机状态栏的信息. 2.通过使用官方提供的类Reachability. 一.通过手机监听手机状态栏的信息 好处: 1.可以通过苹果的审核上架AppSt ...

  9. iOS 中如何判断当前是2G/3G/4G/5G/WiFi

    5G 什么的,还得等苹果API更新啊,不过将来还是这个处理过程就是了. 关于判断当前的网络环境是2G/3G/4G,这个问题以前经常看到,最近在一工程里看到了如果判断的API.而在撸WebRTC音视频通 ...

随机推荐

  1. LKD: Chapter 7 Interrupts and Interrupt Handlers

    Recently I realized my English is still far from good. So in order to improve my English, I must not ...

  2. python基础教程——切片

    获取list或tuple的部分元素: L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] L[0:3] ['Michael', 'Sarah', 'Tra ...

  3. ssh、scp免秘钥远程执行命令:expect

    首先安装expect # yum -y install expect 命令格式 # ./expect IP COMM    #expect是独立的工具,所以不能用sh来执行 1 2 3 4 5 6 7 ...

  4. 《java.util.concurrent 包源码阅读》05 BlockingQueue

    想必大家都很熟悉生产者-消费者队列,生产者负责添加元素到队列,如果队列已满则会进入阻塞状态直到有消费者拿走元素.相反,消费者负责从队列中拿走元素,如果队列为空则会进入阻塞状态直到有生产者添加元素到队列 ...

  5. 极光推送_总结_01_Java实现极光推送

    一.代码实现 1.配置类—Env.java package com.ray.jpush.config; /**@desc : 极光推送接入配置 * * @author: shirayner * @da ...

  6. 迷宫问题-POJ 3984

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24348   Accepted: 14206 Descriptio ...

  7. LogCook 一个简单实用的Android日志管理工具

    众所周知,日志的管理是软件系统很重要的一部分,千万不可忽略其重要性.完整的日志将会在系统维护中起着异常重要的作用,就好像磨刀不误砍柴工一样,日志就像对系统进行分析的工具,工具便捷了,对系统分析起来就能 ...

  8. SaltStack 架构自动部署 03

    架构图 模块化部署 系统模块:系统优化,内核参数,网络参数 功能模块:如:nginx,tomcat, 业务模块: 1.在salt-master端修改配置文件 [root@01 salt]# vim / ...

  9. 条件注释判断IE版本

    在学习Bootstra的时候看到这么一句话, <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media ...

  10. 通过Nutch扩展点开发插件(添加自定义索引字段到solr)

    爬虫系统:通过Nutch扩展点开发插件(添加自定义索引字段到solr) 准备工作 爬虫环境 -- nutch2.3.1 + solr4.10.3 + hbase0.98 开发环境 -- Eclipse ...