react-native-device-info 这个组件的最新版本 0.22.5 能获得 手机的 ip 地址 和 mac 地址 但是 获取不了手机的真实 ip 地址, 下面通过另外中方式抓取手机真实 ip 地址
这里通过 webview 的方式获取的, 接口地址 http://whois.pconline.com.cn/ (这个网站的稳定性 不太清楚)
webView 相关代码 大家可作为参考
- <!-- 这里用的是太平洋电脑的接口 不知道稳定性
- getJson({
- "ip":"xxxxxxxxxx",
- "pro":"广东省",
- "proCode":"440000",
- "city":"深圳市",
- "cityCode":"440300",
- "region":"",
- "regionCode":"0",
- "addr":"广东省深圳市 移通",
- "regionNames":"",
- "err":""
- })
- -->
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- </head>
- <body>
- </body>
- <script>
- // (function () {
- // })();
- function getJson(obj) {
- // 需要 ip 地址, IP 映射的物理地址
- var originalPostMessage = window.postMessage;
- var patchedPostMessage = function (message, targetOrigin, transfer) {
- originalPostMessage(message, targetOrigin, transfer);
- };
- patchedPostMessage.toString = function () {
- return String(Object.hasOwnProperty).replace('hasOwnProperty', 'postMessage');
- };
- window.postMessage = patchedPostMessage;
- var ObjString = JSON.stringify(obj);
- setTimeout(()=>{
- window.postMessage(ObjString)
- },2000)
- }
- </script>
- <script src="http://whois.pconline.com.cn/ipJson.jsp?callback=getJson"></script>
- </html>
来源: http://www.jianshu.com/p/de59ff681c08