1. 問(wèn)題
在 iOS14 中如果不允許精確定位,那么就獲取不到WiFi的名稱(chēng)了屹逛。
我的項(xiàng)目中獲取WiFi名稱(chēng)的方式:
+ (NSString *)wifiName {
NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces());
id info = nil;
for (NSString *interfaceName in interfaces) {
info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef)interfaceName);
if (info) {
break;
}
}
NSDictionary *infoDic = (NSDictionary *)info;
NSString *ssid = [infoDic objectForKey:@"SSID"]; // WiFi的名稱(chēng)
NSString *bssid = [infoDic objectForKey:@"BSSID"]; // WiFi的mac地址
NSLog(@"WiFi SSID = %@, MAC = %@", ssid, bssid);
return ssid;
}
2. 解決
不允許精確定位就獲取不到WiFi名稱(chēng)疾渣,那么只有在獲取WiFi名稱(chēng)的時(shí)候向用戶(hù)申請(qǐng)臨時(shí)開(kāi)啟一次精確位置權(quán)限了拴魄。
???????? Demo地址:LocationDemo
2.1 申請(qǐng)權(quán)限需要調(diào)用- (void)requestTemporaryFullAccuracyAuthorizationWithPurposeKey:(NSString *)purposeKey
方法冗茸。
??:該方法起作用的前提是用戶(hù)已經(jīng)允許了定位席镀,即授權(quán)狀態(tài)不是 kCLAuthorizationStatusNotDetermined
或 kCLAuthorizationStatusRestricted
或 kCLAuthorizationStatusDenied
。
2.2 在申請(qǐng)臨時(shí)開(kāi)啟一次精確位置權(quán)限前夏漱,需要在Info.plist中進(jìn)行配置豪诲。
2.2.1 Info.plist 中的配置
- 添加項(xiàng)目:
NSLocationTemporaryUsageDescriptionDictionary
,即:Privacy - Location Temporary Usage Description Dictionary
麻蹋;
在以上項(xiàng)目中添加鍵值對(duì):PurposeKey : 說(shuō)明描述
跛溉,針對(duì)不同的使用場(chǎng)景焊切,可以添加多個(gè)鍵值對(duì)扮授。例如:
WantsToGetWiFiSSID : Your precise location will be used to get the current Wi-Fi SSID.
WantsToNavigate : Your precise location will be used to calculate a route and allow you to use turn-by-turn directions.
2.2.2 說(shuō)明描述的國(guó)際化
- 需要在
InfoPlist.strings
中添加PurposeKey
對(duì)應(yīng)的國(guó)際化內(nèi)容,例如:
WantsToGetWiFiSSID = "Your precise location will be used to get the current Wi-Fi SSID.";
- 如果未提供國(guó)際化內(nèi)容专肪,則使用
Info.plist
中的說(shuō)明描述刹勃;
2.3 代碼實(shí)現(xiàn)
1?? 申請(qǐng)臨時(shí)開(kāi)啟一次精確位置權(quán)限:
if (@available(iOS 14.0, *)) {
BOOL isFullAccuracy = self.locationManager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy;
if (!isFullAccuracy) {
// 向用戶(hù)申請(qǐng)臨時(shí)開(kāi)啟一次精確位置權(quán)限
[self.locationManager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"WantsToGetWiFiSSID"];
}
}
2?? 在授權(quán)狀態(tài)改變的代理方法中再次獲取WiFi名稱(chēng),更新WiFi名稱(chēng)的顯示:
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager API_AVAILABLE(ios(14.0)) {
// 1. 定位權(quán)限狀態(tài)
CLAuthorizationStatus status = manager.authorizationStatus;
// 2. 精確定位權(quán)限狀態(tài)
CLAccuracyAuthorization accuracyStatus = manager.accuracyAuthorization;
if (accuracyStatus == CLAccuracyAuthorizationFullAccuracy) {
NSLog(@"精確定位已開(kāi)啟");
} else {
NSLog(@"精確定位未開(kāi)啟");
}
NSString *wifiName = [ViewController wifiName];
self.nameLabel.text = [NSString stringWithFormat:@"WiFi名稱(chēng): %@", wifiName];
}
2.4 臨時(shí)獲取的精確位置權(quán)限說(shuō)明
2.4.1 權(quán)限會(huì)自動(dòng)過(guò)期嚎尤,當(dāng)用戶(hù)在使用App時(shí)(如App在前臺(tái))不會(huì)過(guò)期荔仁。
我的測(cè)試情況:
- 前臺(tái) --> 后臺(tái)(大概10s內(nèi)) --> 前臺(tái),不會(huì)過(guò)期芽死;
- 前臺(tái) --> 后臺(tái)(超過(guò)大概10s) --> 前臺(tái)乏梁,過(guò)期;
2.4.2 App開(kāi)啟了持續(xù)的后臺(tái)定位关贵,不會(huì)過(guò)期遇骑。
- ??? 添加后臺(tái)定位的配置
在Xcode的Capability
中配置Background Modes
的Location updates
。
With
UIBackgroundModes
set to include "location" inInfo.plist
, you must also set this property to YES at runtime whenever calling -startUpdatingLocation with the intent to continue in the background.
- ??? 允許后臺(tái)定位:
self.locationManager.allowsBackgroundLocationUpdates = YES;
重要提示:Setting this property to YES when UIBackgroundModes does not include "location" is a fatal error.即:允許后臺(tái)定位的前提是在
Capability
中配置了Background Modes
的Location updates
揖曾,否則會(huì)產(chǎn)生致命錯(cuò)誤落萎。
如果已在Capability
中配置好了,但未允許后臺(tái)定位炭剪,則不會(huì)在后臺(tái)進(jìn)行定位练链,也不會(huì)產(chǎn)生錯(cuò)誤。
3. iOS14 CLLocationManager 類(lèi)中棄用的方法
- 類(lèi)方法
[CLLocationManager authorizationStatus]
已棄用奴拦,需調(diào)用實(shí)例屬性:self.locationManager.authorizationStatus
- 授權(quán)狀態(tài)改變的代理方法的棄用:
locationManager:didChangeAuthorizationStatus:
-->locationManagerDidChangeAuthorization:
/*
* locationManager:didChangeAuthorizationStatus:
*
* Discussion:
* Invoked when the authorization status changes for this application.
*/
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status API_DEPRECATED_WITH_REPLACEMENT("-locationManagerDidChangeAuthorization:", ios(4.2, 14.0), macos(10.7, 11.0), watchos(1.0, 7.0), tvos(9.0, 14.0));
/*
* locationManagerDidChangeAuthorization:
*
* Discussion:
* Invoked when either the authorizationStatus or
* accuracyAuthorization properties change
*/
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager API_AVAILABLE(ios(14.0), macos(11.0), watchos(7.0), tvos(14.0));
4. 定位權(quán)限說(shuō)明
高德開(kāi)放平臺(tái)的文檔定位權(quán)限說(shuō)明 中詳細(xì)描述了 iOS 8.0 - iOS 14.0 前臺(tái)定位與后臺(tái)定位的配置情況媒鼓。
參考鏈接,非喜勿噴错妖,歡迎點(diǎn)贊留言~