测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可 | MIT |
发布最后发布 | 2014年12月 |
由 Hernan Zalazar、Sebastian Iacomuzzi、Eugenio Pace 维护。
将库安装到项目中
如果您正在使用 CocoaPods,请将以下行添加到您的 Podfile
pod 'Auth0Client'
或者,按照以下步骤直接 git clone 并在您的项目中引用库
git clone [email protected]:auth0/Auth0.iOS.git
实例化 Auth0Client
#import "Auth0Client.h"
// ...
Auth0Client *client = [Auth0Client auth0Client:@"YOUR_AUTH0_DOMAIN" clientId:@"YOUR_CLIENT_ID"];
触发登录(使用小部件)
[client loginAsync:self withCompletionHandler:^(NSMutableDictionary* error) {
if (error) {
NSLog(@"Error authenticating: %@", [error objectForKey:@"error"]);
}
else {
// * Use client.auth0User to do wonderful things, e.g.:
// - get user email => [client.auth0User.Profile objectForKey:@"email"]
// - get facebook/google/twitter/etc access token => [[[client.auth0User.Profile objectForKey:@"identities"] objectAtIndex:0] objectForKey:@"access_token"]
// - get Windows Azure AD groups => [client.auth0User.Profile objectForKey:@"groups"]
// - etc.
}
}];
或者您可以使用连接作为参数(例如,这里我们使用 Windows Azure AD 账户进行登录)
[client loginAsync:self connection:@"auth0waadtests.onmicrosoft.com" withCompletionHandler:^(NSMutableDictionary* error) { ... }];
只有某些供应商支持此选项(数据库连接和活动目录/LDAP)。
[client loginAsync:self connection:@"my-db-connection"
username:@"username"
password:@"password"
withCompletionHandler:^(NSMutableDictionary* error) {
if (error) {
NSLog(@"Error authenticating: %@ - %@", [error objectForKey:@"error"], [error objectForKey:@"error_description"]);
}
else {
// * Use client.auth0User to do wonderful things, e.g.:
// - get user email => [client.auth0User.Profile objectForKey:@"email"]
// - get facebook/google/twitter/etc access token => [[[client.auth0User.Profile objectForKey:@"identities"] objectAtIndex:0] objectForKey:@"access_token"]
// - get Windows Azure AD groups => [client.auth0User.Profile objectForKey:@"groups"]
// - etc.
}
}];
可选地,您可以指定
作用域
参数。目前作用域有两个可能的值
- scope:@"openid"(默认值)- 它将返回 access_token,还有 id_token,这是一个 Json Web Token (JWT)。JWT 只包含用户 id。
- scope:@"openid profile":如果您想使整个用户配置文件成为 id_token 的一部分。
您可以通过指定目标客户端的 ID(targetClientId
)以及可选的 NSMutableDictionary 对象(options
),来获取一个委托令牌,以便包括自定义参数,如作用域或 id_token
NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"USER_ID_TOKEN", @"id_token", // default: id_token of the authenticated user (client.auth0User.IdToken)
@"openid profile", @"scope", // default: openid
nil];
[client getDelegationToken:targetClientId options:options withCompletionHandler:^(NSMutableDictionary* delegationResult)
{
// [delegationResult objectForKey:@"id_token"]
}];
options
参数不得包含client_id
和target
键。target
从targetClientId
获取,client_id
使用创建Auth0Client
实例时使用的 id。
安装并 配置您的应用程序 以与 iOS 的 Facebook SDK 一起工作。
在您的iOS应用中实现Facebook登录。有两种方式
用户通过Facebook App Native认证后,调用loginAsync
方法,指定Facebook的access_token
NSString *fb_access_token = [[FBSession.activeSession accessTokenData] accessToken];
[client loginAsync:self connection:@"facebook"
accessToken:fb_access_token
withCompletionHandler:^(NSMutableDictionary* error) {
if (error) {
NSLog(@"Error authenticating: %@ - %@", [error objectForKey:@"error"], [error objectForKey:@"error_description"]);
}
else {
// Use client.auth0User to do wonderful things
}
}];
想了解更多详情,您可以查看我们的示例。
Auth0可以帮助您