最近,开发了一个SDK,一行代码实现所需效果
.h文件主要源码如下
@class QYSigningVCParam;
@interface QYSigningController : UIViewController
typedef void (^SignSuccessBlock)(); // 签署成功回调
typedef void (^SignFailBlock)(NSString* str); // 签署失败回调,返回失败状态码字符串,对应的状态码服务器文档会给出
typedef void (^NetworkErrorBlock)(NSError *error); // http网络请求错误回调,AFNetworking返回的错误
- (instancetype)initWithSigningParam:(QYSigningVCParam *)param
success:(SignSuccessBlock)success
failure:(SignFailBlock)failure
netError:(NetworkErrorBlock)netError;
@end
.m 主要源码如下:
@interface QYSigningController ()
/** 签署成功回调 */
@property (nonatomic,copy)SignSuccessBlock signSuccess;
/** 签署失败回调 */
@property (nonatomic,copy)SignFailBlock signFailure;
/** 网络错误回调 */
@property (nonatomic,copy)NetworkErrorBlock netError;
@end
- (instancetype)initWithSigningParam:(QYSigningVCParam *)param success:(SignSuccessBlock)success failure:(SignFailBlock)failure netError:(NetworkErrorBlock)netError {
if(self = [super init]) {
self.signingParam = param;
self.signSuccess = success;
self.signFailure = failure;
self.netError = netError;
}
return self;;
}
在调用时注意作 if 判断 block 是否为 nil 防止外部传入空值导致程序崩溃
if (self.signFailure) {
self.signFailure(@"signFailure");
}
if (self.netError) {
self.netError(error);
}