alexziskind1 / nativescript-oauth2

Other
86 stars 93 forks source link

SFSafariViewController not found on iOS 13 login #113

Open PhilippS93 opened 4 years ago

PhilippS93 commented 4 years ago

Make sure to check the demo app(s) for sample usage

Make sure to check the existing issues in this repository

If the demo apps cannot help and there is no issue for your problem, tell us about it

Please, ensure your title is less than 63 characters long and starts with a capital letter.

Which platform(s) does your issue occur on?

When implementing the default google sing in process following the video LINK I get the following error when calling loginWithCompletion on iOS:

ERROR Error: Class "SFSafariViewController" referenced by type encoding not found at runtime. (evaluating 'new TnsOAuthLoginNativeViewController()')

Is there any code involved?

` @Injectable() export class OauthService {

private client: TnsOAuthClient = null;

constructor() {
    this.configureOAuthProviders();
}

tnsOauthLogin(providerType): Promise<ITnsOAuthTokenResult> {
    this.client = new TnsOAuthClient(providerType);

    return new Promise<ITnsOAuthTokenResult>((resolve, reject) => {
        this.client.loginWithCompletion(
            (tokenResult: ITnsOAuthTokenResult, error) => {
                if (error) {
                    console.error("back to main page with error: ");
                    console.error(error);
                    reject(error);
                } else {
                    console.log("back to main page with access token: ");
                    console.log(tokenResult);
                    resolve(tokenResult);
                }
            }
        );
    });
}

private configureOAuthProviders() {
    const googleProvider = this.configureOAuthProviderGoogle();

    configureTnsOAuth([googleProvider]);
}

private configureOAuthProviderGoogle(): TnsOaProvider {
    const googleProviderOptions: TnsOaProviderOptionsGoogle = {
        openIdSupport: "oid-full",
        clientId:
            "73137137859-5mud9ul9p05giiiicpuf6l8is3ti3n66.apps.googleusercontent.com",
        redirectUri:
            "com.googleusercontent.apps.73137137859-5mud9ul9p05giiiicpuf6l8is3ti3n66:/auth",
        urlScheme:
            "com.googleusercontent.apps.73137137859-5mud9ul9p05giiiicpuf6l8is3ti3n66",
        scopes: ["email", "profile"]
    };
    const googleProvider = new TnsOaProviderGoogle(googleProviderOptions);

    return googleProvider;
}

} `

OniQ commented 4 years ago

I got the same error after clean install. Been able to get rid of it by removing nativescript-plugin-firebase pacakge and platforms catalog. I can't find the way for those two packages to work together yet. Аlthough it worked before I decided to reinstall packages.

OniQ commented 4 years ago

My solution was to retry creation of TnsOAuthClient. It fails with this error on first attempt, but always works on second attempt.

  get oAuthClient() {
    if (!this._oAuthClient) {
      try {
        this._oAuthClient = new TnsOAuthClient('identityServer', true);
      } catch (e) {
        console.error('Failed to create TnsOAuthClient. Retry...', e);
        this._oAuthClient = new TnsOAuthClient('identityServer', true);
      }
    }
    return this._oAuthClient;
  }
dscirto commented 4 years ago

I was setting up a new environment this afternoon and ran into the same error. Here's some additional info I've found along the way:

Omitting the firebase plugin always solved the problem, but that's not great. Using both plugins with tns-ios 6.4.x or 6.5.x resulted in an error, but tns-ios 6.3.0 worked for both plugins

keithgulbro commented 4 years ago

Having the same issue:

nativescript-oauth2/tns-oauth-native-view-controller.ios.js:12:23: JS ERROR Error: Class "SFSafariViewController" referenced by type encoding not found at runtime. (evaluating 'new TnsOAuthLoginNativeViewController()')

Currently on Nativescript 6.5.0 for both platforms with a Nativescript-Vue application.

My conflict is with the nativescript-urban-airship plugin but this issues has seemed to arise with other developers who are seeking to integrate push notifications into their application.

NOTE: Downgrading to Nativescript 6.3.0 does work as a temporary workaround as @dscirto mentioned.

Looks like newer version of NS are soon to be released. Hopefully this bug is accounted for in those upcoming releeases.

cjohn001 commented 4 years ago

Hello together, I am seeing exactly the same issue when using the plugin together with nativescript-plugin-firebase and I tried with NS 6.5.0 and NS 6.5.1. I debugged the issue in the plugin down to the following file

https://github.com/alexziskind1/nativescript-oauth2/blob/master/src/tns-oauth-native-view-controller.ios.ts line 21

the new fails

const instance = new TnsOAuthLoginNativeViewController();

as the SFSafariViewController seems to be unknown. The thing I do not understand here is that the class SFSafariViewController is not imported and that I have not found a nativescript module yet which exports it. Some Nativescript magic on how to use native objects? If someone could tell the correct import it would be great if he mentions it here, as I had to fork the sources due to other topics and would like to get rid of this workaround. Note, what is even more weird to my understanding is, that calling new twice, leads to the previously mentioned behavior. The second call succeeds. Hence, the following code is a workaround that does the job for me. However, it is not clear why it works and is not a real solution. Would be great if someone could help on the topic

Here the workaround:

public static initWithClient(client: TnsOAuthClient) {
        // create view controller instance
        let instance = TnsOAuthLoginNativeViewController.createInstance(client);
        if (!instance) {
            instance = TnsOAuthLoginNativeViewController.createInstance(client);
        }
        return instance;
    }

private static createInstance(client: TnsOAuthClient) {
        let instance: TnsOAuthLoginNativeViewController;
        try {
            instance = new TnsOAuthLoginNativeViewController();

            if (instance) {
                instance.loginController = new TnsOAuthLoginSubController(client);
                // get style options
                if (client.provider.styleOptions) {
                    instance.styleOptions = client.provider.styleOptions;
                }
            }
        } catch (e) {
            console.log(e);
        }
        return instance;
    }
cjohn001 commented 4 years ago

The issue can be closed, see here: https://github.com/alexziskind1/nativescript-oauth2/issues/135