惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
宝玉的分享
宝玉的分享
美团技术团队
量子位
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
IT之家
IT之家
爱范儿
爱范儿
J
Java Code Geeks
博客园 - Franky
Last Week in AI
Last Week in AI
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
GbyAI
GbyAI
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog

博客园 - 胖胖安

Xamarin.Android 的 Google 登入 Xamarin.Android 的照相機使用 Xamarin.iOS 照相機功能的使用 (1) :最簡單的做法 Xamarin.iOS 的鍵盤控制 (AutoLayout 與 新的 Keyboard 事件 ) Xamarin.Android 控制鍵盤縮放 Xamarin 環境建置–Xamarin Agent install rvm iOS : TableView MultiSelect 兩個 System.IO.FileInfo 的 Extension Method SQL Server 2008 Resize LDF IIS 7 站點下,ASP.NET 3.5 與 4.0 的並存問題 jQuery AutoComplete Parameters Tips : SQL Server 2008 "saving changes is not permitted." 的解決辦法 - 胖胖安 Section 3 : 程式進入點 Section 2 : 專案結構 Section 1 : 使用 XCode 建立一個新專案 iPhone 開發前的準備工作 IIS 7 上 设定ASP.NET 时应注意事项 DBML 在Beta2與正式版間差異 - 胖胖安 - 博客园
Xamarin.iOS 的 Google 登入
胖胖安 · 2016-04-19 · via 博客园 - 胖胖安

在 Xamarin.iOS 開發中,使用Google 帳號進行登入,十分簡單。
只要引入 Xamarin 為 Google Single-SignIn 開發的 Component 即可。

首先在專案的 Components 目錄下引入「Google Sign-In for iOS」

然後只要幾行code就可以完成Google Sin-In 的工作。
真正的問題在Server端的設置。

首先到 https://developers.google.com/mobile/add 這邊增加 iOS App
給出 App name 和 bundle id
然後再下一個頁面增加 Google Sign-In 的功能。

接下來是要記得產生設定檔案
然後下載 GoogleService-Info.plst 檔案,加入至專案。

然後在專案總管這邊,Build Action 選成 BundleResource 。

在 Info.plist 這邊要做兩件事情
新增 CFBundleURLTypes 讓 Google 可以辨識

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLTypes</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>填入REVERSE_CLIENT_ID,去GoogleService-Info.plist 內找出來</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleURLTypes</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>在Google 開發者網站填的Bundle Id</string>
            </array>
        </dict>
    </array>

做到這邊,關於設定的工作就做好了。

而程式碼分成兩部分。
皆需要引用以下的 namespace

using Google.Core;
using Google.SignIn;
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)

需要加上這段 Code 讀取 ClientID ,這邊需要去 GoogleService-Info.plist 複製 CLIENT_ID 出來。

NSError configureError;
Context.SharedInstance.Configure (out configureError);
if (configureError != null) {
  Console.WriteLine ("Error configuring the Google context: {0}", configureError);
  SignIn.SharedInstance.ClientID = "要去GoogleService-Info.plist 取得 CLIENT_ID";
}
public override bool OpenUrl (UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
  return SignIn.SharedInstance.HandleUrl (url, sourceApplication, annotation);
}

另外的目標 ViewController 內加入以下的程式碼

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();

    SignIn.SharedInstance.UIDelegate = this;

    SignIn.SharedInstance.SignedIn += (object sender, SignInDelegateEventArgs e) => {

        if (e.User != null && e.Error == null) {
            Debug.WriteLine(e.User.Profile.Name);
        }
    };

    SignIn.SharedInstance.Disconnected += (object sender, SignInDelegateEventArgs e) => {

        if (e.User != null && e.Error == null) {
            Debug.WriteLine(e.User.Profile.Name);
        }

    };

    SignIn.SharedInstance.SignInUserSilently ();

}
SignIn.SharedInstance.SignInUser ();
SignIn.SharedInstance.SignOutUser ();
SignIn.SharedInstance.DisconnectUser ();


分別用於登入,登出和解除予應用程式的授權。

參考資料:Google Sign-In for iOS 的說明。
程式碼位置:https://github.com/FangHuaiAn/Xamarin-iOSTips