Xamarin: Issue with latest FB API

2019-06-20 01:01发布

问题:

Latest FB login API has three parameters

public unsafe virtual void LogInWithReadPermissions (string[] permissions, UIViewController fromViewController, [BlockProxy (typeof(Trampolines.NIDLoginManagerRequestTokenHandler))] LoginManagerRequestTokenHandler handler)

I am using MVVMCross. For fb login, I tried created an instance of the view i am in, and pass it as a parameter for LogInWithReadPermissions()

VIEWMODEL:

private async void DoFacebookSignIn()
        {
            try 
            {               
                await facebookService. Login();
                DoAutoLogin();
            }
}

SERVICE:

private readonly string[] permitions = new string[] { "email", "public_profile" };    
public async System.Threading.Tasks.Task LogIn()
            {
    LoginManager.LogInWithReadPermissionsAsync (permitions);

                LoginManagerLoginResult result = await LogInWithReadPermissionsAsync();

                if (result.IsCancelled)
                {
                    ServiceFactory.UserMessageService.ShowToast("Facebook login is canceled");
                }
            }

        private Task<LoginManagerLoginResult> LogInWithReadPermissionsAsync()
            {
                var tcs = new TaskCompletionSource<LoginManagerLoginResult> ();
                LoginManager.LogInWithReadPermissions (permitions,null, (LoginManagerLoginResult result, NSError error) =>
                {
                    if(error.IsNotNull ())
                    {
                        tcs.SetException (new IosErrorException(error));
                    } else 
                    {
                        tcs.SetResult (result);
                    }
                });

                return tcs.Task;
            }

But its failing, Do i need to pass view info from Viewmodel, when I am calling this func? How to pass view instance from view model ? Can anyone help?

UPDATE

It's failing at the service:

func LogInWithReadPermissionsAsync() line3: (LoginManager.LogInWithReadPermissions...)

without giving any error. Its just crashing. The Facebook API version: "Xamarin.Facebook.iOS" version="4.13.1"

UPDATE Removed unused code.

回答1:

I got the solution.

The code was fine I just needed to 'Whitelist Facebook Servers for Network Requests' by adding

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>facebook.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>                
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>fbcdn.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
        <key>akamaihd.net</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>

If you're recompiling with iOS SDK 9.0, add the following to your application's plist if you're using a version of the SDK v4.5 or older:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fbapi20130214</string>
    <string>fbapi20130410</string>
    <string>fbapi20130702</string>
    <string>fbapi20131010</string>
    <string>fbapi20131219</string>    
    <string>fbapi20140410</string>
    <string>fbapi20140116</string>
    <string>fbapi20150313</string>
    <string>fbapi20150629</string>
    <string>fbauth</string>
    <string>fbauth2</string>
    <string>fb-messenger-api20140430</string>
</array>
If you're using Facebook.MessengerShareKit from versions older than the v4.6 release, also add:

<string>fb-messenger-platform-20150128</string>
<string>fb-messenger-platform-20150218</string>
<string>fb-messenger-platform-20150305</string>
If you're using v4.6.0 of the SDK, you only need to add:

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

As mentioned in Xamarin Facebook iOS SDK here.