2017-12-26 16 views
0

Ich versuche, Facebook Login in meiner Xamarin.iOS-Anwendung zu implementieren. Ich habe versucht, Xamarin.Auth-Paket zu verwenden, aber manchmal Benutzer kann nicht verarbeitet werden und es gibt einige Probleme mit diesem Paket. Ich fand den besten Weg, manuellen Login-Flow zu meiner Anwendung (mit Web-View) zu implementieren. Derzeit habe ich eine App im Faceboook-Entwicklerportal erstellt und kann den Link von meiner iOS-Anwendung aus aufrufen.So implementieren Sie die manuelle Facebook-Anmeldung in Xamarin.iOS mit Hilfe der Webansicht

So wird der Benutzer auf normale Schaltfläche klicken, die an Facebook Login-Seite weitergeleitet werden. Meine Frage ist, wie bekomme ich das Ergebnis von Facebook Login-Seite? und wie kann ich Benutzer-ID, E-Mail, ... bekommen? Hier

ist der Quellcode so weit:

partial void BtnFacebookLogin_TouchUpInside(UIButton sender) 
    { 
     NSUrl apiRequest = 
      new NSUrl("https://www.facebook.com/dialog/oauth?client_id=" 
      + SharedResources.fbClientId 
      + "&response_type=token&redirect_uri=" 
      + SharedResources.fbRedirectUrl); 

     UIApplication.SharedApplication.OpenUrl(apiRequest); 
    } 
+0

Ich würde empfehlen, dass Sie den offiziellen Facebook-iOS-API verwenden, hat Xamarin ein nuget/Paket, das die native SDK bindet https : //www.nuget.org/packages/Xamarin.Facebook.iOS/ – SushiHangover

+0

Vielen Dank für Ihren Kommentar. Ich habe versucht, FB-Login mit ihrem offiziellen SDK zu implementieren. Aber leider gibt es dafür keine Probe oder Anleitung. Wenn Sie ein Beispiel für die Implementierung der FB-Anmeldung mit ihrem offiziellen SDK haben, werde ich das zu schätzen wissen. –

+0

Beispielcode: https://github.com/xamarin/FacebookComponents/tree/master/Facebook.iOS/samples – SushiHangover

Antwort

0

ich die Antwort gefunden von Facebook-SDK, die derzeit als die beste vorhandenen Lösung angesehen wird, nach dem Download auf die offizielle SDK (Xamarin.Facebook.iOS hier sind), werden die Schritte:

  1. In Info.plist Datei, fügen diese Codes:

<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> 
 
    <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> 
 
    <string>fb-messenger-api</string> 
 
    <string>fbauth2</string> 
 
    <string>fbshareextension</string> 
 
    </array>

  1. Auch in Info.plist-Datei, die ursprünglichen Ansicht, Registerkarte Erweitert, URL-Typen, fügen Sie Ihre Facebook-App-ID wie auf diese Weise „fb1112222 ...... ". Halte 'fb' am Anfang.

Pic: Facebook app ID in Info.plist

  1. In AppDelegate.cs, diese Methoden außer Kraft setzen:

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) 
        { 
         // Override point for customization after application launch. 
         // If not required for your application you can safely delete this method 
    
         Profile.EnableUpdatesOnAccessTokenChange(true); 
         Settings.AppID = "fb app id"; 
         Settings.DisplayName = "fb app name"; 
    
         // This method verifies if you have been logged into the app before, and keep you logged in after you reopen or kill your app. 
         return ApplicationDelegate.SharedInstance.FinishedLaunching(application, launchOptions); 
    
         //return true; 
        } 
    
        public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation) 
        { 
         // We need to handle URLs by passing them to their own OpenUrl in order to make the SSO authentication works. 
         return ApplicationDelegate.SharedInstance.OpenUrl(application, url, sourceApplication, annotation); 
        } 
    
  2. Schließlich dies die Funktion für Facebook ist, können Sie ihr eigener Knopf, oder benutze deinen Knopf. In meiner Art habe ich meinen Knopf mit dem Namen (BtnFacebookLogin) benutzt.

    // Hier ist die Facebook-Funktion

    private void SetupFacebookLoginButton() { Liste readPermissions = new List { "public_profile", "E-Mail"};

     ////Set Up Button Design 
         var fbBtnText = new NSAttributedString("login with facebook", new 
          UIStringAttributes() 
         { 
          ForegroundColor = UIColor.White 
         }); 
    
    
         BtnFacebookLogin.SetAttributedTitle(fbBtnText, UIControlState.Normal); 
         BtnFacebookLogin.SetBackgroundImage(
          UIImage.FromBundle("btn_long_blue.png"), UIControlState.Normal); 
    
         //Strat Login Functions 
         Profile.Notifications.ObserveDidChange((sender, e) => 
         { 
          if (e.NewProfile == null) 
           return; 
    
          if (AccessToken.CurrentAccessToken != null) 
          { 
           var request = new GraphRequest("/me?fields=name,email", null, AccessToken.CurrentAccessToken.TokenString, null, "GET"); 
           request.Start((connection, result, error) => 
           { 
            // Handle if something went wrong with the request 
            if (error != null) 
            { 
             showAlert("Error", error.Description); 
             return; 
            } 
    
            fbReponseFromSDK facebookSDKLoginItem = new fbReponseFromSDK(); 
            // Get your profile name 
            var userInfo = result as NSDictionary; 
            if(userInfo["id"] != null) 
            { 
             Console.WriteLine("id is: " + userInfo["id"].ToString()); 
            } 
            if (userInfo["name"] != null) 
            { 
             Console.WriteLine("name is: " + userInfo["name"].ToString()); 
            } 
            if (userInfo["email"] != null) 
            { 
             Console.WriteLine("email is: " + userInfo["email"].ToString()); 
            } 
            //(Success) Do what you want next : 
            doneFacbookLogin(); 
           }); 
          } 
         }); 
    
         // Handle actions once the user is logged in 
         BtnFacebookLogin.ReadPermissions = readPermissions.ToArray(); 
         BtnFacebookLogin.Completed += (sender, e) => 
         { 
          if (e.Error != null) 
          { 
           // Handle if there was an error 
           showAlert("Facebook Login", e.Error.Description); 
           return; 
          } 
    
          if (e.Result.IsCancelled) 
          { 
           // Handle if the user cancelled the login request 
           //showAlert("Facebook Login", "Login Cancelled"); 
           return; 
          } 
    
          showAlert("Facebook Login", "Login Successfull"); 
         }; 
    
         // Handle actions once the user is logged out 
         BtnFacebookLogin.LoggedOut += (sender, e) => 
         { 
          // Handle your logout 
         }; 
    
         // If you have been logged into the app before, ask for the your profile name 
         if (AccessToken.CurrentAccessToken != null) 
         { 
          var request = new GraphRequest("/me?fields=name,email", null, AccessToken.CurrentAccessToken.TokenString, null, "GET"); 
          request.Start((connection, result, error) => 
          { 
           // Handle if something went wrong with the request 
           if (error != null) 
           { 
            showAlert("Error", error.Description); 
            return; 
           } 
    
           // Get your profile name 
    
          }); 
         } 
        } 
    

Für mehr Informationen, um ihre Proben in GitHub refere (Link)

Verwandte Themen