0

I am new to ionic and cordova plugins and I'm trying to implement the sharing of cookies using the cordova-plugin-sfauthenticationsession in my ionic app.

Here is my code based on the documentation provided in this link:

SFAuthSession.start("myScheme://","https://www.facebook.com/",
        function(data){
                console.log(data);
        },function(error){
                console.log(error);
        }
);

The problem is whenever I try to build my app, the terminal shows an error that it cannot find SFAuthSession. I don't have any idea what I need to import (if any) since it is not included in the documentation. Hope somebody can help me with this.

P.S. I also tried the safari view controller following this documentation and it is working fine since it provides what is to be imported.

Angelo
  • 111
  • 1
  • 11

1 Answers1

1

This is a common error. The docs tell you how to install that specific plugin but they don't tell you that you also need to update your module file to include it.

Look at this page:

It explains that you need to import the plugin in a @NgModule and add it to the list of Providers. For Angular, the import path should end with /ngx. Angular's change detection is automatically handled.

To do this you do:

// app.module.ts
import { Camera } from '@ionic-native/camera/ngx';

...

@NgModule({
  ...

  providers: [
    ...
    Camera
    ...
  ]
  ...
})
export class AppModule { }

So just import your Ionic Native module using that technique and you it should work.

When there is no Ionic Native wrapper

However, based on your comments below it is now clear that there isn't an Ionic Native wrapper for this Cordova plugin.

This means that you will have to either:

  • Access it without Ionic Native
  • OR write your own Ionic Native wrapper

It seems like this article on Medium has a great introduction to this:

Build your first Cordova plugin for Ionic Native – Sangkhim Khun – Medium

You have passed beyond my personal experience here but I'm trying to figure it out with you.

Part three of the tutorial has an interesting snippet for accessing a Cordova plugin directly:

declare var cordova: any;
var success = function(result) {
  alert(JSON.stringify(result, undefined, 2));
}
var failure = function(result) {
  alert(JSON.stringify(result, undefined, 2));
}
cordova.plugins.HelloWorld.coolMethod({
  _sMessage: "Hello World"
}, success, failure);

You would have to compare the documentation of your Cordova plugin to adapt this yourself to get it working.

Alternatively, if you continue reading that tutorial it explains how to create an Ionic Native wrapper which you could even contribute back to the project so that everyone can use this feature.

rtpHarry
  • 13,019
  • 4
  • 43
  • 64
  • Hi @rtpHarry Thank you for your time. I tried doing this but the terminal is returning a different error, it says it cannot find module '@ionic-native/sfauth/ngx'. To be honest I don't have any idea what to import in app.module.ts. I only got the path '@ionic-native/sfauth/ngx' from the recommended fix from VSCode. Is there a way to determine the necessary import? – Angelo Jul 08 '19 at 05:45
  • You are right, I thought it was just a normal install you are doing. The way it works is that first you need a cordova plugin which is what you have found - a bridge between the phone and javascript. Then you need an Ionic Native module that can pass the data from the cordova plugin to Ionic. There isn't one for this cordova plugin. I've updated my answer. – rtpHarry Jul 09 '19 at 03:58