uni-app如何获取苹果手机的IDFA及安卓手机的IEMI

在App.vue文件的onLaunch上增加以下代码:

onUniNViewMessage: function(e) {
			let pages = getCurrentPages();
			let page = pages[pages.length - 1];
			let currentWebview = page.$getAppWebview();
			plus.webview.postMessageToUniNView({
				detail: plus[e.data.module][e.data.api]
			}, currentWebview.id);
		},

然后仿照下面的写法增加IDFA获取方法

onLaunch: function() {
 if (plus.os.name == "iOS" || plus.os.name == "ios") {
 var ASIdentifierManager = plus.ios.importClass("ASIdentifierManager");
 if(ASIdentifierManager){
 var sharedManager = ASIdentifierManager.sharedManager();
 if (sharedManager.isAdvertisingTrackingEnabled()) {
 var advertisingIdentifier = sharedManager.advertisingIdentifier();
 var idfa = plus.ios.invoke(advertisingIdentifier, "UUIDString");
 uni.setStorageSync('idfa', idfa);
 }
 }
 }
 console.log('App Launch')
 },

然后就可以使用下面方法进行获取实际值了

uni.getSystemInfo({
 success: function(res) {
 if (res.platform == "ios") {
 uni.getStorage({
 key: 'idfa',
 success: function(res) {
 this.idfa = res.data;
 }
 });
 } else {
  uni.postMessage({
 module: "device",
 api: "imei"
 });
 globalEvent.addEventListener("plusMessage", e => {
 if (e.data.detail) {
 this.imei = e.data.detail
 }
 });
 }
 }
 });
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注