iOS13 获取已连接的WIFI名称
近期在开发一个物联网APP时,有一个获取手机连接的WIFI名称并广播给物联网设备的功能.在iOS12时并不需要定位功能,但是到了iOS13上需要开启定位功能才可以获取到WIFI名称.
设置info.plist
1 2 3 4 5 6 7 8
| <key>NSLocationWhenInUseUsageDescription</key> <string>App需要您的同意,才能在使用期间访问位置</string> <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>App需要您的同意,才能始终访问位置</string> <key>NSLocationUsageDescription</key> <string>App需要您的同意,才能访问位置</string> <key>NSLocationAlwaysUsageDescription</key> <string>App需要您的同意,才能始终访问位置</string>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #import <SystemConfiguration/CaptiveNetwork.h> #import <CoreLocation/CoreLocation.h> #import "VC.h"
@interface VC () <CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; @end @implementation VC - (void)viewDidLoad { [super viewDidLoad]; NSDictionary *ifs = [self fetchSSIDInfo]; NSLog(@"WIFI:%@", [ifs objectForKey:@"SSID"]); if(@available(iOS 13.0, *)) { [self startLocation]; } } /** 注销监听器 */ -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(id)fetchSSIDInfo { NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); NSLog(@"Supported interfaces: %@", ifs); id info = nil; for (NSString * ifnam in ifs) { info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); NSLog(@"%@ => %@", ifnam, info); if (info && [info count]) { break; } }
return info; } //开始定位 - (void)startLocation { if ([CLLocationManager locationServicesEnabled]) { self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; [self.locationManager requestAlwaysAuthorization]; self.locationManager.distanceFilter = 10.0f; [self.locationManager requestAlwaysAuthorization]; [self.locationManager startUpdatingLocation]; } }
- (void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error { if ([error code] == kCLErrorDenied) { NSLog(@"访问被拒绝"); } if ([error code] == kCLErrorLocationUnknown) { NSLog(@"?法获取位置信息"); } } //定位代理经纬度回调 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations: (NSArray<CLLocation *> *)locations { CLLocation *newLocation = locations[0]; // 获取当前所在的城市名 CLGeocoder *geocoder = [[CLGeocoder alloc] init]; //根据经纬度反向地理编译出地址信息 [geocoder reverseGeocodeLocation:newLocation completionHandler: ^ (NSArray * array, NSError * error) { if (array.count > 0) { CLPlacemark *placemark = [array objectAtIndex:0]; NSLog(@"%@", placemark); NSDictionary *ifs = [self fetchSSIDInfo]; NSLog(@"WIFI:%@", [ifs objectForKey:@"SSID"]); } else if (error == nil && [array count] == 0) { NSLog(@"No results were returned."); } else if (error != nil) { NSLog(@"An error occurred = %@", error); } }]; [manager stopUpdatingLocation]; }
@end
|