iOS获取当前连接的WiFi以及IP地址
更新时间:2017年03月30日 10:35:50 作者:张无忌!
本文主要介绍了iOS获取当前连接的WiFi以及IP地址方法的核心代码。具有很好的参考价值,下面跟着小编一起来看下吧
导入头文件
#import <ifaddrs.h> #import <arpa/inet.h> #import <SystemConfiguration/CaptiveNetwork.h>
核心代码:
+ (nullable NSString*)getCurrentLocalIP { NSString *address = nil; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; // retrieve the current interfaces - returns 0 on success success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked list of interfaces 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; } } // Free memory freeifaddrs(interfaces); return address; }
+ (nullable NSString *)getCurreWiFiSsid { NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); NSLog(@"Supported interfaces: %@", ifs); id info = nil; for (NSString *ifnam in ifs) { info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); NSLog(@"%@ => %@", ifnam, info); if (info && [info count]) { break; } } return [(NSDictionary*)info objectForKey:@"SSID"]; }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
相关文章
IOS NSNotification 键盘遮挡问题的解决办法
这篇文章主要介绍了IOS NSNotification 键盘遮挡问题的解决办法的相关资料,希望通过本文能帮助到大家,解决这样的问题,需要的朋友可以参考下2017-09-09IOS中UITextView或UITextField字数限制的实现
这篇文章主要介绍了IOS中UITextView或UITextField字数限制的实现的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下2017-10-10iOS App开发中的UIStackView堆叠视图使用教程
UIStackView是iOS9以来新增加的组件,使我们能够对UIView子类对象进行灵活排版,这里我们就来看一下iOS App开发中的UIStackView堆叠视图使用教程2016-07-07iOS中UIScrollView嵌套UITableView的实践教程
在UIScrollView嵌套UITableView的问题相信大家都遇到过,小编最近在工作中就遇到了这个问题,所以这篇文章主要介绍了iOS中UIScrollView嵌套UITableView的相关资料,文中介绍的方法是通过自己的实践所得来的,需要的朋友可以参考借鉴,下面来一起看看吧。2017-05-05iOS App开发中Core Data框架基本的数据管理功能小结
除了使用SQL关系型数据库,我们还可以使用Xcode中提供的Core Data来进行表结构数据处理,这里我们就来初步整理iOS App开发中Core Data框架基本的数据管理功能小结:2016-06-06
最新评论