美文网首页
iOS WIFI直连

iOS WIFI直连

作者: LV大树 | 来源:发表于2024-04-08 13:51 被阅读0次

如下:实现的功能,是获取手机连接的wifi Name。


#import "ViewController.h"
#import <Network/Network.h>
#import <NetworkExtension/NetworkExtension.h>
#import <CFNetwork/CFNetwork.h>
#import<SystemConfiguration/CaptiveNetwork.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController ()<CLLocationManagerDelegate>

@end

@implementation ViewController
-(void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
    NSLog(@"ss");
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"%@",locations);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CLLocationManager *manager = [[CLLocationManager alloc]init];
    [manager requestWhenInUseAuthorization];
    
    [manager requestAlwaysAuthorization];
    
    manager.delegate = self;
    [manager startUpdatingLocation];
    
    NSString *wifiName = [self getCurrentWifi];
    NSLog(@"wifiName==%@",wifiName);
    
    NSString *pass = @"kelly1533summer@@";
    NEHotspotConfiguration* configuration = [[NEHotspotConfiguration alloc]initWithSSID:wifiName passphrase:pass isWEP:NO];

    
    [[NEHotspotConfigurationManager sharedManager] getConfiguredSSIDsWithCompletionHandler:^(NSArray<NSString *> * _Nonnull currentssids) {
        NSLog(@"currentssids%@",currentssids);
        
    }];
    
    [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
        NSLog(@"error:%@",error);
    }];
     
}
-(NSString*)getCurrentWifi{
    
    NSString* wifiName =@"";
    
    CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
    
    if(!wifiInterfaces) {
        
        wifiName =@"";
        
    }
    
    NSArray*interfaces = (__bridge NSArray*)wifiInterfaces;
    
    for(NSString*interfaceName in interfaces) {
        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
        
        if(dictRef) {
            
            NSDictionary*networkInfo = (__bridge NSDictionary*)dictRef;
            
            wifiName = [networkInfo objectForKey:(__bridge NSString*)kCNNetworkInfoKeySSID];
            
            CFRelease(dictRef);
            
        }
        
    }
    
    CFRelease(wifiInterfaces);
    
    return wifiName;
    
}
 

@end

前提是:必须开启定位。否则会报错的。

相关文章

网友评论

      本文标题:iOS WIFI直连

      本文链接:https://www.haomeiwen.com/subject/xkhxxjtx.html