PNObject
[](https://travis-ci.org/Giuseppe Nucifora/PNObject)
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
要求
安装
PNObject 通过 CocoaPods 提供。要安装,只需将以下行添加到您的 Podfile 中
pod "PNObject"
1.3.0 版本新特性
- 配置时可以单独设置基础 URL 和 API 路径
- 访问单独的基础 URL、端点路径或完整的端点 URL
使用单独的基础 URL 和 API 路径配置 PNObject 端点客户端 ID、客户端密钥和 OAuthModePassword
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @{BaseUrl:@"http://pnobject.local/",EndpointPath:@"api/v1/"},
EnvironmentStage : @{BaseUrl:@"http://pnobject.stage.it/",EndpointPath:@"api/v1/"},
EnvironmentProduction : @{BaseUrl:@"http://pnobject.prod.it/",EndpointPath:@"api/v1/"},
} userSubclass:[PNUser class]];
/** Can user special char %@ to autoset EndpointPath to Oauth endpointPath */
[[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"%@oauth-token" oauthMode:OAuthModeClientCredential forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"******" clientSecret:@"******" oAuthEndpointAction:@"%@oauth-token" oauthMode:OAuthModePassword forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setOauthUserName:@"admin" oauthPassword:@"admin" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setEnvironment:EnvironmentStage];
[[PNObjectConfig sharedInstance] loadManagersWithCredentials];
}
获取 BaseUrl、EndPointPath 和EndPointUrl
NSLogDebug(@"%@",[[PNObjectConfig sharedInstance] baseUrl]);
NSLogDebug(@"%@",[[PNObjectConfig sharedInstance] endPointPath]);
NSLogDebug(@"%@",[[PNObjectConfig sharedInstance] endPointUrl]);
配置 PNObject 端点客户端 ID、客户端密钥和 OAuthModePassword
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @"http://pnobject.local/api/v1/",
EnvironmentStage : @"http://pnobject.stage.it/api/v1/",
EnvironmentProduction : @"http://pnobject.prod.it/api/v1/"
} userSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentProduction];
[[PNObjectConfig sharedInstance] setOauthUserName:@"admin" oauthPassword:@"admin" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setEnvironment:EnvironmentStage];
[[PNObjectConfig sharedInstance] loadManagersWithCredentials];
}
配置 PNObject 端点客户端 ID、客户端密钥和 OAuthModeClientCredential
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @"http://pnobject.local/api/v1/",
EnvironmentStage : @"http://pnobject.stage.it/api/v1/",
EnvironmentProduction : @"http://pnobject.prod.it/api/v1/"
} userSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentProduction];
[[PNObjectConfig sharedInstance] setEnvironment:EnvironmentStage];
[[PNObjectConfig sharedInstance] loadManagersWithCredentials];
}
配置 PNObject 端点和使用自定义 PNUser 对象
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[PNObjectConfig initSharedInstanceForEnvironments:@{EnvironmentDevelopment : @"http://pnobject.local/api/v1/",
EnvironmentStage : @"http://pnobject.stage.it/api/v1/",
EnvironmentProduction : @"http://pnobject.prod.it/api/v1/"
} userSubclass:[PNUser class]];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentStage];
[[PNObjectConfig sharedInstance] setClientID:@"xxxxxxxxx" clientSecret:@"xxxxxxxxxxxx" forEnv:EnvironmentProduction];
[[PNObjectConfig sharedInstance] setEnvironment:EnvironmentStage];
[[PNObjectConfig sharedInstance] loadManagersWithCredentials];
}
User.h
#import <PNObject/PNUser.h>
@interface User : PNUser
@end
User.m
#import "User.h"
#import "PNObject+Protected.h"
#import "PNObject+PNObjectConnection.h"
#import <NSDate_Utils/NSDate+NSDate_Util.h>
@interface User () <PNObjectSubclassing>
@end
@implementation User
+ (NSDictionary *)objcetMapping {
NSMutableDictionary *userMapping = [[NSMutableDictionary alloc] initWithDictionary:[[PNUser class] objcetMapping]];
return userMapping;
}
+ (BOOL) singleInstance {
return [[self class] singleInstance];
}
+ (NSString * _Nonnull) objectEndPoint {
return @"User";
}
+ (NSString * _Nonnull ) objectClassName {
return @"User";
}
###
Custom Reset Password
+ (void) resetPasswordForEmail:(NSString * _Nonnull) userEmail
Progress:(nullable void (^)(NSProgress * _Nonnull uploadProgress)) uploadProgress
Success:(nullable void (^)(NSDictionary * _Nullable responseObject))success
failure:(nullable void (^)(NSError * _Nonnull error))failure {
[self POSTWithEndpointAction:@"password-reset-request"
parameters:[self resetPasswordFormObject:userEmail]
progress:uploadProgress
success:^(NSURLSessionDataTask * _Nullable task, NSDictionary * _Nullable responseObject) {
if (success) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure) {
failure(error);
}
}];
}
+ (NSDictionary * _Nonnull) resetPasswordFormObject:(NSString *) email {
NSMutableDictionary *resetPasswordDictionary = [[NSMutableDictionary alloc] init];
[resetPasswordDictionary setObject:email forKey:@"email"];
return resetPasswordDictionary;
}
作者
Giuseppe Nucifora, [邮箱地址保护]
许可证
PNObject 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。