iPhone13 and above get wifi name 2

iPhone13 and above get wifi name 2

1. The application scenario is generally: connecting to hardware devices, software is required to obtain the WiFi name, and after manually entering the password, perform hardware network configuration operations.

1. Enter the Developer Center, under Identifiers, and check Access WiFi Information in Capabilities. As shown below:

Insert image description here

2. Add permission to obtain WiFi information in xcode. As shown below:
Insert image description here
3. Turn on positioning: After iOS13, to obtain the WiFi name, you need to turn on positioning first.

1) The info.plist file needs to be configured with the permissions
Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description

4. After ios13 After
upgrading to iOS13, I found that the previous interface for obtaining the WiFi name failed, and the fixed value "WLAN" was returned. This may be due to Apple's privacy protection issues for users, because the user's geographical location can be located through wifi information. Therefore, if you want to continue to obtain the WiFi name after iOS13, you need to determine whether the user agrees to the app's use of geographical location information before calling the interface.

1. Add positioning library

Insert image description here
2.2. Configure in the Info.plist file. I have matched all four positioning parameters.

Insert image description here
5.添加头文件
#import <CoreLocation/CoreLocation.h>
#import <NetworkExtension/NetworkExtension.h>
#import <CoreLocation/CLLocationManager.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <SystemConfiguration/CaptiveNetwork.h>

6. Add positioning management agent
Insert image description here
@interface ViewControllerAccount_setup: UIViewController<CBCentralManagerDelegate, CBPeripheralDelegate,CLLocationManagerDelegate>

7. Add variables of CLLocationManager

@property (strong, nonatomic) CLLocationManager *locationmanager;

#pragma mark - Locate authorization proxy method

  • (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse ||
    status == kCLAuthorizationStatusAuthorizedAlways) {
    //再重新获取ssid
    [self getSSID];
    }
    }

  • (void)getLocation
    {
    if (!self.locationmanager) {
    self.locationmanager = [[CLLocationManager alloc] init];

    }

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //This code will turn on the location authorization option in the app settings. Only if the user chooses to allow it once, the next time the user calls this method, a question will pop up. box, select not allowed or allowed during use, the next time you call this method, the inquiry box will not pop up [self.locationmanager requestAlwaysAuthorization];

    }

    self.locationmanager.delegate = self;
    //If the user refuses for the first time, a prompt box will pop up and jump to the settings interface, asking the user to turn on location permissions
    //If the user jumps to the settings interface and chooses to ask next time, then return to the app , the value of [CLLocationManager authorizationStatus] will be nil, so || the subsequent judgment
    if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || ![CLLocationManager authorizationStatus]) { [self alertMy]; } }


  • (void)alertMy{ //1. Create UIAlertControler UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Prompt" message:@"The app needs to obtain your location permission to obtain wifi information and configure the network for the robot" preferredStyle:UIAlertControllerStyleAlert];

     UIAlertAction *conform = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         //使用下面接口可以打开当前应用的设置页面
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
         NSLog(@"点击了确认按钮");
     }];
     //2.2 取消按钮
     UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
         NSLog(@"点击了取消按钮");
     }];
    
     //3.将动作按钮 添加到控制器中
     [alert addAction:conform];
     [alert addAction:cancel];
     
     //4.显示弹框
     [self presentViewController:alert animated:YES completion:nil];
    

}

  • (void)getSSID{
    NSString *ssidStr = [ViewControllerAccount_setup fetchSSIDInfo][@“SSID”];

    NSLog(@“ssidStr=%@”,ssidStr);

}

  • (id)fetchSSIDInfo {

    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString *ifnam in ifs) {
    info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);

      if (info && [info count]) {
          break;
      }
    

    }
    return info;
    }

Insert image description here

Insert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/wangyuhong2267/article/details/132900467