A recent project is frequently performed on the network, but it is sometimes necessary to determine the network status and the type of the current network. See a lot of seniors are using
Reachability to achieve. I also tried to test the effect is good, packaged into a tool class for everyone to use.
Instructions for use:
1. Add the framework:
Add systemconfiguration.framework into the project.
2. Download Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip
Copy the inside of the Reachability.h and reachability.m into the project
3. Add CloNetworkUtil.h and CLONETWORKUITL.M to your project
Import the CloNetworkUtil.h header file where you use it to
CloNetworkUtil.h Code:
View Sourceprint?01.//02.// CloNetworkUtil.h03.// NetWorkTest04.//05.// Created by Cloay on 12-6-19.06.// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.07./**08.1.添加framework:09.将SystemConfiguration.framework 添加进工程。10.2.下载 https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip11.复制里面的Reachability.h和Reachability.m到项目中12.*/13. 14.#import <Foundation/Foundation.h>15.#import "Reachability.h"16.@interface CloNetworkUtil : NSObject{17.}18. 19. 20.- (Reachability *)initReachability;21.- (BOOL)getNetWorkStatus;22.- (NSString *)getNetWorkType;23.@end
The implementation is as follows:
View Sourceprint?01.//02.// CloNetworkUtil.m03.// NetWorkTest04.//05.// Created by Cloay on 12-6-19.06.// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.07.//08. 09.#import "CloNetworkUtil.h"10. 11.@implementation CloNetworkUtil12. 13.//初始化reachability14.- (Reachability *)initReachability{15.Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];16.return reachability;17.}18. 19.//判断网络是否可用20.- (BOOL)getNetWorkStatus{21.if ([[self initReachability] currentReachabilityStatus] == NotReachable) {22.return NO;23.}else {24.return YES;25.}26.}27. 28./**29.获取网络类型30.return31.*/32.- (NSString *)getNetWorkType33.{34.NSString *netWorkType;35.Reachability *reachability = [self initReachability];36.switch ([reachability currentReachabilityStatus]) {37.case ReachableViaWiFi: //Wifi网络38.netWorkType = @"wifi";39.break;40.case ReachableViaWWAN: //无线广域网41.netWorkType = @"wwan";42.break;43.default:44.netWorkType = @"no";45.break;46.}47.return netWorkType;48.}49. 50.@end
It's simply not a description! :
If you have any questions, please leave a message and learn together!
iOS Development Network Chapter-Network check