Native.js虽然强大和开放,但很多web开发者因为不熟悉原生API而难以独立完成。
这篇帖子的目的就是汇总各种写好的NJS代码,方便web开发者。
众人拾柴火焰高,有能力的开发者多多提交NJS代码,大家都会给你点赞的,我们也会为每位共享NJS代码的朋友送上200积分。
Native.JS监听手机锁屏,解锁,开屏 - DCloud问答
Native.js 获取安卓设备device.uuid - DCloud问答
一些android公共方法native.js实现 - DCloud问答
Native.js如何访问蓝牙Bluetooth - DCloud问答
使用BroadcastReceiver监听蓝牙开关状态 - DCloud问答
安卓Native.js蓝牙连接票据打印机完整代码已测试修改 - DCloud问答
Andorid下实现的获取MAC地址的NJS代码分享 - DCloud问答
【交流分享】Native.js获取设备当前网速 - DCloud问答
Native.js打开Android网络设置 - DCloud问答
Android平台 Native.js跳转系统设置各个界面。 - DCloud问答
Native.js调用原生播放video视频的样例 - DCloud问答
Android调用系统通讯录控件,native.js实现监听startActivityForResult后返回结果 - DCloud问答
分享一个Android原生日历提醒插入案例。。。求个iOS的??谢谢。。。。。 - DCloud问答
native.js实现AlarmManager定时任务 - DCloud问答
Native.js调用系统裁剪图片、头像裁剪-Android - DCloud问答
如何操作系统剪切板,如何用Native.js复制内容 - DCloud问答
Android调用系统通讯录控件,native.js实现监听startActivityForResult后返回结果 - DCloud问答
Android平台通过native.js实现接收系统消息,如监听安装卸载apk事件 - DCloud问答
Android如何判断app是否安装(native.js) - DCloud问答
以监听手机飞行模式开关为例说明如何使用Native.js进行BroadcastReceiver广播 - DCloud问答
[20180607已更新] 使用NJS控制Android通知栏,不用个推实现本地消息推送(Local Notification) - DCloud问答
调用Native.js,如何启动一个service? - DCloud问答
基于native.js的文件系统管理功能实现 - DCloud问答
var pushManager = plus.android.importClass("com.igexin.sdk.PushManager");
var context = plus.android.runtimeMainActivity();
function enable() {
pushManager.getInstance().turnOnPush(context);
}
function disable() {
pushManager.getInstance().turnOffPush(context);
}
感谢yeahcheung分享
利用native.js获取手机gps是否开启 - DCloud问答
使用native.js修改系统壁纸(Android) - DCloud问答
视频拍摄能不能限制拍摄时间和微信小视频一样 - DCloud问答
Anroid 6.0 动态权限申请代码分享 - DCloud问答
【分享】一个Notification 进度条插件(android,NJS实现,直接就可使用) - DCloud问答
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
var setting2 = NSURL2.URLWithString("app-settings:");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
iOS平台:用Native.js来写 如何判断系统功能权限是否开启 - DCloud问答
var NSBundle = plus.ios.importClass('NSBundle');
var bundle = NSBundle.mainBundle();
console.log(bundle.bundleIdentifier());
plus.ios.deleteObject(bundle);
如何判断 手机上是否安装 某个app 比如百度地图? - DCloud问答
见Hello H5+里Native.js部分演示及源码。
或在这里搜索“game center”,文档 - DCloud问答
iOS下如何使用Native.js设置剪贴板? - DCloud问答
用njs如何把base64数据保存为图片(ios) - DCloud问答
var webview = plus.ios.currentWebview();
var scrollView = webview.plusGetAttribute("scrollView");
scrollView.plusSetAttribute("decelerationRate:",0.99);
var NSTimeZone = plus.ios.importClass("NSTimeZone");
var sys = NSTimeZone.systemTimeZone();
console.log(sys.plusGetAttribute("name"));
var UIApplication = plus.ios.import("UIApplication");
var sharedApplication = UIApplication.sharedApplication();
sharedApplication.setNetworkActivityIndicatorVisible(true);
plus.ios.deleteObject(sharedApplication);
var CLLocationManager = plus.ios.import("CLLocationManager");
var authorizationStatus = CLLocationManager.authorizationStatus();
switch(authorizationStatus) {
case 0:
/// User has not yet made a choice with regards to this application
break;
case 1:
// This application is not authorized to use location services. Due
// to active restrictions on location services, the user cannot change
// this status, and may not have personally denied authorization
break;
case 2:
// User has explicitly denied authorization for this application, or
// location services are disabled in Settings.
break;
case 3:
// User has granted authorization to use their location at any time,
// including monitoring for regions, visits, or significant location changes.
break;
case 4:
// User has granted authorization to use their location only when your app
// is visible to them (it will be made visible to them if you continue to
// receive location updates while in the background). Authorization to use
// launch APIs has not been granted.
break;
case 5:
// This value is deprecated, but was equivalent to the new -Always value.
break;
defalut:
break;
}
var BundleClass = plus.ios.importClass("NSBundle");
var BundleObj = BundleClass.mainBundle();
var filenamagerobj = plus.ios.newObject("NSFileManager");
var FileAttr = plus.ios.invoke(filenamagerobj,"attributesOfFileSystemForPath:error:",BundleObj.bundlePath(),null);
// NSFileSystemFreeSize 参数获取剩余空间
// NSFileSystemSize 获取手机总存储空间
var freeSpace = plus.ios.invoke(FileAttr,"objectForKey:","NSFileSystemFreeSize");
var numberFormatterObj = plus.ios.newObject("NSNumberFormatter");
var FreeSpaceStr = plus.ios.invoke(numberFormatterObj,"stringFromNumber:",freeSpace);
var freeSpace = FreeSpaceStr / 1024/1024/1024;
function turnonLight(isOn) {
if(plus.os.name == "iOS") {
var avcaptClass = plus.ios.importClass("AVCaptureDevice");
if(avcaptClass) {
var device = avcaptClass.defaultDeviceWithMediaType("vide");
plus.ios.invoke(device, "lockForConfiguration:", null);
if(isOn) {
plus.ios.invoke(device, "setTorchMode:", 1);
plus.ios.invoke(device, "setFlashMode:", 1);
} else {
plus.ios.invoke(device, "setTorchMode:", 0);
plus.ios.invoke(device, "setFlashMode:", 0);
}
plus.ios.invoke(device, "unlockForConfiguration");
}
}
};
// NewViewController为应用内创建的原生的ViewController类名,所调用页面的内容需要在原生代码中完成
var newVCobj = plus.ios.newObject("NewViewController");
var UIApplicationClass = plus.ios.importClass("UIApplication");
var UIAppObj = UIApplicationClass.sharedApplication();
var del = plus.ios.invoke(UIAppObj,"delegate");
// 如果当前应用delegate对象包含UIWindow对象并且变量名命名为“window”可以这么写,
// 否则需要根据实际代码情况修改
// 应用的delegate对象也可以添加一个返回UIViewController的方法
var appWindowObj = plus.ios.invoke(del,"window");
var appRootController = plus.ios.invoke(appWindowObj,"rootViewController");
plus.ios.invoke(appRootController,"presentViewController:animated:completion:",newVCobj,"YES",null);