iOS WiFi 相关知识点总结

iOS 跳转到系统设置里面的WiFi列表页

参考:segmentfault

  1. iOS 7, 8, 9
   NSURL *url = [NSURL URLWithString:@"prefs:root=WIFI"];
   if ([[UIApplication sharedApplication] canOpenURL:url]) 
   {
        [[UIApplication sharedApplication] openURL:url];
   }
  1. iOS 10
//iOS10
NSString * defaultWork = [self getDefaultWork];
NSString * wifiMethod = [self getWIFIhMethod];
NSURL*url=[NSURL URLWithString:@"Prefs:root=WIFI"];
Class LSApplicationWorkspace = NSClassFromString(@"LSApplicationWorkspace");
[[LSApplicationWorkspace performSelector:NSSelectorFromString(defaultWork)] performSelector:NSSelectorFromString(bluetoothMethod) withObject:url withObject:nil];

-(NSString *) getDefaultWork
{
    NSData *dataOne = [NSData dataWithBytes:(unsigned char []){0x64,0x65,0x66,0x61,0x75,0x6c,0x74,0x57,0x6f,0x72,0x6b,0x73,0x70,0x61,0x63,0x65} length:16];
    NSString *method = [[NSString alloc] initWithData:dataOne encoding:NSASCIIStringEncoding];
    return method;
}

-(NSString *) getWIFIhMethod
{
    NSData *dataOne = [NSData dataWithBytes:(unsigned char []){0x6f, 0x70, 0x65, 0x6e, 0x53, 0x65, 0x6e, 0x73, 0x69,0x74, 0x69,0x76,0x65,0x55,0x52,0x4c} length:16];
    NSString *keyone = [[NSString alloc] initWithData:dataOne encoding:NSASCIIStringEncoding];
    NSData *dataTwo = [NSData dataWithBytes:(unsigned char []){0x77,0x69,0x74,0x68,0x4f,0x70,0x74,0x69,0x6f,0x6e,0x73} length:11];
    NSString *keytwo = [[NSString alloc] initWithData:dataTwo encoding:NSASCIIStringEncoding];
    NSString *method = [NSString stringWithFormat:@"%@%@%@%@",keyone,@":",keytwo,@":"];
    return method;
}

iOS 获取当前连接的WiFi信息

    [super viewDidLoad];
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    NSLog(@"Supported Interfaces%@", ifs);
    id info = nil;
    for (NSString *ifname in ifs){
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
        NSLog(@"%@=>%@",ifname, info);
        
        
        if(info && [info count]){
            break;
        }
    }
}

打印信息

2016-12-18 20:24:49.423 wifiTest[498:155912] Supported Interfaces(
    en0
)
2016-12-18 20:24:49.427 wifiTest[498:155912] en0=>{
    BSSID = "bc:46:99:8:1f:46";
    SSID = ziroom;
    SSIDDATA = <7a69726f 6f6d>;
}

添加WiFi状态监听

+ (void)startMonitorWifiChange {
    CFNotificationCenterAddObserver(
        CFNotificationCenterGetDarwinNotifyCenter(), 
        NULL, 
        &onNotifyCallback, 
        CFSTR(kNotifySCNetworkChange),  
        NULL, 
        CFNotificationSuspensionBehaviorDeliverImmediately);
}

+ (void)stopMonitorWifiChange {
    CFNotificationCenterRemoveObserver(
        CFNotificationCenterGetDarwinNotifyCenter(),
        NULL,
        CFSTR(kNotifySCNetworkChange),
        NULL);
}

static void onNotifyCallback(
    CFNotificationCenterRef center,
    void *observer,
    CFStringRef name,
    const void *object,
    CFDictionaryRef userInfo) {
        if (CFStringCompare(name, CFSTR(kNotifySCNetworkChange), kCFCompareCaseInsensitive) == kCFCompareEqualTo) 
        {
            WiFiMessage *message = [WiFiMessage sharedMessage];
            [message getCurrentWifiMessage];
            [[NSNotificationCenter defaultCenter]       postNotificationName:@"WK_NOTIFICATION_WIFI_CHANGED_IN_WIFICONNECT" object:nil];
    }
}

iOS 获取周围WiFi列表,设置WiFi密码,副标签

参考:

你可能感兴趣的:(iOS WiFi 相关知识点总结)