Nitish
Nitish

Reputation: 104

Xamarin facebook sdk for iOS causing app crash

I am using xamarin facebook sdk for iOS in a xamarin project with shared code and forms UI along with Azure Mobile app backend. I am trying to do client flow login using this sdk but am not able to. The app crashes on device as soon as its launched. However, when using the similar approach on android side (using xamarin facebook sdk for android) everything works superb. My code is based on what Adrian Hall has suggested in his book. It works fine in simulator and opens up the web view for login but fails on device. Following is the code in iOS project:

public class iOSLoginProvider : ILoginProvider
{
    public async Task LoginAsync(MobileServiceClient client)
    {
        var accessToken = await LoginFacebookAsync();
        var zumoPayload = new JObject()
        {
            ["access_token"] = accessToken
        };
        await client.LoginAsync("facebook", zumoPayload);
    }

    public UIViewController RootView => UIApplication.SharedApplication.KeyWindow.RootViewController;

    private TaskCompletionSource<string> fbtcs;

    public async Task<string> LoginFacebookAsync()
    {
        fbtcs = new TaskCompletionSource<string>();
        var loginManager = new LoginManager();

        loginManager.LogInWithReadPermissions(new[] { "public_profile" }, RootView, LoginTokenHandler);
        return await fbtcs.Task;
    }
    private void LoginTokenHandler(LoginManagerLoginResult loginResult, NSError error)
    {
        if (loginResult.Token != null)
        {
            fbtcs.TrySetResult(loginResult.Token.TokenString);
        }
        else
        {
            fbtcs.TrySetException(new Exception("Facebook Client Flow Login Failed"));
        }
    }
}

I have added requirement permissions in info.plist and have whitelisted facebook apps as well. When I comment out LoginFacebookAsync() and LoginTokenHandler(), the app runs fine. It launches perfectly, although doesn't do login because code is not complete but it does launch. However as soon as these methods are in code, the app fails to launch. I have tried compiling on two devices (iOS 9.3.5 and iOS 10.3.3) and simulators as well.

Update Following is the crash report from device log:

Incident Identifier: 8AEC3E39-8670-4C6B-A87E-9B2C014B1A6E
CrashReporter Key:   ce11b8d9b291dbec2269487cc1ef04b41d8b4d35
Hardware Model:      iPhone6,1
Process:             fbloginios.iOS [646]
Path:                /private/var/containers/Bundle/Application/CFE5351F-4DE4-4822-9B42-8C9CE909D905/fbloginios.iOS.app/fbloginios.iOS
Identifier:          com.yourcompany.fbloginios
Version:             1.0 (1.0)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.yourcompany.fbloginios [543]


Date/Time:           2017-11-03 08:58:39.0078 -0400
Launch Time:         2017-11-03 08:58:38.5767 -0400
OS Version:          iPhone OS 10.3.3 (14G60)
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Application Specific Information:
abort() called

Filtered syslog:
None found

Last Exception Backtrace:
0   CoreFoundation                  0x18a332fe0 __exceptionPreprocess + 124
1   libobjc.A.dylib                 0x188d94538 objc_exception_throw + 56
2   fbloginios.iOS                  0x101596494 0x1000c8000 + 21816468
3   fbloginios.iOS                  0x1015cfbd8 0x1000c8000 + 22051800
4   libobjc.A.dylib                 0x188d95418 CALLING_SOME_+initialize_METHOD + 24
5   libobjc.A.dylib                 0x188d95684 _class_initialize + 612
6   libobjc.A.dylib                 0x188d9d4b4 lookUpImpOrForward + 228
7   libobjc.A.dylib                 0x188da8478 _objc_msgSend_uncached + 56
8   fbloginios.iOS                  0x1000ef02c 0x1000c8000 + 159788
9   fbloginios.iOS                  0x1000effd0 0x1000c8000 + 163792
10  fbloginios.iOS                  0x1017281a8 0x1000c8000 + 23462312
11  fbloginios.iOS                  0x1000f00d4 0x1000c8000 + 164052
12  libdyld.dylib                   0x18921d59c start + 4

Upvotes: 0

Views: 724

Answers (1)

Nitish
Nitish

Reputation: 104

I've found the solution to this issue. The issue is with info.plist. With sdk 4.6.0 and above the requirements in info.plist LSApplicationQueriesSchemes tag is only:

<key>LSApplicationQueriesSchemes</key>
<array>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
</array>

I was adding the requirements as per older version of sdk and that was causing the issue. I was following xamarin's official recipe for this which still has older code. Hope it helps someone who runs into similar issue.

Upvotes: 1

Related Questions