Fork us on GitHub

Sign-in with Apple Support

New cn1lib adds support for Sign-in-with Apple
Sign-in with Apple Support

Sign-in with Apple Support

We have just finished the initial release of our "Sign-in with Apple" cn1lib, which adds "Sign-in with Apple" support to Codename One apps. On iOS 13 and higher, this will use Apple’s native Authentication framework. On other platforms (e.g. Android, Desktop, and Simulator), this will use Apple’s Oauth2 authentication service.

The main motivation for adding this functionality is that Apple would require apps that use "sign in with…​" to support its service too. If you won’t support sign in with Apple but include support signin with Facebook/Google your app might be rejected in the future.

If your app doesn’t require sign-in or uses custom login logic there’s no requirement to support "sign in with Apple"

Getting Started

The hardest part of adding Apple sign-in support house-keeping you need to perform in Apple’s developer portal. If you only intend to support Apple sign-in with your iOS app, and not on other platforms, then the process is pretty simple - you just check a box next to "Sign-in with Apple" in the capabilites section of your App ID details page. Set-up for other platforms is a bit more involved. You need to create a "Services ID" (used for the Oauth2 client ID), and generate a private key so you will be able to generate the Oauth2 client secret on-demand.

For full instructions see the setup documentation in the cn1lib’s wiki.

You can find the plugin in Codename One Settings. Once you’ve added the cn1lib to your Codename One project, you can begin using the AppleLogin class to provide Apple sign-in support.

The Code

The following is an example of how to add Apple login support to your app.

AppleLogin login = new AppleLogin();
// If using on non-iOS platforms, set Oauth2 settings here:
// login.setClientId(...);
// login.setKeyId(...);
// login.setTeamId(...);
// login.setRedirectURI(...);
// login.setPrivateKey(...);

if (login.isUserLoggedIn()) {
    new MainForm().show();
} else {
    new LoginForm().show();
}


....


class LoginForm extends Form {
    LoginForm() {
        super(BoxLayout.y());
        $(getContentPane()).setPaddingMillimeters(3f, 0, 0, 0);
        add(FlowLayout.encloseCenter(new Label(AppleLogin.createAppleLogo(0x0, 15f))));


        Button loginBtn = new Button("Sign in with Apple");
        AppleLogin.decorateLoginButton(loginBtn, 0x0, 0xffffff);

        loginBtn.addActionListener(evt->{
            login.doLogin(new LoginCallback() {
                @Override
                public void loginFailed(String errorMessage) {
                    System.out.println("Login failed");
                    ToastBar.showErrorMessage(errorMessage);
                }

                @Override
                public void loginSuccessful() {
                    new MainForm().show();
                }
            });
        });

        add(FlowLayout.encloseCenter(loginBtn));


    }
}


....

class MainForm extends Form {
    MainForm() {
        super(BoxLayout.y());
        add(new SpanLabel("You are now logged in as "+login.getEmail()));
        Button logout = new Button("Logout from Apple");
        logout.addActionListener(e->{
            login.doLogout();
            new LoginForm().show();
        });
        add(logout);
    }
}

For full working demo, see the Demo app

Some screenshots from the demo:

Sign-in with apple demo

More information

  1. See the Github project for the Apple Sign-in cn1lib.

  2. See the Demo project for an example.

  3. See the set-up instructions detailed instructions on adding support for Apple Sign-in.

  4. See Apple’s documentation for Sign-in with Apple.

Share this Post:

Posted by Steve Hannah

Steve writes software for Codename One. He is an open source enthusiast who loves to tinker with new technologies.