iOS Question B4XPages - google and facebook authentication

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello all,

I'm implement this resource (https://www.b4x.com/android/forum/t...-your-users-google-facebook.68625/post-435340) in a B4XPages App and noticed that for B4i there is a lot of code in Main module.
Considering that following B4X best practices implementation I must to avoid to put code in Main, preferring the B4XMainPage module (only the Ios exclusive code should be in Main ) and there is no "page" object (it's required to use B4X Pages), I changed the code to :
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private btnSignInFacebook As Button
    Private btnSignInGoogle As Button
    Private btnSignOut As Button
    
    Public analytics As FirebaseAnalytics
    Public auth As FirebaseAuth
    Public facebook As FacebookSdk
    Public FacebookAppId As String = "xxxxxxxxxxxxxxx"
    Private lblName As Label
    
End Sub

Public Sub Initialize
    
    analytics.Initialize
    B4XPages.GetManager.LogEvents = True
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    auth.Initialize("auth")
    facebook.Initialize(FacebookAppId)
    
End Sub

Private Sub Auth_SignedIn (User As FirebaseUser)
    SetState
End Sub


'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Private Sub SetState
    Dim SignedIn As Boolean = auth.CurrentUser.IsInitialized
    If SignedIn Then
        lblName.Text = "Hello: " & auth.CurrentUser.DisplayName
    End If
    btnSignInGoogle.Enabled = Not(SignedIn)
    btnSignInFacebook.Enabled = Not(SignedIn)
    btnSignOut.Enabled = SignedIn
End Sub


Sub btnSignOut_Click
    auth.SignOutFromGoogle
    facebook.SignOut
    lblName.Text = "Goodbye!"
    SetState
End Sub

Sub btnSignInFacebook_Click
    facebook.SignIn
End Sub

Sub btnSignInGoogle_Click
    
    auth.SignInWithGoogle(B4XPages.GetPage("mainPage"))   
    
End Sub

Then I have one question and one problem:
1. Question: is this correct to change the routines from Main to B4XMainPage in this case?

Problem:
2. I don't know what to do with this piece of code, as there is no direct page manipulation in B4XPages:
B4X:
    auth.SignInWithGoogle(B4XPages.GetPage("mainPage"))
It's raising and error (in the original example there was page instead of B4XPages.GetPage("mainPage"))...

Thanks everybody!
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
B4XPages.GetNativeParent(Me) will return the native Page object.
Thanks @Erel . That's it: it was requiring the native object! Worked!

Looks correct. I assume that you ran it and it worked
I'm trying to build B4XPages apps as compatible as possible between platforms... But sometimes I still have some doubts about what is platform specific.
Thanks for your help ! ?
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
1. Looks correct. I assume that you ran it and it worked.

2. B4XPages.GetNativeParent(Me) will return the native Page object.

Ok... A little problem here...
Google authentication is running fine but sign in with Facebook no. Clicking the Sign In With Facebook opens the FB authentication site but, after logged in, the webview returns to the start (isn't automatically closed as using Google) and doesn't go back to the app .
The code in B4XMainPage is in the first post... the code in Main module is:
Main:
#Region  Project Attributes
    #ApplicationLabel: goLunch
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    '#iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone
    #ATSEnabled: True
    #MinVersion: 8

#End Region

    #ProvisionFile: Comten_goLunchCustomer.mobileprovision
    #UrlScheme: XXXXXXX

    #QueriesSchemes: fbapi
    #QueriesSchemes: fb-messenger-api
    #QueriesSchemes: fbauth2
    #QueriesSchemes: fbshareextension
    #UrlScheme: fbxxxxxx
    

Sub Process_Globals
    
    Public App As Application
    Public NavControl As NavigationController


End Sub

Private Sub Application_Start (Nav As NavigationController)
    
    NavControl = Nav
    Dim PagesManager As B4XPagesManager
    PagesManager.Initialize(NavControl)
    
End Sub

Private Sub Application_OpenUrl (Url As String, Data As Object, SourceApplication As String) As Boolean
    
    If B4XPages.MainPage.facebook.OpenUrl(Url, Data, SourceApplication) Then Return True
    If B4XPages.MainPage.auth.OpenUrl(Url, Data, SourceApplication) Then Return True
    Return False
    
End Sub

'Template version: B4i-1.0
#Region Delegates
Private Sub Application_Background
    B4XPages.Delegate.Activity_Pause
End Sub

Private Sub Application_Foreground
    B4XPages.Delegate.Activity_Resume
End Sub
#End Region

What am I doing wrong???
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
1. Add a log message to see whether Application_OpenUrl is called.
2. If it is not called then the problem is not related to the code you posted. Maybe something else is missing.
Ok... there is a log BEFORE (when clicking on FB Authentication Button). This one:

B4X:
-canOpenURL: failed for URL: "fbauth2:/" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

Error 10814... is it something related to FB App configuration in FB panel?


UPDATE:

I got this log more when the app starts:

B4X:
FBSDKLog: Missing [FBSDKAppEvents appEventsState.appID] for [FBSDKAppEvents flushOnMainQueue:]

What means?
 
Last edited:
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
1. Add a log message to see whether Application_OpenUrl is called.
2. If it is not called then the problem is not related to the code you posted. Maybe something else is missing.
Ok!!!
After some google search, I found some reports that told that Facebook app must to be installed in iPhone in order to FB Auth works (shouldn't be, but...). I installed FB App in iPhone and worked (!), which means that Fb Auth only runs if the Facebook app is installed.
I don't know if it is something new caused by new Apple policies (they are fighting against Facebook) or because some version of FBSDK, but the fact is:

"Facebook app must to be installed in IOS to FB Authentication runs. This doesn't run over webview." (this is an important information for developers if is correct...)

Maybe I did some mistake but that's what I got in my tests here (@Erel - is this conclusion correct? ). My personal opinion: if I isn't a mistake, while the dispute between Apple and Fb persists, it's not safe to trust in Fb Authentication in IOS...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
I've tested it on a device without Facebook app. I do see the facebook page, however I'm getting an error about the Facebook app not being in production. I guess that it is a new requirement.

Strange... I think that the implementation in my app is ok because it authenticates if FB app is installed, even tough doesn't work without it. If you want @Erel , I can post the full app here (it's yet only with authentication screen and so I can zip and post) and then you can evaluate what's the difference between your implementation and mine. Could be?
To be able to find out why my app is requiring FB installed could be a good information to developers in order to avoid the same mistake (if there is one), or alert about some FB authentication exception (if it is...).
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
You can send me the project with your FB app configuration and I'll try it. Try to make a small project based on: https://www.b4x.com/android/forum/t...-your-users-google-facebook.68625/post-435340

I didn't make a full test as my FB app is not fully working.
Hello @Erel ,

follows the link to B4i/B4XPages app with the authentication (google + FB). You must to fill your Firebase and Facebook credentials. If it works with your credentials without to require the installation of Facebook app in iPhone at least I'll know that there is something wrong with my Facebook app configuration... let's see.

Thanks!

 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Bad:
B4X:
query = "create table if not exists notifications (id int,idRest int,ifoodShortId text default '',idProd text,notificationType text" _
    & ",nomeFantasia text default '',notificationTitle text,notificationBody text,notificationImage text,notificationLink text,expires int default 0, " _
    & "prodName text default '',whatsApp text default '',received int default 0,open int default 0,clicked open default 0,expiresAt int default 0)"
Good:
B4X:
query = $"create table if not exists notifications (id int,idRest int,ifoodShortId text default '',idProd text,notificationType text
                   ,nomeFantasia text default '',notificationTitle text,notificationBody text,notificationImage text,notificationLink text,expires int default 0, 
                   prodName text default '',whatsApp text default '',received int default 0,open int default 0,clicked open default 0,expiresAt int default 0)"$

Good that you insisted as I see that the instructions weren't up to date.
B4X:
#QueriesSchemes: fbapi
#QueriesSchemes: fb-messenger-api
#QueriesSchemes: fbauth
#QueriesSchemes: fbauth2
#QueriesSchemes: fbshareextension
#UrlScheme: fb333333330041798
#PlistExtra: <key>FacebookAppID</key><string>333333330041798</string>
#PlistExtra: <key>FacebookDisplayName</key><string>B4A Test1</string>

Fixed now.
 
Upvote 0
Top