iOS Core notes-network programming-monitoring network status

Source: Internet
Author: User

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

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.