ios获取ip的代码,用native.js怎么实现的,找了很多代码 ,都不会使用,麻烦有木有代码啊参考一下,论坛里只给出了android获取ip的方式,能不能补充一下,plus.ios.name=="IOS"是如何实现获取IP地址的??、在线等.....
TianYA1991
- 发布:2016-03-04 08:58
- 更新:2019-08-31 17:46
- 阅读:5879
需要写插件实现目前NJS暂不支持改功能
#import <ifaddrs.h>
#import <arpa/inet.h>
#import "PGMethod.h"
#import "PGPlugin.h"
//1.插件实现
@interface IF: PGPlugin
- (void)getAddresses:(PGMethod*)method;
@end
@implementation IF
- (void)getAddresses:(PGMethod*)method {
NSString *callbackID = [method getArgumentAtIndex:0];
struct ifaddrs *interfaces = NULL;
struct ifaddrs *enumAddr = NULL;
NSMutableDictionary *ret = [NSMutableDictionary dictionary];
if ( 0 == getifaddrs(&interfaces)) {
enumAddr = interfaces;
while(enumAddr != NULL) {
if(enumAddr->ifa_addr->sa_family == AF_INET) {
NSString *address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)enumAddr->ifa_addr)->sin_addr)];
NSString *ifName = [NSString stringWithUTF8String:enumAddr->ifa_name];
//NSLog(@"sa_family--%d---Name:[%@]---Addr:[%@]", temp_addr->ifa_addr->sa_family, ifName, address);
[ret setObject:address forKey:ifName];
}
enumAddr = enumAddr->ifa_next;
}
freeifaddrs(interfaces);
}
[self toSucessCallback:callbackID withJSON:ret];
}
@end
//2.注册插件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL ret = [PDRCore initEngineWihtOptions:launchOptions
withRunMode:PDRCoreRunModeNormal];
[[PDRCore Instance] regPluginWithName:@"IF" impClassName:@"IF" type:PDRExendPluginTypeFrame javaScript:@";(function(plus){\
var B = window.plus.bridge,\
_PLUSNAME = 'IF';\
plus.IF = {\
getAddresses:function(SuccessCallback)\
{\
var callbackid = B.callbackId(SuccessCallback);\
B.exec(_PLUSNAME,'getAddresses',[callbackid]);\
}\
};\
})(window.plus);"];
//3.manifest.json permissions节点下添加
"permissions": {
"IF": {
"description": ""
},
//4. Example
plus.IF.getAddresses(
function( addrs ) {
console.log(JSON.stringify(addrs))
}
);
8***@qq.com
这个只能获取到ip地址,我想获取wifi名称,以及附近的wifi列表怎么实现呢。
2017-05-11 20:13
随风飘扬
这插件加在什么地方使用?
2018-09-04 16:53