Xamarin.ios 谷歌登录后如何返回应用程序

Xamarin.ios 谷歌登录后如何返回应用程序,xamarin.ios,xamarin.forms,Xamarin.ios,Xamarin.forms,我正在尝试使用xamarin.auth在我的应用程序中实现google登录,如下所示 var auth = new OAuth2Authenticator("284202576320-7kgdhaa5sgvkoe03jmmcv0p8lfdma306.apps.googleusercontent.com","cAZW7uegD-h2- tNMMf5q1UGQ","https://www

我正在尝试使用xamarin.auth在我的应用程序中实现google登录,如下所示

var auth = new OAuth2Authenticator("284202576320-7kgdhaa5sgvkoe03jmmcv0p8lfdma306.apps.googleusercontent.com","cAZW7uegD-h2-
                                                         tNMMf5q1UGQ","https://www.googleapis.com/auth/userinfo.email",new 
                                                         Uri("https://accounts.google.com/o/oauth2/auth"),new 
                                                         Uri("http://dev.myfav.restaurant/Account/LoginComplete"),new 
                                                         Uri("https://accounts.google.com/o/oauth2/token"),null,true)
    {
        AllowCancel = true,                
    };
但已完成的事件未触发,并在登录后进入网页:( 我在犯错误

我需要让用户回到我的应用程序我如何才能做到这一点???谁能帮我这个请


在iOS中提前感谢

一旦您使用Xamarin.Auth完成身份验证您只需关闭viewController,即可将您放回应用程序中

您可以通过订阅
OAuth2Authenticator的
Completed
事件来执行此操作

auth.Completed += (sender, e) =>
{
    DismissViewController(true, null);
};
如果使用“本机UI”(构造函数中的最后一个参数设置为true),这意味着外部/系统浏览器用于登录而不是WebView。因此,在Android上而不是WebView上使用[Chrome]自定义选项卡,在iOS上而不是UIWebView(或WKWebView)上使用SFSafariViewController

使用本机UI时,用户将离开应用程序,返回应用程序的唯一方法是应用程序链接(或深度链接),这需要完全不同的方法

首先,您不能对
重定向url
使用http[s]方案(在Android上可以,但在iOS上不可以)。请使用自定义方案

请参阅示例(Xamarin.Forms ComicBook):

以及回购协议中的文件:


请遵循以下两个示例:一个是使用web视图,另一个是使用google auth的google登录sdk。 和 所以根据Mounika.Kola报道的这个问题。我认为你应该引用验证器。Completed-=OnAuthCompleted在你的代码中。参考一下我在Xamarin表单中用于谷歌授权的代码

void OnLoginClicked(object sender, EventArgs e)
{
string clientId = null;
string redirectUri = null;

        switch (Device.RuntimePlatform)
        {
            case Device.iOS:
                clientId = Constants.iOSClientId;
                redirectUri = Constants.iOSRedirectUrl;
                break;

            case Device.Android:
                clientId = Constants.AndroidClientId;
                redirectUri = Constants.AndroidRedirectUrl;
                break;
        }

        var authenticator = new OAuth2Authenticator(
            clientId,
            null,
            Constants.Scope,
            new Uri(Constants.AuthorizeUrl),
            new Uri(redirectUri),
            new Uri(Constants.AccessTokenUrl),
            null,
            true);

        authenticator.Completed += OnAuthCompleted;
        authenticator.Error += OnAuthError;

        AuthenticationState.Authenticator = authenticator;

        var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
        presenter.Login(authenticator);
    }
async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
{

        var authenticator = sender as OAuth2Authenticator;
        if (authenticator != null)
        {
            authenticator.Completed -= OnAuthCompleted;
            authenticator.Error -= OnAuthError;
        }

        User user = null;
        if (e.IsAuthenticated)
        {
            // If the user is authenticated, request their basic user data from Google
            // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo
            var request = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account);
            var response = await request.GetResponseAsync();
            if (response != null)
            {
                // Deserialize the data and store it in the account store
                // The users email address will be used to identify data in SimpleDB
                string userJson = await response.GetResponseTextAsync();
                user = JsonConvert.DeserializeObject<User>(userJson);
            }

            if (account != null)
            {
                store.Delete(account, Constants.AppName);
            }

            await store.SaveAsync(account = e.Account, Constants.AppName);
            await DisplayAlert("Email address", user.Email, "OK");
        }
    }
void OnLoginClicked(对象发送方,事件参数e)
{
字符串clientId=null;
字符串URI=null;
交换机(设备运行时平台)
{
case Device.iOS:
clientId=Constants.iOSClientId;
redirectUri=Constants.iOSRedirectUrl;
打破
case设备。Android:
clientId=Constants.AndroidClientId;
redirectUri=Constants.AndroidRedirectUrl;
打破
}
var authenticator=新的OAuth2Authenticator(
clientId,
无效的
常数。范围,
新Uri(常量.AuthorizeUrl),
新Uri(重定向Uri),
新Uri(Constants.AccessTokenUrl),
无效的
正确的);
authenticator.Completed+=OnAuthenticator已完成;
authenticator.Error+=OnAuthorError;
AuthenticationState.Authenticator=验证器;
var presenter=new Xamarin.Auth.Presenters.OAuthLoginPresenter();
演示者登录(验证器);
}
AuthCompleted上的异步无效(对象发送方、AuthenticatorCompletedEventArgs e)
{
var authenticator=发送方作为OAuth2Authenticator;
if(验证器!=null)
{
authenticator.Completed-=OnAuthenticator已完成;
authenticator.Error-=OnAuthorError;
}
User=null;
如果(如未经认证)
{
//如果用户已通过身份验证,请从Google请求其基本用户数据
//用户信息URL=https://www.googleapis.com/oauth2/v2/userinfo
var request=newoauth2request(“GET”,新Uri(Constants.UserInfoUrl),null,e.Account);
var response=wait request.GetResponseAsync();
if(响应!=null)
{
//反序列化数据并将其存储在帐户存储中
//用户的电子邮件地址将用于标识SimpleDB中的数据
字符串userJson=await response.GetResponseTextAsync();
user=JsonConvert.DeserializeObject(userJson);
}
如果(帐户!=null)
{
store.Delete(account,Constants.AppName);
}
wait store.SaveAsync(account=e.account,Constants.AppName);
等待DisplayAlert(“电子邮件地址”,user.Email,“OK”);
}
}

我希望它能对您有所帮助。

Mounika使用本机用户界面(ctor中的最后一个参数设置为true),因此这将不起作用。好的,谢谢您的回复,我将尝试@moljac