在Nativescript中获取mac地址和ip [英] Get mac address and ip in Nativescript
问题描述
我们正在实现服务器分发应用程序,我们需要通过以下方式限制对应用程序的访问:
- mac地址
- ip
目前,我还没有找到任何模块可以使用nativescript从设备中获取此数据,所以我不知道是否有插件,或者我怎么能实现这一目标.
在nativescript中,您可以访问设备的本地api 因此,如果没有任何模块/插件,则可以使用此选项访问本机api. https://docs.nativescript.org/core-concepts/使用javascript访问本机apis
例如,在此处 在JAVA中:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();
我们可以像这样用javascript编写它:
首先,我们应该在getSystemService
的位置罚款:
搜索android文档后,我们发现:
getSystemService
在android.content.Context
中
用于访问本机脚本中的上下文 http://docs.nativescript.org/cookbook/application >
我们可以做到:
import app = require("application");
app.android.context;
所以让我们用javascript来编写它:
我们在javascript中没有类型,所以我们改用var;
var context = android.content.Context;
var wifiManager = app.android.context.getSystemService(context.WIFI_SERVICE);
var wInfo = wifiManager.getConnectionInfo();
var mac = wInfo.getMacAddress();
注意1:如上面的java解决方案链接中所述,您应该将此权限<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
添加到app/App_Resources/AndroidManifest
注2:以上解决方案适用于android,对于ios,您应该找到具有Objective_c的解决方案,并借助nativescript文档转换为javascript.
注意3:在android 6中,您可能需要请求权限
您还可以使用此方法为本地脚本创建插件.
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.
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.
这篇关于在Nativescript中获取mac地址和ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!