var zeroAddress = sockaddr_in()
@objc(Reachability) //宣告給Object C 看的
public class Reachability:NSObject {
func method()->Bool {
print("TESTSTSTET")
return false
}
func method2()->Bool { //因為ObjectC 看不到class fun 所以要加上此層包裝
return Reachability.isConnected() //靜態呼叫 尚未驗證是否有錯誤
}
//檢查是否有連線
class func isConnected()->Bool {
// var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}
var flags = SCNetworkReachabilityFlags()
if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
return false
}
let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
return (isReachable && !needsConnection)
}
}
****.m
#import <WIFI_TEST-Swift.h> //系統自己產生 設定記得要改
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
@implementation NEHotspotNetwork (loadSwfit)
-(void )test
{
Reachability* reachData = [[Reachability alloc] init];
//
[reachData method ];
[reachData method2 ];
}
@end
沒有留言:
張貼留言