Flutter : Sign in With Apple
Introduction
Recently Apple launched its own Login method.Sign in with Apple is an alternative to the existing sign in with Google and Facebook options that apps and websites often offer. Apple’s version protects your privacy and even lets you mask your email address.
In this article, I will be showing how to set up and implement Apple Sign In with Flutter.
Note : It can only be implemented in ios and only in mac having xCode(Software to develop ios apps). For Android We can use Google Login.
Step 1
Add the dependency to your pubspec.yaml file . You can get the latest version from https://pub.dev/packages/apple_sign_in
dependencies:
apple_sign_in: ^0.1.0
You can install packages from the command line:
$ flutter pub get
Alternatively, your editor might support flutter pub get like
Android Studio and Visual Studio code .
Step 2
Configuration in Xcode
Open the Flutter project with Xcode by doing right click on the project then Flutter > Open ios module in Xcode .
|
|
|
|
|
In Xcode go to the project settings and click on Signing & Capabilities .
|
Click on the Capability and a dialog box will appear where we should search for Sign in with apple and click on the result.
|
|
|
|
Step 3
The real step ( coding )
- import the “apple_sign_in.dart” to use it’s functionality .
import 'package:apple_sign_in/apple_sign_in.dart';
2. In initState( ) of Scaffold initialise the Functionality .
if(Platform.isIOS){ //check for ios if developing for both android & ios
AppleSignIn.onCredentialRevoked.listen((_) {
print("Credentials revoked");
});
}
3. Place the Apple Sign in button Specially provided for this operation .
AppleSignInButton(
style: ButtonStyle.black,
type: ButtonType.continueButton,
onPressed: appleLogIn,
);
4. Define the appleLogIn( ) method for the sign in functionality
if(await AppleSignIn.isAvailable()) {
//Check if Apple SignIn isn available for the device or not
}else{
print('Apple SignIn is not available for your device');
}
If Available then We can request for login
if(await AppleSignIn.isAvailable()) {
final AuthorizationResult result = await
AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
}
Handle the result according to your requirement
if(await AppleSignIn.isAvailable()) {
final AuthorizationResult result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
switch (result.status) {
case AuthorizationStatus.authorized:
print(result.credential.user);//All the required credentials
case AuthorizationStatus.error:
print("Sign in failed: ${result.error.localizedDescription}");
break;
case AuthorizationStatus.cancelled:
print('User cancelled');
break;
}
}
You can send the credentials to your backend on whatever you want to do.
Step 4
Running the app
Conclusion
Now, our “Login with Apple Tutorial” is complete. I hope that you have received some useful information from this article.
You can follow me on Twitter and LinkedIn . Also, don’t forget to checkout our Website. Thank you for reading, if you enjoyed the article make sure to show me some love by hitting that clap (👏) button!
Happy coding…
Guru Prasad Mohapatra