swift之判断网络状态Alamofire、Reachability
2018-01-26 10:51
363 查看
==============//Alamofire监控网络
func monitorNet() {
let manager = NetworkReachabilityManager(host: "www.apple.com")
manager?.listener = { status in
print("网络状态: \(status)")
}
manager?.startListening()//开始监听网络
}
================Reachability.swift监控网络
下载地址https://github.com/ashleymills/Reachability.swift
var reachability:
Reachability?
var netWorkIsAvailable
func monitorNet(){
do {
reachability = try Reachability.reachabilityForInternetConnection()
} catch {
print("unable to create Reachability")
return
}
if reachability.isReachable() {
print("appDelegate:网络连接:可用")
netWorkIsAvailable = true
} else {
print("appDelegate:网络连接:不可用")
netWorkIsAvailable = false
}
if reachability!.isReachableViaWiFi() {
print("连接类型:WiFi")
} else if reachability!.isReachableViaWWAN() {
print("连接类型:移动网络")
} else {
print("连接类型:没有网络连接")
}
do {
try reachability?.startNotifier()//开始监测
} catch {
print("could not start reachability notifier")
}
}
// 网络可用或切换网络类型时执行
reachability.whenReachable = { reachability in
// 判断网络状态及类型
}
// 网络不可用时执行
reachability.whenUnreachable = { reachability in
// 判断网络状态及类型
}
// ======= ==== = ================
/**
通过消息自动监听网络改变
添加消息监听
*/
func NetworkStatusListener() {
// 1、设置网络状态消息监听
NSNotificationCenter.defaultCenter().addObserver(self, selector:"networkStatusChange", name: ReachabilityChangedNotification, object:nil);
// 2、获得网络Reachability对象
// Reachability必须一直存在,所以需要设置为全局变量
conn = Reachability.reachabilityForInternetConnection()!;
// 3、开启网络状态消息监听
conn!.startNotifier();
}
func networkStatusChange() {
checkNetworkStatus();
}
/**
移除消息通知
*/
deinit {
// 关闭网络状态消息监听
conn!.stopNotifier();
// 移除网络状态消息通知
NSNotificationCenter.defaultCenter().removeObserver(self);
}
/**
主动检测网络状态
*/
func checkNetworkStatus() {
let reachability = Reachability.reachabilityForInternetConnection()// 准备获取网络连接信息
if reachability!.isReachable() {
// 判断网络连接状态
print("网络连接:可用")
if reachability!.isReachableViaWiFi() {// 判断网络连接类型
print("连接类型:WiFi")
} else
if reachability!.isReachableViaWWAN() {
print("连接类型:移动网络")
}
}else{
print("网络连接:不可用")
print("连接类型:没有网络连接")
}
}
// //移除通知
// func onButtonClicked(sender: AnyObject) {
// reachability!.stopNotifier()
// NSNotificationCenter.defaultCenter().removeObserver(
// self,
// name: ReachabilityChangedNotification,
// object: reachability
// )
// }
==================swift使用OC的reachalibity利用桥接来完成;
创建OC的LYBReachability.h和.m文件后会生成桥接文件FastappSwift-Bridging-Header.h;在这个桥接文件中引入
#import "LYBReachability.h";
*************
#import <Foundation/Foundation.h>
#import <Reachability/Reachability.h>
@interface LYBReachability :
NSObject
+ (BOOL)setNet;
@end
*************
#import "LYBReachability.h"
@implementation LYBReachability
+ (BOOL)setNet{
Reachability * re=[Reachability
reachabilityWithHostName:@"www.baidu.com"];
if ([re
currentReachabilityStatus] == ReachableViaWiFi || [re
currentReachabilityStatus] == ReachableViaWWAN) {
return YES;
}else{
return NO;
}
}
@end
****************
使用:
func Alamofire() {
if
LYBReachability.setNet(){
print("有网络")
} else{
return NO;
}
}
func monitorNet() {
let manager = NetworkReachabilityManager(host: "www.apple.com")
manager?.listener = { status in
print("网络状态: \(status)")
}
manager?.startListening()//开始监听网络
}
================Reachability.swift监控网络
下载地址https://github.com/ashleymills/Reachability.swift
var reachability:
Reachability?
var netWorkIsAvailable
func monitorNet(){
do {
reachability = try Reachability.reachabilityForInternetConnection()
} catch {
print("unable to create Reachability")
return
}
if reachability.isReachable() {
print("appDelegate:网络连接:可用")
netWorkIsAvailable = true
} else {
print("appDelegate:网络连接:不可用")
netWorkIsAvailable = false
}
if reachability!.isReachableViaWiFi() {
print("连接类型:WiFi")
} else if reachability!.isReachableViaWWAN() {
print("连接类型:移动网络")
} else {
print("连接类型:没有网络连接")
}
do {
try reachability?.startNotifier()//开始监测
} catch {
print("could not start reachability notifier")
}
}
// 网络可用或切换网络类型时执行
reachability.whenReachable = { reachability in
// 判断网络状态及类型
}
// 网络不可用时执行
reachability.whenUnreachable = { reachability in
// 判断网络状态及类型
}
// ======= ==== = ================
/**
通过消息自动监听网络改变
添加消息监听
*/
func NetworkStatusListener() {
// 1、设置网络状态消息监听
NSNotificationCenter.defaultCenter().addObserver(self, selector:"networkStatusChange", name: ReachabilityChangedNotification, object:nil);
// 2、获得网络Reachability对象
// Reachability必须一直存在,所以需要设置为全局变量
conn = Reachability.reachabilityForInternetConnection()!;
// 3、开启网络状态消息监听
conn!.startNotifier();
}
func networkStatusChange() {
checkNetworkStatus();
}
/**
移除消息通知
*/
deinit {
// 关闭网络状态消息监听
conn!.stopNotifier();
// 移除网络状态消息通知
NSNotificationCenter.defaultCenter().removeObserver(self);
}
/**
主动检测网络状态
*/
func checkNetworkStatus() {
let reachability = Reachability.reachabilityForInternetConnection()// 准备获取网络连接信息
if reachability!.isReachable() {
// 判断网络连接状态
print("网络连接:可用")
if reachability!.isReachableViaWiFi() {// 判断网络连接类型
print("连接类型:WiFi")
} else
if reachability!.isReachableViaWWAN() {
print("连接类型:移动网络")
}
}else{
print("网络连接:不可用")
print("连接类型:没有网络连接")
}
}
// //移除通知
// func onButtonClicked(sender: AnyObject) {
// reachability!.stopNotifier()
// NSNotificationCenter.defaultCenter().removeObserver(
// self,
// name: ReachabilityChangedNotification,
// object: reachability
// )
// }
==================swift使用OC的reachalibity利用桥接来完成;
创建OC的LYBReachability.h和.m文件后会生成桥接文件FastappSwift-Bridging-Header.h;在这个桥接文件中引入
#import "LYBReachability.h";
*************
#import <Foundation/Foundation.h>
#import <Reachability/Reachability.h>
@interface LYBReachability :
NSObject
+ (BOOL)setNet;
@end
*************
#import "LYBReachability.h"
@implementation LYBReachability
+ (BOOL)setNet{
Reachability * re=[Reachability
reachabilityWithHostName:@"www.baidu.com"];
if ([re
currentReachabilityStatus] == ReachableViaWiFi || [re
currentReachabilityStatus] == ReachableViaWWAN) {
return YES;
}else{
return NO;
}
}
@end
****************
使用:
func Alamofire() {
if
LYBReachability.setNet(){
print("有网络")
} else{
return NO;
}
}
相关文章推荐
- Swift - HTTP网络操作库Alamofire使用详解1(配置,以及数据请求)
- Swift-->Http网络请求(NSURLSession, Alamofire)
- iOS之Reachability和AFN判断网络连接状态
- [绍棠_Swift] Swift3.0中的Alamofire网络请求的封装
- swift开发之网络数据库(mysql)的增删改查(2)--引用Alamofire的post传值
- Swift学习记录 - Alamofire 4.0.1网络库基本使用(持续更新...)
- iOS之Reachability和AFN判断网络连接状态
- Swift - 如何用Reachability来混编判断网络状况
- iOS开发——Reachability和AFNetworking判断网络连接状态
- Swift 中的网络请求问题 OC(AFNetworking) && Swift(Alamofire)
- Swift - HTTP网络操作库Alamofire使用详解2(文件上传)
- [Swift 工作tips] 之 使用Alamofire做网络请求时设置请求超时(timeout)时间
- iOS之Reachability和AFN判断网络连接状态
- Alamofire源码解读系列(七)之网络监控(NetworkReachabilityManager)
- Swift 之 Alamofire 网络请求、取消请求
- iOS开发——Reachability和AFNetworking判断网络连接状态
- Swift:网络库Alamofire
- swift 的网络请求--Alamofire框架
- Swift - 使用Alamofire通过HTTPS进行网络请求,及证书的使用