NativeScript托管设备
为托管应用程序的NativeScript设备插件。
npm i --save nativescript-hosted-device

NativeScript托管设备插件

这是一个NativeScript插件,允许您从托管的Angular 2应用程序中访问设备信息。

安装

tns plugin add nativescript-hosted-device

用法

使用以下代码和一个Navivescript WebView创建一个包装应用程序。以下项目可作为示例:https://github.com/Ulbora/NSWrapper

var application = require("application");
function pageLoaded(args) {
var page = args.object;
var web = page.getViewById("webView");
var androidSettings = web.android.getSettings();
androidSettings.setJavaScriptEnabled(true);
var device = new com.ulbora.device.Device;
web.android.addJavascriptInterface(device, 'NSDevice');
web.url = "http://someURLWhereAngular2AppIsHosted";
}
exports.pageLoaded = pageLoaded;

在Angular 2托管应用程序内部,编写您想访问设备信息的代码。以下项目可作为示例:https://github.com/KenWilliamson/Angular2HostedMobileApp

组件代码

deviceReady: boolean;

ngOnInit() {
this.id = this._routeParams.get('id');
this.hero = this._heroDetailsService.getHeroDetails(this.id);
try {
if (NSDevice) {
this.deviceReady = true;
}
} catch (err) {
}
}


onShowDevice() {
try {
this.dev = "Mobile version: " + NSDevice.getModel();
} catch (err) {
alert('Failed because: ' + err);
this.error = err.message;
}
}

模板代码

<div *ngIf="deviceReady">
<div> <b>Your device</b>: <span (click)="onShowDevice()" class="glyphicon glyphicon-phone"></span></div>
</div>

可用方法

NSDevice.getModel()

返回设备的构建信息

NSDevice.getProductName()

返回设备的产品名称

NSDevice.getManufacturer()

返回设备的制造商信息

NSDevice.getSerialNumber()

返回设备的序列号

NSDevice.getOSVersion()

返回设备的操作系统版本

NSDevice.getPlatform()

返回设备平台