B4i Library User Authentication with Apple ID

If you are using FirebaseAuth then use this class instead: https://www.b4x.com/android/forum/threads/sign-in-with-apple-and-firebase.116280/



Starting from iOS 13 there is integral support for letting the user sign in with the user Apple ID.

The AppleAuthButton is a custom view that manages authentication.

You need to add the AuthResult event which will be raised when the user successfully signs in:
B4X:
Sub AppleAuthButton1_AuthResult (Name As String, Email As String)
    Msgbox($"Name: ${Name}
Email: ${Email}"$, "")
End Sub

Configuration:
1. Enable 'Sign In With Apple' in the app identifier page in Apple developer console. The app id must be a non-wildcard id.
2. Download an updated provision profile and set it with:
B4X:
#ProvisionFile: xxx.mobileprovision

3. Add to main module:
B4X:
#AdditionalLib: AuthenticationServices.framework
#MinVersion: 13
#Entitlement: <key>com.apple.developer.applesignin</key><array><string>Default</string></array>

4. Add the class to your project and add the custom view.

Depends on: iXUI library.

Notes

The user will only be able to authenticate successfully once. The username and email are empty if the user tries to sign in again.
This means that you must save the username and email and don't let the user sign in again.
During development you can reset it in the settings app:

Settings > Apple Id > Password & Security > Apple ID logins > {YOUR APP} > Stop using Apple ID
 

Attachments

  • AppleAuthentication.zip
    3.8 KB · Views: 120
Last edited:

Vins

Member
Licensed User
Is Apple authentication only functional from version. 13? For the previous ones, how can I do?
 
D

Deleted member 103

Guest
Hi,

this query always returns "False", tested with iphon-X iOS 14.5.
It worked before, has anything changed recently?

B4X:
If credential.GetField("email").IsInitialized

B4X:
Private Sub Auth_Result(Success As Boolean, Result As Object)
    If Success Then
        Dim no As NativeObject = Result
        Dim credential As NativeObject = no.GetField("credential")
        If GetType(credential) = "ASAuthorizationAppleIDCredential" Then
            Dim email, name As String
            If credential.GetField("email").IsInitialized Then
                Dim formatter As NativeObject
                name = formatter.Initialize("NSPersonNameComponentsFormatter").RunMethod("localizedStringFromPersonNameComponents:style:options:", _
                    Array(credential.GetField("fullName"), 0, 0)).AsString
                email = credential.GetField("email").AsString
                Log(email)
                Log(name)
                CallSub3(mCallBack, mEventName & "_AuthResult", name, email)
            End If
        Else
            Log("Unexpected type: " & GetType(credential))
        End If
    Else
        CallSub(mCallBack, mEventName & "_Cancel")
    End If
End Sub
 
D

Deleted member 103

Guest
Check the "Notes" section in the first post.
But when the app is uninstalled, this info is also deleted.
If the app is installed again, then my app no longer knows this information.
How can I find out if my app has already asked for the Apple-Id?

Ok, I think if I save the info with so, then should be better, right?
B4X:
            Dim p As Phone
            p.KeyChainPut(AppName, E-Mail & ";" & Name)

Or is there a way to read these settings?
Settings > Apple Id > Password & Security > Apple ID logins > {YOUR APP} > Stop using Apple ID
 
Last edited by a moderator:

astronald

Active Member
Licensed User
Longtime User
Hello default is in dark mode is posiblle switch to white?
thank for your answer...
 

stevenindon

Active Member
Licensed User
Hello All,

I have implemented the above code and successfully logged in using Apple ID with the above example download. The problem is on the second log in,
how can i use the SAVED name and email (In which i get in the first time login) as Apple Sign In menu keeps pops up?

I wanted to use the SAVED Name and Email when i click on the same button. (On the second login onwards)
 

stevenindon

Active Member
Licensed User
Erel noted. If user uninstall the app (All username and email will be lost in my app)... and once the user install back the app, Apple log-in wont pop up anymore.

*I noticed that if i uninstall the app, "App Using Apple ID" in IOS settings did not clear the App. This will prevent the app (After reinstall) to pop up for Apple ID Signin menu.
 
Last edited:

stevenindon

Active Member
Licensed User

Erel, Thanks and after searching through the forum, I found out that using :

p.KeyChainPut("E-Mail",Email)

p.KeyChainGet("E-Mail")

Solved the problem. Even tho uninstall, these info will still be in the phone... and can be reuse for Apple Login.

Thanks again.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…