1, AFN monitoring network status:
1.- (void)afnMonitoring
2.{
3. // 开始网络监控
4. AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
5.
6. [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
7. NSLog(@"-----当前的网络状态---%zd", status);
8. }];
9.
10. [mgr startMonitoring];
11.
12. // 拿到当前网络状态
13.// mgr.networkReachabilityStatus;
14.}
?备注
: enum {
// 未知网络
Afnetworkreachabilitystatusunknown,
// 无网络连接
Afnetworkreachabilitystatusnotreachable,
// 手机自带网络(3G | 4G)
Afnetworkreachabilitystatusreachableviawwan,
// 无线网络
Afnetworkreachabilitystatusreachableviawifi,
}
2, Apple official monitoring network status:
1.-(void) applemonitoring
2.{
3.//monitoring Notification
4. [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (getnetworkstatus) Name: Kreachabilitychangednotification Object:nil];
5.
6. Start monitoring network
7. self.reachability = [reachability reachabilityforinternetconnection];
8. [Self.reachability Startnotifier];
9.}
.
11.-(void) Dealloc
12.{
[[Nsnotificationcenter Defaultcenter] removeobserver:self];
.
15. [Self.reachability Stopnotifier];
Self.reachability = nil;
.}
.
19.-(void) Getnetworkstatus
20.{
. if ([reachability reachabilityforlocalwifi].currentreachabilitystatus! = notreachable) {
22. NSLog (@ "WiFi");
.} else if ([reachability reachabilityforinternetconnection].currentreachabilitystatus! = notreachable) {
24 . NSLog (@ "is the mobile phone comes with the network");
.} else {
26. NSLog (@ "network problem");
.}
28.}
.
?重要
:
iOS Core notes-network programming-monitoring network status