- 只能拿到當前連線 Wi-Fi 資訊
- 使用 private API
最後終於找到在 iOS 9 可以使用 NEHotspotHelper,但有一些限制:
- iOS 9+ 支援
- 需要向 Apple 申請 com.apple.developer.networking.HotspotHelper entitlement 才可使用
- 只能被動由系統通知 Wi-Fi 清單,無法主動取得
大概寫一下流程,希望有空的時候可以補齊
前置步驟比較瑣碎
- 首先到 https://developer.apple.com/contact/network-extension 填寫申請書
- 在開發者網頁中,將 App ID 啟用 Network Extensions
- 更新 Provisioning Profiles,並且選擇使用 Network Extension iOS Entilements
- XCode 更新 Provisioning Profiles
- Info.plist 檔案中在 UIBackgroundModes 加入 network-authentication
- entitlements 檔案中加入 com.apple.developer.networking.HotspotHelper 為 YES
程式碼:
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:@"xxx" forKey:kNEHotspotHelperOptionDisplayName];
dispatch_queue_t queue = dispatch_queue_create("xxx", 0);
BOOL ret = [NEHotspotHelper registerWithOptions:options queue:queue handler:^(NEHotspotHelperCommand *cmd) {
NEHotspotNetwork *network;
if (cmd.commandType == kNEHotspotHelperCommandTypeFilterScanList) {
for (network in cmd.networkList) {
NSString *wifiInfo = [[NSString alloc] initWithFormat:@"SSID: [%@] MAC: [%@] strength: [%f]", network.SSID, network.BSSID, network.signalStrength];
NSLog(@"%@", wifiInfo);
}
}
}];
NSLog(@"return value %d", ret);
確認 ret 為 True 表示有註冊成功,可到設定 -> Wi-Fi,觸發 Wi-Fi 清單更新後就會印 Log 到 console 上了。
參考資料
沒有留言:
張貼留言