zoukankan      html  css  js  c++  java
  • 获取设备IP地址

    腾讯的IP地址API接口地址:http://fw.qq.com/ipaddress
    返回的是数据格式为:

    1 var IPData = new Array(“58.218.198.205″,”",”江苏省”,”徐州市”); 

    使用JS代码进行调取:

    1 2  

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js
    新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=12.130.132.30
    搜狐IP地址查询接口(默认GBK):http://pv.sohu.com/cityjson
    搜狐IP地址查询接口(可设置编码):http://pv.sohu.com/cityjson?ie=utf-8
    搜狐另外的IP地址查询接口:http://txt.go.sohu.com/ip/soip

    #pragma mark 获取设备IP//(局域网内部IP外部的2G/3G/4G网络获取不到)
    // Get IP Address
    
    +(NSString *)getIPAddress {
        NSString *address = @"error";
        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;
        
    }
    
    //*************************************
    //通过搜狐的IP地址查询接口进行编码获取;
    //可以获取非WiFi网络,但是WiFi网络IP地址为公网非局域网地址

    +(NSDictionary *)deviceWANIPAdress{ NSError *error; NSURL *ipURL = [NSURL URLWithString:@"http://pv.sohu.com/cityjson?ie=utf-8"]; NSMutableString *ip = [NSMutableString stringWithContentsOfURL:ipURL encoding:NSUTF8StringEncoding error:&error]; //判断返回字符串是否为所需数据 if ([ip hasPrefix:@"var returnCitySN = "]) { //对字符串进行处理,然后进行json解析 //删除字符串多余字符串 NSRange range = NSMakeRange(0, 19); [ip deleteCharactersInRange:range]; NSString * nowIp =[ip substringToIndex:ip.length-1]; //将字符串转换成二进制进行Json解析 NSData * data = [nowIp dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; return dict; } return nil; }
  • 相关阅读:
    The formatter threw an exception while trying to deserialize the message in WCF
    通过Web Deploy方式部署WCF
    The Managed Metadata Service or Connection is currently not available
    How to create Managed Metadata Column
    冒泡算法
    asp.net core 实战项目(一)——ef core的使用
    Vue学习笔记入门篇——安装及常用指令介绍
    Vue学习笔记入门篇——数据及DOM
    Vue学习笔记目录
    Chart.js在Laravel项目中的应用
  • 原文地址:https://www.cnblogs.com/OIMM/p/5620058.html
Copyright © 2011-2022 走看看