Ionic 4+ Capacitor Angular:如何为 ios 和 android 编写平台特定的 ts 代码?

Ionic 4+ Capacitor Angular: How to write platform-specific ts-code for ios and android?

如何在ts文件中编写特定于平台的代码?那么仅对 ios 有效的代码和仅对 android 有效的代码?如何确定设备运行 ios 还是 android 作为操作系统?

我将 Ionic 4+ 与 Capacitor 和 Angular 一起使用。

非常感谢!

因此,要编写插件特定的 TS 代码,您需要已经开发了 Capacitor 插件,它应该公开可以在 TS 代码中使用的方法等

我认为这是对电容器插件生态系统工作原理的非常有说服力的描述

https://capacitor.ionicframework.com/docs/plugins/

下面是文章中有关如何创建您自己的 Android 插件的详细示例: https://capacitor.ionicframework.com/docs/plugins/android

关于 OS 设备运行一个 - Ionic 有一个内置功能: https://ionicframework.com/docs/building/cross-platform/#platform-detection

因此使用 'Platform' 您可以检测 OS 并执行 OS 特定操作 请参阅 Ionic 的参考应用程序 app.component.ts,它在应用程序初始化方法中的位置,以检测何时可以调用本机启动画面等插件:

https://github.com/ionic-team/ionic-conference-app/blob/master/src/app/app.component.ts

您可以使用Platform检测OS。

import { Platform } from '@ionic/angular';

@Component({...})
export class MyPage {
  constructor(public platform: Platform) {
     if(platform.is('ios')){
        //ios logic
     }
  }
}

非常感谢您的努力。

我找到了我要找的东西。

https://capacitor.ionicframework.com/docs/apis/device

import { Plugins } from '@capacitor/core'; 
const { Device } = Plugins;
const info = await Device.getInfo();
console.log(info);

// Example output:
{
"diskFree": 12228108288,
"appVersion": "1.0.2", 
"appBuild": "123", 
"operatingSystem": "ios", 
"osVersion": "11.2", 
"platform": "ios", 
"memUsed": 93851648, 
"battery": -1, 
"diskTotal": 499054952448, 
"model": "iPhone", 
"manufacturer": "Apple", 
"uuid": "84AE7AA1-7000-4696-8A74-4FD588A4A5C7", 
"isVirtual":true 
}