目前内置的扩展实现有:
NJCSVCManager
NJCSViewAnimate
NJCSNotificationManager
NJCSUserDefaultManager
NJCSModelAdapter
其余的扩展都是接口层的实现,需要用户根据自己的情况具体实现,如:
NJCSHttp
NJCSAlert
NJCSLogComponent
等……
1:下面以NJCSHttp为例进行扩展接入介绍:
接口协议:
#import#import typedef void(^callbackBlock)(BOOL isSuccess, id resultData); @protocol NJCSHttpInterfaceProtocol // // 外部需要实现的http get请求 // // @param urlString 接口url 字符串 // @param params 请求参数字典 // @param resultBlock 请求结束后的block // - (void)Get:(NSString *)urlString params:(NSDictionary *)params resultBlock:(void(^)(BOOL isSuccess, id resultData))resultBlock; // // 外部需要实现的http post请求 // // @param urlString 接口url 字符串 // @param params 请求参数字典 // @param resultBlock 请求结束后的block // - (void)Post:(NSString *)urlString params:(NSDictionary *)params resultBlock:(void(^)(BOOL isSuccess, id resultData))resultBlock; @end @protocol NJCSHttpProtocol - (void)GetRequestWithURLString:(NSString *)urlString params:(NSDictionary *)params callbackConfig:(NSDictionary *)callbackConfig; - (void)PostRequestWithURLString:(NSString *)urlString params:(NSDictionary *)params callbackConfig:(NSDictionary *)callbackConfig; @end
说明:
协议NJCSHttpProtocol内部定义的方法是为了暴露给JS,NJCSHttp(接口层)必须实现该协议,而协议NJCSHttpInterfaceProtocol,是实现层必须要实现的(用户层),接口层源码如下:
#import "NJCSHttp.h" #import "NJCSHttpInterfaceProtocol.h" #import "NJCSBridge.h" @interface NJCSHttp ()@end @implementation NJCSHttp - (void)GetRequestWithURLString:(NSString *)urlString params:(NSDictionary *)params callbackConfig:(NSDictionary *)callbackConfig { NSParameterAssert(urlString); NSParameterAssert(params); NSParameterAssert(callbackConfig); if ([_interface respondsToSelector:@selector(Get:params:resultBlock:)]) { NSMutableDictionary *tempParams = [params mutableCopy]; if ([params objectForKey:@"runNative"]) { [tempParams removeObjectForKey:@"runNative"]; } [_interface Get:urlString params:tempParams resultBlock:^(BOOL isSuccess, id resultData) { NSString *jsInstance = [callbackConfig objectForKey:@"instanceName"]; NSString *methodName = [callbackConfig objectForKey:@"methodName"]; NSParameterAssert(jsInstance); NSParameterAssert(methodName); NSMutableArray *params = [[NSMutableArray alloc] initWithCapacity:1]; [params addObject:resultData]; if (isSuccess) { [[NJCSBridge getInstance] runJSFunctionWithInstanceName:jsInstance methodName:methodName parameters:params]; }else{ //接口请求失败 } }]; } } - (void)PostRequestWithURLString:(NSString *)urlString params:(NSDictionary *)params callbackConfig:(NSDictionary *)callbackConfig { NSParameterAssert(urlString); NSParameterAssert(params); NSParameterAssert(callbackConfig); if ([_interface respondsToSelector:@selector(Post:params:resultBlock:)]) { [_interface Post:urlString params:params resultBlock:^(BOOL isSuccess, id resultData) { NSString *jsInstance = [callbackConfig objectForKey:@"instanceName"]; NSString *methodName = [callbackConfig objectForKey:@"methodName"]; NSParameterAssert(jsInstance); NSParameterAssert(methodName); NSMutableArray *params = [[NSMutableArray alloc] initWithCapacity:1]; [params addObject:resultData]; if (isSuccess) { [[NJCSBridge getInstance] runJSFunctionWithInstanceName:jsInstance methodName:methodName parameters:params]; }else{ //接口请求失败 } }]; } } @end
在接口层的GetRequestWithURLString方法和PostRequestWithURLString方法中会各自调用实现层的Get和Post方法;
2:实现层代码如下:
#import "CommonHTTPTool.h" #import "NJCSHttp.h" #import "IMHTTPClient.h" @implementation CommonHTTPTool - (void)Get:(NSString *)urlString params:(NSDictionary *)params resultBlock:(void (^)(BOOL, id))resultBlock { IMHTTPClient *client = [[IMHTTPClient alloc] initWithURL:urlString parameters:params]; [client imHTTPRequestOperationWithHTTPMethod:@"GET" success:^(IMHTTPRequestOperation *operation, id responseObject, id userInfo) { resultBlock(YES, responseObject); } failure:^(IMHTTPRequestOperation *operation, NSError *error, id userInfo) { resultBlock(NO, error); }]; } - (void)Post:(NSString *)urlString params:(NSDictionary *)params resultBlock:(void (^)(BOOL, id))resultBlock { IMHTTPClient *client = [[IMHTTPClient alloc] initWithURL:urlString parameters:params]; [client imHTTPRequestOperationWithHTTPMethod:@"POST" success:^(IMHTTPRequestOperation *operation, id responseObject, id userInfo) { resultBlock(YES, responseObject); } failure:^(IMHTTPRequestOperation *operation, NSError *error, id userInfo) { resultBlock(NO, error); }]; } @end
说明:IMHTTPClient是实现层(用户层)自己封装的网络库。