Devise 1.1.0

Devise 1.1.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2015 年 7 月

Patryk KaczmarekRadosław Szeja 维护。



 
依赖项
UICKeyChainStore~> 1.1
ngrvalidator~> 1.1.0
XLForm~> 2.1
 

Devise 1.1.0

  • 作者
  • Patryk Kaczmarek、Adrian Kashivskyy、Wojciech Trzasko、Radosław Szeja 和 Paweł Białecki

devise-ios

Circle CI

devise-ios 是一个自动连接 Devise 的简单客户端。专门创建用于与 devise-ios 后端 gem 一起工作,使您的工作更加容易和快速!

功能

devise-ios 实现

  • 用户注册
  • 登录/登出
  • 密码提醒
  • 表单验证
  • 更新资料
  • 删除账户
  • 轻量级 Facebook 和 Google+ 登录

要求

  • Xcode 6.0 和 iOS 7.0+ SDK
  • CocoaPods 0.37.2(使用 gem install cocoapods 来获取!)

配置

当您想要使用 devise-ios 时,请使用 #import <Devise/Devise.h>

[DVSConfiguration sharedConfiguration] 是一个单例,用于将配置保存在一个地方。在您的 AppDelegate 中的 application:didFinishLaunchingWithOptions: 方法中的某个地方,最初开始时使用

[[DVSConfiguration sharedConfiguration] setServerURL:<#NSURL#>];

devise-ios 还能通知您遇到的问题。日志在调试过程中特别有用。设计了 3 种日志级别

  • DVSLoggingModeNone - 不记录任何内容,忽略所有消息。
  • DVSLoggingModeWarning - 使用 NSLog 打印所有消息。
  • DVSLoggingModeAssert - 使用消息终止代码。

要指定日志模式,请使用

[[DVSConfiguration sharedConfiguration] setLoggingMode:<#DVSLoggingMode#>];

devise-ios 负责处理网络问题,并且在连接问题时能够自动重试请求。您可以使用 DVSConfigurationnumberOfRetriesretryTresholdDuration 属性来指定重试次数和重试间隔。

Devise 在内部使用 AFNetworking。如果需要启用网络活动指示器,请在 AppDelegate application:didFinishLaunchingWithOptions: 中使用 AFNetworkActivityIndicatorManager

用户管理器

devise-ios 的主要类是 DVSUserManager。提供的实现足以实现登录、注册、编辑以及 devise-ios 提供的其他所有功能。

功能相当直观且具有自我解释性。

  • 用户注册

    - (void)registerWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;
  • 资料更新

    - (void)updateWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;
  • 登录

    - (void)loginWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;
  • 使用 Facebook 账户登录

    - (void)signInUsingFacebookWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;
  • 使用Google Plus账号登录

    - (void)signInUsingGoogleWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;

    为了处理来自Google Plus授权的回调,请在AppDelegate方法中实现以下之一

    - (BOOL)application: (UIApplication *)application openURL: (NSURL *)url sourceApplication: (NSString *)sourceApplication annotation: (id)annotation {
        return [[DVSUserManager defaultManager] handleURL:url sourceApplication:sourceApplication annotation:annotation];
        //or instance of other DVSUserManager used to sign in via g+.
    }

    这样确保至少有一个传递的回调会在授权结果中被调用。

  • 密码提示

    - (void)remindPasswordWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;
  • 密码更新

    - (void)changePasswordWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;
  • 账户删除

    - (void)deleteAccountWithSuccess:(DVSVoidBlock)success failure:(DVSErrorBlock)failure;

用户自定义

尽管DVSUser的实现足够用于基本使用,但您也可以进行自定义。

如果需要持久化本地关于DVSUser子类的更多信息(除了emailsessionTokenidentifier之外 - 这些默认存储),则遵守DVSUserPersisting协议。您可以通过调用来选择哪些属性应该持久化

- (NSArray *)propertiesToPersistByName;

只需记住传递属性名称时作为NSString

用户模型验证和信息提示

devise-ios底层使用NGRValidator进行数据验证。在此基础上,devise-ios提供了一个功能,可以为您的自定义验证规则或修改默认验证规则。如果您想从中受益,遵守DVSUserManagerDelegate协议并实现- (void)userManager:(DVSUserManager *)manager didPrepareValidationRules:(NSMutableArray *)validationRules forAction:(DVSActionType)action;方法。

假设DVSUser的一个子类有一个额外的属性NSString *registrationUsername,您想在注册过程中对其进行验证以满足条件

  • 不能为空
  • 长度至少为4个字符
  • 长度最多为20个字符

如果验证失败,则显示适当的消息

  • 如果少于4个字符:“应至少有4个字符”
  • 如果多于20个字符:“应最多有20个字符”

此外,registrationUsername对用户来说听起来不太好,因此应将其显示为“用户名”

- (void)userManager:(DVSUserManager *)manager didPrepareValidationRules:(NSMutableArray *)validationRules forAction:(DVSActionType)action {

    NGRPropertyValidator *validator = NGRValidate(@"registrationUsername").required().lengthRange(5, 20).msgTooShort(@"should have at least 4 signs.").msgTooLong(@"should have at most 20 signs").localizedName(@"Username");
    [validationRules addObject:validator];
}

当用户为registrationUsername属性提供字符串foo时,devise-ios将返回一个带有本地化描述的NSError

NSLog(@"%@", error.localizedDescription);
// Username should have at least 4 characters.

就是这样!有关更多信息,请参阅NGRValidator

扩展网络

好的。但是,您创建DVSUser子类的目的不是为了本地使用,对吧?要附加自己的参数到请求,请像下面一样遵守DVSUserJSONSerializerDataSource

- (void)setupManager {

    TestUser *user = [[TestUser alloc] init]; //TestUser is subclass of DVSUser with "foo" property

    self.manager = [[DVSUserManager alloc] initWithUser:user]; //manager is an ivar here
    self.manager.serializer.dataSource = self;
}

#pragma mark - DVSUserJSONSerializerDataSource

- (NSDictionary *)additionalRequestParametersForAction:(DVSActionType)action {
    //use action to distinguish type of request
    TestUser *user = (TestUser *)self.manager.user;
    //make sure that foo is not nil. Eg. add own validation rule in userManager:didPrepareValidationRules:forAction: method
    return @{@"foo" : user.foo};
}

devise-ios会处理其余部分。

UI组件

Sign up view example

在您的应用中某个时刻,您可能希望为用户提供快速设置,并允许他们登录和注册。devise-ios提供了一个方便的视图控制器,称为DVSAccountRetrieverViewController,它简化了此过程。以下是一个简单的使用示例

DVSAccountRetrieverViewController *logInController = [[DVSAccountRetrieverViewController alloc] initWithType:DVSRetrieverTypeLogIn fields:DVSAccountRetrieverFieldEmailAndPassword | DVSAccountRetrieverFieldProceedButton];
logInController.delegate = self;
[self.navigationController pushViewController:logInController animated:YES];

简单吧?如你所见,初始化器接收两个参数:typefields。第一个参数定义了视图控制器将如何显示和操作。如果您想执行登录操作,应传递DVSRetrieverTypeLogIn。使用DVSAccountRetrieverViewController将会自动配置继续按钮标题和点击事件来执行登录请求。对于注册操作,可以使用DVSRetrieverTypeSignUp类型。

fields是选项参数,定义了视图的哪些部分应该可见。例如,如果您想使用仅包含文本字段和继续按钮的简单表单,应定义如下fields

DVSAccountRetrieverFields logInFields = DVSAccountRetrieverFieldEmailAndPassword | DVSAccountRetrieverFieldProceedButton;

结果将是

Log in view example

如果您想向表单中添加密码提示,请使用以下组合

DVSAccountRetrieverFields logInFields = DVSAccountRetrieverFieldEmailAndPassword | DVSAccountRetrieverFieldProceedButton | DVSAccountRetrieverFieldPasswordReminder;

结果

Log in view with reminder example

为了处理执行操作的结果,您的类应重写两个DVSAccountRetrieverViewControllerDelegate协议方法

// for success
- (void)accountRetrieverViewController:(DVSAccountRetrieverViewController *)controller didSuccessForAction:(DVSRetrieverAction)action user:(DVSUser *)user;

// for failure
- (void)accountRetrieverViewController:(DVSAccountRetrieverViewController *)controller didFailWithError:(NSError *)error forAction:(DVSRetrieverAction)action;

在两种情况下,视图控制器将返回 action 变量,它定义完成操作的类型并可以具有以下值之一:DVSRetrieverActionLogInDVSRetrieverActionSignUpDVSRetrieverActionPasswordRemind。成功回调还将在 user 中返回相应的用户对象。

DVSAccountRetrieverViewController 不实现自动关闭功能。开发者负责决定何时关闭视图。为了帮助完成此任务,devise-iosDVSAccountRetrieverViewControllerDelegate 中提供额外的回调,它将在用户点击关闭按钮时执行。

- (void)accountRetrieverViewControllerDidTapDismiss:(DVSAccountRetrieverViewController *)controller;

演示

实现完整的账户生命周期。还包括一个简单的 DVSUser 子类化和验证示例。要运行演示,请按照以下说明操作

$ git clone --recursive [email protected]:netguru/devise-ios.git
$ pod install

如果还没有用 --recursive 克隆项目

$ git submodule update --init --recursive
$ pod install

许可

devise-iosMIT 许可 下可用。

贡献

首先,感谢您的贡献!

以下是您需要遵循的一些指南

更多信息

有疑问?请 提出问题!您还可以阅读我们的博客文章 宣布 devise-iOS 简化的身份验证

版权 © 2014-2015 Netguru