Search code examples
angularionic-frameworkcapacitor

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


How to write platform-specific code in the ts-file? So code that is only valid for ios and code that is only valid for android? How can I determine whether the device runs ios or android as operating system?

I use Ionic 4+ with Capacitor and Angular.

Many thanks in advance!


Solution

  • You can use Platform to detect OS.

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