Shrutika Patil
Shrutika Patil

Reputation: 565

Build issue for ionic app

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

Upvotes: 0

Views: 80

Answers (1)

Mangesh Daundkar
Mangesh Daundkar

Reputation: 1046

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);
      });
  }

Upvotes: 1

Related Questions