0

I am using FileChooser ionic plugin for Android and FilePicker plugin for IOS. If I use the same code to build android and IOS app, it's giving me the error that FilePicker plugin cant be added to Android. To build app on different platforms currently, I am maintaining two different branches for iOS build and android build.

I want to maintain my code on only one branch. How can I do that?

Solutions which didn't work:

  1. Searched for the plugin which can work on both iOS and Android but it's not available
  2. Searched if we can add plugins under the platform in config.xml
  3. I found no way to detect the platform and add the plugins accordingly
francescalus
  • 30,576
  • 16
  • 61
  • 96
Shrutika Patil
  • 565
  • 3
  • 18

1 Answers1

1

You can add platform specific checks and use plugins accordingly.

  import { Platform } from 'ionic-angular';
  import { FileChooser } from '@ionic-native/file-chooser';
  import { FilePath } from '@ionic-native/file-path';
  import { IOSFilePicker } from '@ionic-native/file-picker';

  constructor(
    private fileChooser: FileChooser,
    private filePicker: IOSFilePicker,
    private filePath: FilePath,
    private platform: Platform) {
  }

  chooseFile() {
    if (this.platform.is('ios')) {
      this.pickFileFromIOSDevice();
    }
    else if (this.platform.is('android')) {
      this.pickFileFromAndroidDevice();
    }
  }

  pickFileFromIOSDevice() {
    this.filePicker.pickFile()
      .then(
        uri => {
          this.fileName = uri.substring(uri.lastIndexOf("/") + 1);
        }
      )
      .catch(error => {
        this.showError(error);
      });
  }

  pickFileFromAndroidDevice() {
    this.fileChooser.open()
      .then(
        uri => {
          this.filePath.resolveNativePath(uri)
            .then(file => {
              this.fileName = file.substring(file.lastIndexOf("/") + 1);
            })
            .catch(err => console.log(err));
        }
      )
      .catch(error => {
        this.showError(error);
      });
  }
Mangesh Daundkar
  • 1,026
  • 1
  • 15
  • 20
  • This solution worked for me. But now everytime I need to remove and again add node modules and platform to build android and ios apps. Is there any way to manage plugins ? – Shrutika Patil Aug 16 '18 at 17:28
  • No. Actually, its not necessary to remove and add node modules and platform everytime. Its already handled in code through if else condition. If platform is android it picks filechooser and if platform is iOS it picks filepicker. Why you are removing and adding node modules and platform again. I have already build the apk with this code. Its working fine. – Mangesh Daundkar Aug 17 '18 at 04:41