默认实现的协议
在WXSDKEngine中有两个默认实现的协议,是关于网络和导航栏的。这两部分是基础的功能,提供默认实现有利用整个系统运行起来。
// register some default handlers when the engine initializes.
+ (void)_registerDefaultHandlers
{
[self registerHandler:[WXNetworkDefaultImpl new] withProtocol:@protocol(WXNetworkProtocol)];
[self registerHandler:[WXNavigationDefaultImpl new] withProtocol:@protocol(WXNavigationProtocol)];
}
WXNetworkProtocol内容
@protocol WXNetworkProtocol
/**
* @abstract send request
*
* @param request The URL Request
*
* @param sendDataCallback This block is called periodically to notify the progress.
*
* @param responseCallback This block is called when receiving a response and no further messages will be received until the completion block is called.
*
* @param receiveDataCallback This block is called when data is available.
*
* @param compeletionCallback This block is called when the last message related to a specific task is sent.
*/
- (id)sendRequest:(NSURLRequest *)request withSendingData:(void (^)(int64_t bytesSent, int64_t totalBytes))sendDataCallback
withResponse:(void (^)(NSURLResponse *response))responseCallback
withReceiveData:(void (^)(NSData *data))receiveDataCallback
withCompeletion:(void (^)(NSData *totalData, NSError *error))compeletionCallback;
@end
WXNetworkProtocol使用者
WXSDKInstance
- (void)renderWithURL:(NSURL *)url options:(NSDictionary *)options data:(id)data;
WXStreamModule
- (void)sendHttp:(NSDictionary*)param callback:(WXModuleCallback)callback
- (void)fetch:(NSDictionary *)options callback:(WXModuleCallback)callback progressCallback:(WXModuleKeepAliveCallback)progressCallback
WXNetworkProtocol实现者
- WXNetworkDefaultImpl
@interface WXNetworkDefaultImpl : NSObject
@end
直接使用系统API NSURLSession
,没有用第三方库,比如AFNetworking
- 主要是数据通信业务
NSURLSessionDataTask
NSURLSessionDataTask *task = [_session dataTaskWithRequest:request];
if (!_callbacks) {
_callbacks = [NSMutableDictionary dictionary];
}
[_callbacks setObject:info forKey:task];
[task resume];
疑问:既然主要是数据业务,没有涉及上传和下载,为什么不直接实现
NSURLSessionTaskDelegate
,而仅仅只是其父协议NSURLSessionDelegate
?
WXNavigationProtocol协议内容
/**
* This enum is used to define the position of navbar item.
*/
typedef NS_ENUM(NSInteger, WXNavigationItemPosition) {
WXNavigationItemPositionCenter = 0x00,
WXNavigationItemPositionRight,
WXNavigationItemPositionLeft,
WXNavigationItemPositionMore
};
/**
* @abstract The callback after executing navigator operations. The code has some status such as 'WX_SUCCESS'、'WX_FAILED' etc. The responseData
* contains some useful info you can handle.
*/
typedef void (^WXNavigationResultBlock)(NSString *code, NSDictionary * responseData);
@protocol WXNavigationProtocol
/**
* @abstract Returns the navigation controller.
*
* @param container The target controller.
*/
- (id)navigationControllerOfContainer:(UIViewController *)container;
/**
* @abstract Sets the navigation bar hidden.
*
* @param hidden If YES, the navigation bar is hidden.
*
* @param animated Specify YES to animate the transition or NO if you do not want the transition to be animated.
*
* @param container The navigation controller.
*
*/
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated
withContainer:(UIViewController *)container;
/**
* @abstract Sets the background color of navigation bar.
*
* @param backgroundColor The background color of navigation bar.
*
* @param container The target controller.
*
*/
- (void)setNavigationBackgroundColor:(UIColor *)backgroundColor
withContainer:(UIViewController *)container;
/**
* @abstract Sets the item in navigation bar.
*
* @param param The data which is passed to the implementation of the protocol.
*
* @param position The value indicates the position of item.
*
* @param block A block called once the action is completed.
*
* @param container The target controller.
*
*/
- (void)setNavigationItemWithParam:(NSDictionary *)param
position:(WXNavigationItemPosition)position
completion:(WXNavigationResultBlock)block
withContainer:(UIViewController *)container;
/**
* @abstract Clears the item in navigation bar.
*
* @param param The data which is passed to the implementation of the protocol.
*
* @param position The value indicates the position of item.
*
* @param block A block called once the action is completed.
*
* @param container The target controller.
*
*/
- (void)clearNavigationItemWithParam:(NSDictionary *)param
position:(WXNavigationItemPosition)position
completion:(WXNavigationResultBlock)block
withContainer:(UIViewController *)container;
/**
* @abstract Pushes a view controller onto the receiver’s stack.
*
* @param param The data which is passed to the implementation of the protocol.
*
* @param block A block called once the action is completed.
*
* @param container The target controller.
*
*/
- (void)pushViewControllerWithParam:(NSDictionary *)param
completion:(WXNavigationResultBlock)block
withContainer:(UIViewController *)container;
/**
* @abstract Pops the top view controller from the navigation stack.
*
* @param param The data which is passed to the implementation of the protocol.
*
* @param block A block called once the action is completed.
*
* @param container The target controller.
*
*/
- (void)popViewControllerWithParam:(NSDictionary *)param
completion:(WXNavigationResultBlock)block
withContainer:(UIViewController *)container;
/**
* @abstract Pops all the view controllers on the stack except the root view controller.
*
* @param param The data which is passed to the implementation of the protocol.
*
* @param block A block called once the action is completed.
*
* @param container The target controller.
*
*/
- (void)popToRootViewControllerWithParam:(NSDictionary *)param
completion:(WXNavigationResultBlock)block
withContainer:(UIViewController *)container;
@end
- 协议中使用到的枚举类型,自定义类型跟协议内容放在同一个文件是一种值得推荐的做法
- 每个协议函数都有一个
UIViewController
参数,而不是UINavigationController
参数。这点值得借鉴
WXNavigationProtocol使用者
WXAComponent
- (void)openURL
WXComponent +Navigation
@interface WXComponent (Navigation)
- (void)setNavigationBarHidden:(BOOL)hidden;
- (void)setNavigationBackgroundColor:(UIColor *)backgroundColor;
- (void)setNavigationItemWithParam:(NSDictionary *)param position:(WXNavigationItemPosition)position;
- (void)setNavigationWithStyles:(NSDictionary *)styles;
@end
WXNavigationProtocol实现者
WXNavigationDefaultImpl
- 采用
UINavigationController
对象的系统API完成相应的功能,这个对象可以由UIViewController
参数的navigationController
属性得到 - 参数都是通过一个字典传递,这个基本上大众的选择了
- icon下载,使用了一个静态局部变量。如果用户没有实现
WXImgLoaderProtocol
协议,下载不了
- (id)imageLoader
{
static id imageLoader;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
imageLoader = [WXHandlerFactory handlerForProtocol:@protocol(WXImgLoaderProtocol)];
});
return imageLoader;
}
这里的imageLoader
可能为nil
,如果用户没有实现WXImgLoaderProtocol
协议