Get mac address and ip in Nativescript

我们两清 提交于 2019-12-23 03:28:51

问题


We are implementing a server for app distribution and we need restrict the access to the apps by:

  • mac address
  • ip

At the moment I have not found any module that can obtain this data from the device in nativescript, so i don't know if there's a plugin or how else can I achieve this.


回答1:


In nativescript you can access native apis of device so if there isn't any module/plugin for it you can use this option for accessing native apis. https://docs.nativescript.org/core-concepts/accessing-native-apis-with-javascript

for example there is solution for mac adress here in JAVA:

WifiManager wifiManager = (WifiManager)  getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();

we can write it in javascript like this:

first we should fine where is this getSystemService:

after searching in documentation of android we found:

getSystemService is in android.content.Context for accessing context in nativescript http://docs.nativescript.org/cookbook/application

we can do:

import app = require("application");
app.android.context;

so let's write it in javascript:

we don't have types in javascript so we use var instead;

  var context = android.content.Context;
  var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
  var wInfo = wifiManager.getConnectionInfo();
  var mac = wInfo.getMacAddress();

NOTE1 : as mentioned in above java solution link you should add this permision <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> to app/App_Resources/AndroidManifest

NOTE2 : the above solution was for android,for ios you should find the solution with objective_c and convert to javascript with help of nativescript documentation.

NOTE3:In android 6 you might need request permision

You can also use this method to create a plugin for nativescript.



来源:https://stackoverflow.com/questions/40852900/get-mac-address-and-ip-in-nativescript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!