美文网首页
获取iPhone手机当前连接的WiFi信息

获取iPhone手机当前连接的WiFi信息

作者: YFBigHeart | 来源:发表于2018-12-16 17:01 被阅读41次

    第一步:获取WiFi信息需要在Target—>Capabilities—>Access wifi information 点击允许


    image.png

    下面是获取WiFi信息的类方法及实现已使用WiFi信息的本地持久化储存,使用中记得更改存储主键值[[NSUserDefaults standardUserDefaults] objectForKey:kDefaultUserID]

    //
    //  YFWifiItem.h
    //  SmartAlarm
    //
    //  Created by 晴天 on 2018/12/16.
    //  Copyright © 2018年 Huang Shan. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface YFWifiItem : NSObject <NSCoding>
    /** userUUID */
    @property (nonatomic, copy)NSString *userUUID;
    /** ssid */
    @property (nonatomic, copy)NSString *wifiSsid;
    /** password */
    @property (nonatomic, copy)NSString *wifiPassword;
    /** wifiBssid, 网络的IPMac */
    @property (nonatomic, copy) NSString *wifiBssid;
    
    // 1.存储或更新WiFi信息
    + (YFWifiItem *)updateOrSavewWithSsid:(NSString *)wifiSsid bssid:(NSString *)bssid wifipassword:(NSString *)password;
    // 2.根据SSId拿当前用户存储过的WiFi数据
    + (YFWifiItem *)getWifiItemWithSSID:(NSString *)ssid;
    
    #pragma mark - 获取当前链接的WiFi基本信息
    // 获取IP地址
    + (NSString *)deviceIPAdress;
    // 获取WiFi的ssid
    + (NSString *)getWifiSSID;
    // 获取WiFi的Bssid
    + (NSString *)getWifiBSSID;
    
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    //
    //  YFWifiItem.m
    //  SmartAlarm
    //
    //  Created by 晴天 on 2018/12/16.
    //  Copyright © 2018年 Huang Shan. All rights reserved.
    //
    
    #import "YFWifiItem.h"
    #import <SystemConfiguration/CaptiveNetwork.h>
    #import <ifaddrs.h>
    #import <arpa/inet.h>
    
    @implementation YFWifiItem
    
    - (instancetype)initWithCoder:(NSCoder *)aDecoder {
        if (self = [super init]) {
            _userUUID = [aDecoder decodeObjectForKey:@"userUUID"];
            _wifiSsid = [aDecoder decodeObjectForKey:@"wifiSsid"];
            _wifiPassword = [aDecoder decodeObjectForKey:@"wifiPassword"];
            _wifiBssid = [aDecoder decodeObjectForKey:@"wifiBssid"];
        }
        return self;
    }
    - (void)encodeWithCoder:(NSCoder *)aCoder {
        [aCoder encodeObject:self.userUUID forKey:@"userUUID"];
        [aCoder encodeObject:self.wifiSsid forKey:@"wifiSsid"];
        [aCoder encodeObject:self.wifiPassword forKey:@"wifiPassword"];
        [aCoder encodeObject:self.userUUID forKey:@"wifiBssid"];
    }
    
    // 1.存储或更新WiFi信息
    + (YFWifiItem *)updateOrSavewWithSsid:(NSString *)wifiSsid bssid:(NSString *)bssid wifipassword:(NSString *)password {
        //查看当前是否已经有
        YFWifiItem *wifiItem = [[YFWifiItem alloc] init];
        wifiItem.userUUID = [[NSUserDefaults standardUserDefaults] objectForKey:kDefaultUserID];
        wifiItem.wifiSsid = wifiSsid;
        wifiItem.wifiPassword = password;
        wifiItem.wifiBssid = bssid;
        if (wifiSsid.length > 0) {
            NSString *path = [self getSaveUserWifiPath];
            NSArray *oldDataArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
            NSMutableArray *arrM = [NSMutableArray arrayWithArray:oldDataArr];
            if (oldDataArr.count > 0) {
                NSArray *results = [oldDataArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"wifiSsid=%@",wifiSsid]];
                [arrM removeObjectsInArray:results];
            }
            [arrM addObject:wifiItem];
            [NSKeyedArchiver archiveRootObject:arrM.copy toFile:path];
        }
        return wifiItem;
    }
    
    // 获取当前用户WiFi的存储路径
    + (NSString *)getSaveUserWifiPath {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:[NSString stringWithFormat:@"UserIdWifi-%@",[[NSUserDefaults standardUserDefaults] objectForKey:kDefaultUserID]]];
        return path;
    }
    
    // 2.根据SSId拿当前用户存储过的WiFi数据
    + (YFWifiItem *)getWifiItemWithSSID:(NSString *)ssid {
        if (!ssid.length) {
            return nil;
        }
        NSString *path = [self getSaveUserWifiPath];
        NSArray *oldDataArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
        NSArray *results = [oldDataArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"wifiSsid=%@",ssid]];
        if (results.count > 0) {
            return results.lastObject;
        }else {
            return nil;
        }
    }
    
    #pragma mark -获取WiFi基本信息
    + (NSString *)deviceIPAdress {
        //出处: https://www.jianshu.com/p/c3499d8e9a72
        NSString *address = @"an error occurred when obtaining ip address";
        struct ifaddrs *interfaces = NULL;
        struct ifaddrs *temp_addr = NULL;
        int success = 0;
        
        success = getifaddrs(&interfaces);
        
        if (success == 0) { // 0 表示获取成功
            
            temp_addr = interfaces;
            while (temp_addr != NULL) {
                if( temp_addr->ifa_addr->sa_family == AF_INET) {
                    // Check if interface is en0 which is the wifi connection on the iPhone
                    if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                        // Get NSString from C String
                        address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                    }
                }
                
                temp_addr = temp_addr->ifa_next;
            }
        }
        freeifaddrs(interfaces);
        
        return address;
    }
    
    /// 获取wifi名称
    + (NSString *)getWifiSSID {
        NSString *ssid = nil;//NSLocalizedString(@"未连接WiFi", nil);
        NSDictionary *info = [self getWifiInfo];
        if (info != nil) {
            ssid = [info objectForKey:@"SSID"];
        }
        return ssid;
    }
    /// 获取WiFi的Bssid
    + (NSString *)getWifiBSSID {
        NSString *bSsid = nil;//NSLocalizedString(@"未连接WiFi", nil);
        NSDictionary *bssidInfo = [self getWifiInfo];
        if (bssidInfo != nil) {
            bSsid = [bssidInfo objectForKey:@"BSSID"];
        }
        return bSsid;
    }
    
    + (NSDictionary *)getWifiInfo
    {
        NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
        
        NSDictionary *SSIDInfo;
        for (NSString *interfaceName in interfaceNames) {
            SSIDInfo = CFBridgingRelease(
                                         CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
            
            BOOL isNotEmpty = (SSIDInfo.count > 0);
            if (isNotEmpty) {
                break;
            }
        }
        return SSIDInfo;
    }
    
    @end
    
    

    获取WiFi列表实现可参考:
    获取WiFi列表信息https://group.cnblogs.com/topic/78158.html

    相关文章

      网友评论

          本文标题:获取iPhone手机当前连接的WiFi信息

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