Latest 0.0.2
Homepage https://github.com/duanruiying/DryReachability
License MIT
Platforms ios 10.0, requires ARC
Dependencies ReachabilitySwift
Frameworks UIKit, Foundation, SystemConfiguration
Authors

iOS: 网络状态监控

Prerequisites

  • Xcode 10.2.1
  • iOS 9.0 or later
  • ObjC、Swift 5 or later

Installation

  • pod ‘DryReachability’

Features

/// 监控App蜂窝联网权限
DryReachability.restrictedStatus { (state) in
    print("App: (state.rawValue)")
}

/// 获取设备网络状态
let ss = DryReachability.currentReachabilityStatus()
print("current device: (ss.rawValue)")

/// 监控设备网络状态
DryReachability.reachabilityStatus { (state) in
    print("device: (state.rawValue)")
}

/// 获取设备当前连接WiFi的SSID
if let ssid = DryReachability.ssid() {
    print("ssid: (ssid)")
}

/// 获取设备当前连接WiFi的IP地址
if let address = DryReachability.getwayAddress() {
    print("address: (address)")
}
App处于后台时,需要申请后台时间,才能实时监控网络状态:
==================================Object C==================================
(1)在AppDelegate创建属性:
@interface AppDelegate ()
@property (assign) UIBackgroundTaskIdentifier bgTaskID;//后台任务时间申请
@end

(2)在applicationDidEnterBackground中申请:
- (void)applicationDidEnterBackground:(UIApplication *)application {

    /// 申请后台运行时间
    __weak typeof(self) weakSelf = self;
    self.bgTaskID = [application beginBackgroundTaskWithExpirationHandler:^{
        /// 后台运行时间超时回调(达到系统分配时间上限)
        [application endBackgroundTask:weakSelf.bgTaskID];
        weakSelf.bgTaskID = UIBackgroundTaskInvalid;
    }];

    /// 申请后台运行时间失败
    if (self.bgTaskID == UIBackgroundTaskInvalid) {
        return;
    }
}

==================================Swift===================================
(1)在AppDelegate创建属性
class AppDelegate: UIResponder {
    var bgTaskID: UIBackgroundTaskIdentifier?//后台任务时间申请
}

(2)在applicationDidEnterBackground中申请
func applicationDidEnterBackground(_ application: UIApplication) {

    /// 申请后台运行时间
    weak var weakSelf = self
    self.bgTaskID = application.beginBackgroundTask(expirationHandler: {
        /// 后台运行时间超时回调(达到系统分配时间上限)
        if weakSelf != nil && weakSelf!.bgTaskID != nil {
            application.endBackgroundTask(weakSelf!.bgTaskID!)
            weakSelf!.bgTaskID = UIBackgroundTaskIdentifier.invalid
        }
    })

    /// 申请后台运行时间失败
    if self.bgTaskID == UIBackgroundTaskIdentifier.invalid {
        return
    }
}

Latest podspec

{
    "name": "DryReachability",
    "version": "0.0.2",
    "summary": "DryReachability",
    "homepage": "https://github.com/duanruiying/DryReachability",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "duanruiying": "[email protected]"
    },
    "source": {
        "git": "https://github.com/duanruiying/DryReachability.git",
        "tag": "0.0.2"
    },
    "description": "TODO: iOSu7f51u7edcu72b6u6001u76d1u63a7.",
    "swift_versions": "5",
    "platforms": {
        "ios": "10.0"
    },
    "requires_arc": true,
    "user_target_xcconfig": {
        "OTHER_LDFLAGS": [
            "-w"
        ]
    },
    "pod_target_xcconfig": {
        "OTHER_LDFLAGS": [
            "-w"
        ]
    },
    "source_files": "DryReachability/Classes/Code/**/*",
    "frameworks": [
        "UIKit",
        "Foundation",
        "SystemConfiguration"
    ],
    "dependencies": {
        "ReachabilitySwift": [
            "4.3.1"
        ]
    },
    "swift_version": "5"
}

Pin It on Pinterest

Share This