ChenYilong / iOS13AdaptationTips

iOS13 AdaptationTips
MIT License
175 stars 6 forks source link

Network #24

Open ChenYilong opened 5 years ago

ChenYilong commented 5 years ago

原文链接: https://github.com/ChenYilong/iOS13AdaptationTips/issues/24

CNCopyCurrentNetworkInfo() does NOT work on iOS13

2019August08-推送:

已经有哥们儿 work around了: Fix CNCopyCurrentNetworkInfo() does NOT work on iOS13

评价:

work around方案获取 wift信息还要先位置授权。 BSSID是无线AP的唯一编码,这玩意拿到了跟定位没啥区别。

但一般用户的感知可能正好相反。 拿我Wi-Fi可以,别拿我的坐标。

那没有的获取wifi,后面其他方案的定位准么?

目前看只能是室外GPS定位了。

相关讨论

iTeaTime(技术清谈)@张二板-iOS-南京: A: iOS要想拿到附近的wifi列表要向苹果爸爸申请,不申请的话,只能拿到当前手机连接的那个wifi信息。

Q:就是安卓可以直接获取啊 iOS要根据uuid 安卓可以直接获取到附近的wifi列表并且拿到BSSID(MAC地址)

A: 我去年遇到这个问题的,然后主动帮产品经理设计了一版适合iOS的原型:

DargonLee commented 5 years ago

@Harlans 的实现:

oc版本的


#import "WIFIViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <SystemConfiguration/CaptiveNetwork.h>

@interface WIFIViewController ()<CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) NSArray *currentNetworkInfos;
@property (weak, nonatomic) IBOutlet UILabel *ssidLabel;
@property (weak, nonatomic) IBOutlet UILabel *bssidLabel;

@end

@implementation WIFIViewController

- (NSArray *)currentNetworkInfos
{
    if (!_currentNetworkInfos) {

        _currentNetworkInfos = [NSArray arrayWithArray:[SSID fetchNetwrokInfo]];
    }
    return _currentNetworkInfos;
}

- (CLLocationManager *)locationManager
{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc]init];
    }
    return _locationManager;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (@available(iOS 13.0, *)) {
        CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
        if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
            [self updateWIFI];
        }else {
            self.locationManager.delegate = self;
            [self.locationManager requestWhenInUseAuthorization];
        }
    }else {
        [self updateWIFI];
    }
}

- (void)updateWIFI
{
    NetworkInfo *info = [self.currentNetworkInfos firstObject];
    self.ssidLabel.text = info.ssid;
    self.bssidLabel.text = info.bssid;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self updateWIFI];
    }
}

@end

@implementation SSID

+ (NSArray <NetworkInfo *>*)fetchNetwrokInfo
{
    NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces());
    NSMutableArray *networkInfos = [NSMutableArray array];
    for (NSString *interface in interfaces) {
         NSDictionary *info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interface);
        NetworkInfo *networkInfo = [[NetworkInfo alloc]initWith:interface success:YES ssid:@"" bssid:@""];
        NSString *ssid = (__bridge NSString *)kCNNetworkInfoKeySSID;
        NSString *bssid = (__bridge NSString *)kCNNetworkInfoKeyBSSID;
        networkInfo.ssid = info[ssid];
        networkInfo.bssid = info[bssid];
        [networkInfos addObject:networkInfo];
    }
    return networkInfos;
}

@end

@implementation NetworkInfo

- (instancetype)initWith:(NSString *)interface success:(BOOL)suc ssid:(NSString *)ssid bssid:(NSString *)bssid
{
    if (self == [super init]) {
        self.interface = interface;
        self.success = suc;
        self.ssid = ssid;
        self.bssid = bssid;
    }
    return self;
}

@end
luzsyn commented 5 years ago

你好,请问已经获取了到定位权限,iOS13之前都是可以正常获取到mac地址的,有几个用户更新了iOS13系统以后就无法获取到mac地址了,您知道是什么原因吗?