iOS obtains the information of the Wi-Fi connected to the current mobile phone

Because after iOS14, the location permission must be turned on when obtaining Wi-Fi information, otherwise the Wi-Fi information cannot be obtained. I have been looking for this pit for a long time. Hope this helps fellow Taoists who have the same problem.

Be sure to add this key-value pair in the plist file, otherwise you still cannot get Wi-Fi information

 

    if (@available(iOS 14.0, *)) {

        BOOL isFullAccuracy = self.localmanager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy;

            if (!isFullAccuracy) {

                // Apply to the user to temporarily enable the precise location permission once

                [self.localmanager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"WantsToGetWiFiSSID"];

            }

        [NEHotspotNetwork fetchCurrentWithCompletionHandler:^(NEHotspotNetwork * _Nullable currentNetwork) {

            self.detail.ssid = currentNetwork.SSID;

            self.detail.mac = currentNetwork.BSSID;

            self.nameLb.text = currentNetwork.SSID;

        }];

    }else{

        //Get the currently connected Wi-Fi data

         CFArrayRef myArray = CNCopySupportedInterfaces();

         if(myArray !=nil) {

             CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));

             if(myDict !=nil) {

                 NSDictionary*dict = (NSDictionary*)CFBridgingRelease(myDict);

         NSLog(@"wifi information%@", dict);

                ssid = [dict valueForKey:@"SSID"];

         self.detail.ssid = ssid;

         self.nameLb.text = ssid;

         self.detail.mac = [dict valueForKey:@"BSSID"];

   

             }

         }

    }

Guess you like

Origin blog.csdn.net/ForeverMyheart/article/details/113887290