AppBaseInfoDeviceUtil.uts 3.5 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1
import { UTSiOS } from "DCloudUTSFoundation";
DCloud-yyl's avatar
DCloud-yyl 已提交
2 3 4
import { Bundle, FileManager } from 'Foundation';
import { } from 'CommonCrypto';
import { UnsafeBufferPointer, UnsafeRawBufferPointer } from 'Swift';
DCloud-yyl's avatar
DCloud-yyl 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

export class AppBaseInfoDeviceUtil {

	public static getAppID() : string {
		return UTSiOS.getAppId();
	}

	public static getAppName() : string {
		return UTSiOS.getAppName();
	}

	public static getHostName() : string {
		return UTSiOS.getHostName();
	}

	public static getHostTheme() : string {
		return UTSiOS.getHostTheme();
	}

	public static getHostLanguage() : string {
		return UTSiOS.getHostLanguage();
	}

	public static getHostVersion() : string {
		return UTSiOS.getHostVersion();
	}

	public static getHostPackageName() : string {
		return UTSiOS.getHostPackageName();
	}

	public static getAppVersion() : string {
		return UTSiOS.getAppVersion();
	}

	public static getAppVersionCode() : string {
		return UTSiOS.getAppVersionCode();
	}

	public static getAppWgtVersion() : string {
		return UTSiOS.getAppWgtVersion();
	}

	public static getOsLanguage() : string {
		return UTSiOS.getOsLanguage();
	}

	public static getOsLanguageNormal() : string {
		const LOCALE_ZH_HANS = 'zh-Hans'
		const LOCALE_ZH_HANT = 'zh-Hant'
		let locale = UTSiOS.getOsLanguage();
		if (locale.indexOf('zh') == 0) {
			if (locale.indexOf('-hans') > -1) {
				return LOCALE_ZH_HANS;
			}
			if (locale.indexOf('-hant') > -1) {
				return LOCALE_ZH_HANT;
			}
			if (locale.includes("-tw") || locale.includes("-hk") || locale.includes("-mo") || locale.includes("-cht")) {
				return LOCALE_ZH_HANT;
			}

			return LOCALE_ZH_HANS;
		} else {
			return locale;
		}
	}

	public static getAppInnerVersion() : string {
		return UTSiOS.getInnerVersion();
	}

DCloud-yyl's avatar
DCloud-yyl 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	public static getBundleId() : string {
		return Bundle.main.bundleIdentifier!
	}

	public static getSignature() : string {
		let bundleId = AppBaseInfoDeviceUtil.getBundleId()
		const embeddedPath = Bundle.main.path(forResource = "embedded", ofType = "mobileprovision")
		if (embeddedPath != null) {
			if (FileManager.default.fileExists(atPath = embeddedPath!)) {
				const embeddedProvisioning : string | null = UTSiOS.try(new String(contentsOfFile = embeddedPath!, encoding = String.Encoding.ascii), "?")
				const embeddedProvisioningLines = embeddedProvisioning?.split("\n")
				let target = ""
				embeddedProvisioningLines?.forEach((line : string, index : number) => {
					if (line.indexOf("application-identifier") != -1) {
						if (index + 1 < embeddedProvisioningLines!.length) {
							target = embeddedProvisioningLines![index + 1]
						}
					}
				})
				const leftStr = "<string>"
				const rightStr = "</string>"
				if (target != "") {
					const start = target.indexOf(leftStr) + leftStr.length;
					const end = target.indexOf(rightStr)
					const fullIdentifier = target.slice(start, end)
					const idStart = fullIdentifier.indexOf(".") + 1
					const id = fullIdentifier.slice(idStart)
					if(id.length > 0){
						bundleId = id
					}
				}
			}
		}
		
		const strData = bundleId.data(using = String.Encoding.utf8)!
		let digest = new Array<UInt8>(repeating = 0, count = new Int(CC_MD5_DIGEST_LENGTH))
		strData.withUnsafeBytes((body : UnsafeRawBufferPointer) => {
			CC_MD5(body.baseAddress, new UInt32(strData.count), UTSiOS.getPointer(digest))
		})
		let md5String = ""
		digest.forEach((byte : UInt8) => {
			md5String += new String(format = "%02x", new UInt8(byte))
		})

		return md5String
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
123
}