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/

i_view64_UN4n1t4ZCe.png


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: 77
Last edited:

Vins

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

Filippo

Expert
Licensed User
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
 

Filippo

Expert
Licensed User
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:
Top