iOS Dev-Detect network status (WiFi, 2g/3g/4g)

Source: Internet
Author: User

Detecting Network status

In the network application, the network status of the user device needs to be monitored in real time to
Let users know their network status, to prevent some misunderstandings (such as the application of incompetence)
Intelligent processing based on the user's network status, saving user traffic and improving the user experience
WIFI\3G network: Automatically download HD images
Low-speed network: Download thumbnails only
No network: Show only cached data that is offline

Apple officially provides a sample program called Reachability, which allows developers to detect network status
Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

Reachability Steps to use
Add Frame Systemconfiguration.framework

Add source code

Include header file

#import "Reachability.h"
Extracting tool Classes
常见用法// 是否WIFI+ (BOOL) IsEnableWIFI {    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL) IsEnable3G {    return ([[Reachability reachabilityForInternetConnectionen] currentReachabilityStatus] != NotReachable);}
Network Monitoring
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selectornil];self.netReachability = [Reachability reachabilityForInternetConnection];[self.netReachability startNotifier];- (void)dealloc{    [self.netReachability stopNotifier];    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];}
Detecting Network Status instances

Networktool Tool Class

#import <Foundation/Foundation.h>@interface NetworkTool : NSObject/** *  是否WIFI */+ (BOOL)isEnableWIFI;/** *  是否3G */+ (BOOL)isEnable3G;@end
#import "NetWorkTool.h"#import "Reachability.h"@implementation NetworkTool// 是否WIFI+ (BOOL)isEnableWIFI {    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);}// 是否3G+ (BOOL)isEnable3G {    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);}@end

Implementation class

#import "ViewController.h" #import "Reachability.h" #import "NetworkTool.h"  @interface viewcontroller ()@property(nonatomic,Strong) reachability *reachability;@end @implementation viewcontroller - (void) viewdidload{[SuperViewdidload];//Listen for notification of changes in network status[[NsnotificationcenterDefaultcenter] Addobserver: SelfSelector@selector(Networkstatechange) Name:kreachabilitychangednotification object:Nil];//Get Reachability object     Self. reachability= [reachability reachabilityforinternetconnection];//Start monitoring network[ Self. reachabilityStartnotifier];} - (void) dealloc{[ Self. reachabilityStopnotifier]; [[NsnotificationcenterDefaultcenter] Removeobserver: Self];} - (void) networkstatechange{NSLog(@"The network state has changed"); [ SelfChecknetworkstate];} - (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{[ SelfChecknetworkstate];}
#pragma mark-app Application network type changes will be called- (void) networkstatechange{if([Networktool Isenablewifi]) {Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Friendly and helpful"message:@"You now have a network environment of wifi! Start enjoying it! "Delegate Selfcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil];    [Alert show]; }Else if([Networktool isenable3g]) {Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Friendly and helpful"message:@"You now network environment for 3G/4G Network!" "Delegate Selfcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil];    [Alert show]; }Else{Uialertview *alert = [[Uialertview alloc] initwithtitle:@"Friendly and helpful"message:@"You don't currently have a network to connect to or have dropped!" "Delegate Selfcancelbuttontitle:@"OK"Otherbuttontitles:Nil,Nil];    [Alert show]; }}

iOS Dev-Detect network status (WiFi, 2g/3g/4g)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.