Sign in with Apple🔗

Sign in with Apple makes it easy for users to access your apps and websites using their Apple ID. Instead of filling out forms, checking email addresses, and picking new passwords, they can use Apple Sign-in to create an account and start using your app right away. To ensure maximum security, all accounts are protected by two-factor authentication, and Apple will not track user activity in your application or website.
Sign in with Apple is required to implement
According to the Apple App store guidelines, in cases where you have authorization through any third-party service (for example, Facebook), then Sign in with Apple must be necessarily integrated into your application.
To use Sign in with Apple, users must:
- Have iOS 13 and above.
- Have an Apple ID with two-factor authentication (2FA) enabled.
- Sign in to iCloud on your device.
See Apple's official website for more information.
Initial setup🔗
Before you begin, you need to configure certificates and keys in your Apple Developer account in order to activate the Sign in with Apple functionality. Add Sign in with Apple for your application on the Certificates, Identifiers & Profiles page in your developer account.
If you want to send emails to users
If you want to send e-mails to users who, at the entrance, have hidden their email through the Apple Relay service, you need to register your outgoing mail domains, subdomains or email addresses. Read more on the official site
Also, in the Xcode project, in the 'Signing & Capabilities' tab, you need to add Sign in with Apple, for this:
In Unity
Add the code to the Unity post build process:
public class PostBuildProcessSample
{
[PostProcessBuild]
public static void PostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.iOS)
{
#if UNITY_IOS
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
#if UNITY_2019_3_OR_NEWER
string projectTarget = proj.GetUnityMainTargetGuid();
#else
string targetName = PBXProject.GetUnityTargetName();
string projectTarget = proj.TargetGuidByName(targetName);
#endif
//Adding capabilities
#if UNITY_2019_3_OR_NEWER
ProjectCapabilityManager projectManager = new ProjectCapabilityManager(projPath, "ios.entitlements", targetGuid: projectTarget);
#else
ProjectCapabilityManager projectManager = new ProjectCapabilityManager(projPath, "ios.entitlements", targetName);
#endif
projectManager.AddSignInWithApple();
projectManager.WriteToFile();
#endif
}
}
}
Or manually enable it in an already generated project:

In iOS
In XCode project:

Add dependencies🔗
Add the MRGS SDK to the project:
Unity:
Adding to the project (general instruction)
Step 1. Add Sources
To add MRGS to a project via the Unity Package Manager (available from Unity 2018+) simply add a scopedRegistries section to the Packages/manifest.json file by adding the following entry:
{
"dependencies": {
...
},
"scopedRegistries": [
{
"name": "MRGS",
"url": "https://mrgs-nexus.my.games/repository/mrgs-uninty-plugins/",
"scopes": [
"games.my.mrgs"
]
}
]
}
Alternatively, you can click Edit -> Project Settings -> Package Manager -> '+' in scoped registry section, and fill in the fields according to the data above.
Step 2. Add dependency
- Click
Window -> Package Manager -> select 'Packages: MyRegistries' from dropdown list, select packageMRGSAuthenticationfrom the list, then click "Install" - Import the module:
using MRGS;
- Download the latest version of the library. Unzip the archive.
- (For unitypackage integration) In Unity, click
Assets -> Import Package -> Custom Package, and select thegames.my.mrgs.authentication.unitypackagepackage from the downloaded archive. - (For tgz integration) In Unity, click
Window -> Package Manager -> '+' -> Add package from tarball, and select thegames.my.mrgs.authentication-<version> package. tgzfrom the downloaded archive. - Import the module:
using MRGS;
iOS:
Adding to the project (general instruction)
Step 1: Add dependencies
Via Package collection
- In Xcode select File > Add Packages
- Select "+" > "Add Swift Package Collection"
- Insert URL:
https://mrgs-nexus.my.games/repository/ios-sdks/MRGSPackageCollection.json - Select module MRGSAuthentication from "MRGS Package Collection".
- Or you can select "MRGS" package from "MRGS Package Collection" (contains all mrgs modules as products) and then select "MRGS/Authentication" product only.
Individual packages
- In Xcode select File > Add Packages
- In the search bar at the top right, paste the URL:
https://mrgs-gitea.my.games/mrgs/mrgsauthentication-ios-sdk.git - Add a module to your project
- Or you can paste the url
https://mrgs-gitea.my.games/mrgs/ios-sdks.gitto include the "MRGS" package which contains all mrgs modules as products and then select only the product" MRGS/Authentication".
Step 2: Add support for ObjectiveC categories
- Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthentication;or#import <MRGSAuthentication/MRGSAuthentication.h>
Step 1. Add Sources
In your podfile, add sources to the top of the file:
source 'https://github.com/CocoaPods/Specs.git' # For main repo
source 'https://mrgs-gitea.my.games/mrgs/cocoapods-specs.git' # For MRGS repo
Step 2: Add dependencies
Add the latest version of MRGSAuthentication to target:
To add via subspecs:
To add via individual modules:
To add all mrgs modules:
Step 3: Install dependencies
- Run
pod install(orpod install --repo-updateif necessary) - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthentication;or#import <MRGSAuthentication/MRGSAuthentication.h>
Step 1: Add dependencies
Add the dependency to your Cartfile:
binary "https://mrgs-nexus.my.games/repository/ios-sdks/MRGSAuthentication/MRGSAuthentication.json" ~> 5.0.0
Step 2: Install dependencies
- Run
carthage update --use-xcframeworks - Add downloaded frameworks to your project (make sure "do not embed" option is enabled)
- Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthentication;or#import <MRGSAuthentication/MRGSAuthentication.h>
- Download the latest version of the library. Unzip the archive.
-
Add
MRGSAuthentication.xcframeworkfrom the downloaded archive to your project (Drag the libraries to the "Linked frameworks and Libraries" section) (Also contains MRGSAuthentication.framework for compatibility - fat framework) -
Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGSAuthentication;or#import <MRGSAuthentication/MRGSAuthentication.h> - Also, you can add the
MRGServiceKit.handmodule.modulemapfiles from the archive to your project, or specify the path to them in the project settings in theBuild Settings -> Header search pathssection. Now, instead of importing each of our frameworks separately, you can only import one header file:@import MRGServiceKit;
Adding a button🔗
Then, you need to add the 'Sigh in with Apple' button to the login screen, in accordance with Human interface guidelines.
Example of adding a native button object (iOS only)
An example of adding a native button object:
ASAuthorizationAppleIDButton *appleIDButton = [ASAuthorizationAppleIDButton new];
appleIDButton.frame = CGRectMake(.0, .0, 200, 100.0);
appleIDButton.cornerRadius = 10;
[self.view addSubview:appleIDButton];
[appleIDButton addTarget:self action:@selector(handleAuthrization:) forControlEvents:UIControlEventTouchUpInside];
Please note that Sign in With Apple only works on iOS 13 and above, so before showing the button, you need to check whether this functionality is available on the device:
Working with the MRGSAuthentication interface🔗
To work with Sign in with Apple, use the MRGSAuthenticationSignInWithApple class. Please note that this class implements our main protocol MRGSAuthentication, that is, it has all the methods of this protocol. The following describes the standard implementation of the MRGSAuthentication interface:
Delegate🔗
Before you start working with the API, you must install and implement a delegate that accepts callbacks:
// Setting
// 'this' conforms to 'IMRGSAuthenticationDelegate' protocol
MRGSAuthenticationSignInWithApple.Instance.Delegate = this;
// Implementation
// The method that is called when the user has logged out of the social network remotely, or his session has ended.
public void OnAuthenticationProviderDidLogoutUser(MRGSAuthentication provider, MRGSAuthenticationUser user){
Debug.Log("MRGSAuthentication<Unity> - OnAuthenticationProviderDidLogoutUser: " + user + "\nNetwork: " + provider.SocialId().ToString());
}
// Setting
[MRGSAuthenticationSignInWithApple sharedInstance].delegate = self;
// Implementation
// The method that is called when the user remotely logs out of the social network, or his session ends.
- (void)authenticationProvider:(id<MRGSAuthentication>)provider didLogoutUser:(MRGSAuthenticationUser *)user{
NSLog(@"User did logout: \n%@", user);
}
// The method that is called when the controller needs to be displayed to authorize the user.
- (void)shouldPresentAuthorizationController:(UIViewController *)viewController {
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:viewController animated:YES completion:nil];
}
Status🔗
Then, you need to check the authorization status (whether the user is logged in):
Login🔗
For regular login, use one of the methods:
// If error occured, error object will be non-null
MRGSAuthenticationSignInWithApple.Instance.Login((MRGSError error) => {
if (error == null) {
// Auth success
}
});
// The response will include an object describing the user and the token, and an error object, if any.
MRGSAuthenticationSignInWithApple.Instance.Login((MRGSAuthenticationCredential credentials, MRGSError error) => {
Debug.Log("MRGSAuthenticationSignInWithApple:\nCredentials: " + credentials + "\nError:\n" + error);
if (error == null) {
// Auth success
}
});
// Only the object of the error will come in the answer, if it happened
[[MRGSAuthenticationSignInWithApple sharedInstance] login:^(NSError *error) {
if (!error) {
// Auth success
}
}];
// The response will contain an object describing the user and the token, and the object of the error if it happened
[[MRGSAuthenticationSignInWithApple sharedInstance] loginWithCompletionHandler:^(MRGSAuthenticationCredential *credentials, NSError *error) {
NSLog(@"Result - %@. Error - %@", credentials, error);
if (!error) {
// Auth success
}
}];
Login with additional permissions🔗
You can also request additional data from the user:
- Name
Description of enum:
To do this, use the method below, passing an array of additional permissions that you want to receive:
var scopes = new List<string>();
scopes.Add(MRGSAuthenticationSignInWithApple.Scopes.Email);
scopes.Add(MRGSAuthenticationSignInWithApple.Scopes.FullName);
MRGSAuthenticationSignInWithApple.Instance.Login(scopes, (MRGSAuthenticationCredential credentials, MRGSError error) => {
Debug.Log("MRGSAuthenticationSignInWithApple:\nCredentials: " + credentials + "\nError:\n" + error);
if (error == null) {
// Auth success
}
});
Important
Since Sign in with Apple has no “exit” method, and permissions cannot be re-requested until the user has logged out of the account (at the time of writing the documentation, the mechanism is as follows), please note that it is better to immediately fill in the data that you need, otherwise later You won’t be able to update them.
Validate received credentials
After logging in via Sign in with Apple, the application receives an identityToken (stored in MRGSAuthenticationAccessToken.AccessToken) and authorizationCode (stored in MRGSAuthenticationAccessToken.TokenSecret), using the token, you can verify the received data (valid for 600 seconds), and with using the code, you can get a refresh token to refresh the token. This operation must be performed on the side of the project. More details can be found on the Apple website, useful links can also be found here.
Authorization Status🔗
To get all the information about the current authorization status and user information, use the methods:
// The information about the user, identifier, name, email, etc.
MRGSAuthenticationSignInWithApple.Instance.GetCurrentUser((MRGSAuthenticationUser user, MRGSError error) => {
Debug.Log("MRGSAuthenticationSignInWithApple:\User: " + user + "\nError:\n" + error);
if (error == null) {
// Work with user data
}
});
// Information about the token, lifetime, etc.
MRGSAuthenticationSignInWithApple.Instance.GetAccessToken((MRGSAuthenticationAccessToken token, MRGSError error) => {
Debug.Log("MRGSAuthenticationSignInWithApple:\Token: " + token + "\nError:\n" + error);
if (error == null) {
// Work with token info
}
else if (error.Code == (int)MRGSAuthenticationErrorCode.ConnectionFailed)
{
// No network, try later
}
else
{
// Logout user from app (MRGS will logout automatically in SDK)
}
});
// An object is returned that combines information about the user (identifier, name, email, etc.) and about the token.
[[MRGSAuthenticationSignInWithApple sharedInstance] getCredentials:^(MRGSAuthenticationCredential *credentials, NSError *error) {
NSLog(@"Result - %@. Error - %@", credentials, error);
if(!error){
// Credentials data work
}
}];
// User information - identifier, name, email, etc.
[[MRGSAuthenticationSignInWithApple sharedInstance] getCurrentUser:^(MRGSAuthenticationUser *user, NSError *error) {
NSLog(@"User - %@. Error - %@", user, error);
if(!error){
// User data work
}
}];
// Information about the token - lifetime, etc.
MRGSAuthenticationAccessToken* info = [[MRGSAuthenticationSignInWithApple sharedInstance] getAccessToken:^(MRGSAuthenticationAccessToken *token, NSError *error) {
if (!error) {
NSLog(@"getAccessToken result - %@", token);
} else {
NSLog(@"getAccessToken error - %@", error);
}
}];
Logout🔗
In order to "log out" from a user’s account, use the method:
Important
Since Sign in with Apple does not have a "exit" method, the exit is done by deleting session data from us. That is, in fact, the user remains logged in, but we think as he is not. The only way to exit is to go into the system settings and deauthorize the application (in this case, we will call the delegate method didLogoutUser).
Created: 2020-04-13