iOS判斷當前是否有網路

2021-06-22 17:37:23 字數 997 閱讀 2816

[**]:

在對應的.h檔案中加入宣告:

[cpp]view plain

copy

print?

- (bool

) connectedtonetwork;  

如果方法前面加+,就相當於類的靜態方法,這裡要注意一下

在.m中加入定義:

[cpp]view plain

copy

print?

-(bool

) connectedtonetwork  

bool

isreachable = ((flags & kscnetworkflagsreachable) != 0);  

bool

needsconnection = ((flags & kscnetworkflagsconnectionrequired) != 0);  

return

(isreachable && !needsconnection) ? yes : no;  

}  

在呼叫的時候,在對應的按鈕中加入判斷:

[cpp]view plain

copy

print?

- (ibaction)testout:(uibutton *)sender  

nslog(@"testout event"

);  

}  

加入對應的標頭檔案:

判斷當前網路是否可用

有兩種方式 方式一,是否有網路 所有網路 mobile wifi public boolean isnetworkconnected 第二種方式,是否有指定的網路 檢查wifi是否可用的兩種方式 wifimanager wifimanager wifimanager getsystemservice...

iOS 判斷當前網路狀態

1.如果只判斷當前是否是無網的狀態 if reachability reachabilityforlocalwifi currentreachabilitystatus notreachable reachability reachabilityforinternetconnection curre...

iOS開發判斷當前的網路狀態

在實際開發過程中經常需要判斷應用當前的網路狀態 wifi,4g,無網路 這裡需要用到乙個第三方框架afnetworking 2,引用標頭檔案,使用cocoapod需要用 import import afnetworking afnetworking.h 3,除錯 pragma mark 網路狀態監測...