runtime.uts 1.85 KB
/**
 * plus.runtime 的 iOS 原生实现
 * 获取应用版本号等信息
 */

// #ifdef APP-IOS
import Foundation.NSBundle;
import Foundation.NSDictionary;
// #endif

/**
 * 获取应用版本号
 */
export function getAppVersion(): string {
	// #ifdef APP-IOS
	try {
		const bundle = NSBundle.mainBundle();
		if (bundle != null) {
			const infoDictionary = bundle.infoDictionary;
			if (infoDictionary != null) {
				const version = infoDictionary.objectForKey("CFBundleShortVersionString");
				if (version != null) {
					return version.toString();
				}
			}
		}
	} catch (e: any) {
		console.error(`[iOS] 获取版本号失败: ${String(e)}`);
	}
	return "";
	// #endif

	// #ifndef APP-IOS
	return "";
	// #endif
}

/**
 * 获取应用ID(Bundle Identifier)
 */
export function getAppId(): string {
	// #ifdef APP-IOS
	try {
		const bundle = NSBundle.mainBundle();
		if (bundle != null) {
			const bundleIdentifier = bundle.bundleIdentifier;
			if (bundleIdentifier != null) {
				return bundleIdentifier;
			}
		}
	} catch (e: any) {
		console.error(`[iOS] 获取应用ID失败: ${String(e)}`);
	}
	return "";
	// #endif

	// #ifndef APP-IOS
	return "";
	// #endif
}

/**
 * 获取版本代码(Build Number)
 */
export function getVersionCode(): number {
	// #ifdef APP-IOS
	try {
		const bundle = NSBundle.mainBundle();
		if (bundle != null) {
			const infoDictionary = bundle.infoDictionary;
			if (infoDictionary != null) {
				const buildNumber = infoDictionary.objectForKey("CFBundleVersion");
				if (buildNumber != null) {
					const buildStr = buildNumber.toString();
					// 尝试转换为数字
					const buildNum = parseInt(buildStr);
					if (!isNaN(buildNum)) {
						return buildNum;
					}
				}
			}
		}
	} catch (e: any) {
		console.error(`[iOS] 获取版本代码失败: ${String(e)}`);
	}
	return 0;
	// #endif

	// #ifndef APP-ANDROID
	return 0;
	// #endif
}