簡體   English   中英

如何使用WebView在Xamarin.iOS中實現手動Facebook登錄

[英]How to implement manual Facebook login in Xamarin.iOS using webview

我正在嘗試在Xamarin.iOS應用程序中實現Facebook登錄。 我嘗試使用Xamarin.Auth程序包,但有時用戶無法處理,並且此程序包存在一些問題。 我發現最好的方法是對應用程序實施手動登錄流程(使用Web視圖)。 目前,我已經在Faceboook開發人員門戶中創建了一個應用程序,並且可以從我的iOS應用程序訪問該鏈接。

因此,用戶將單擊常規按鈕,該按鈕將被轉發到Facebook登錄頁面。 我的問題是,如何從Facebook登錄頁面獲取結果? 以及如何獲取用戶ID,電子郵件等?

這是到目前為止的源代碼:

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);
    }

在下載官方SDK(Xamarin.Facebook.iOS)之后,我通過使用Facebook SDK(目前被認為是最好的現有解決方案)找到了答案,請按以下步驟操作:

  1. 在Info.plist文件中,添加以下代碼:

 <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. 同樣在Info.plist文件的原始視圖,“高級”選項卡,“ URL類型”中,以這種方式添加您的Facebook應用ID“ fb1112222 ......”。 開頭請保持“ fb”。

圖片:Info.plist中的Facebook應用ID

  1. 在AppDelegate.cs中,重寫以下方法:

      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. 最后,這是Facebook的功能,您可以使用自己的按鈕,也可以使用您的按鈕。 以我的方式,我在按鈕上使用了名稱(BtnFacebookLogin)。

    //這是facebook函數

    私人無效SetupFacebookLoginButton(){列表readPermissions =新列表{“ public_profile”,“電子郵件”};

      ////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 }); } } 

有關更多信息,請參考GitHub中的示例( Link

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM