JsenKit 1.3.38

JsenKit 1.3.38

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2019 年 7 月

WangXuesen 维护。



JsenKit 1.3.38

  • 作者:
  • WangXuesen

描述

JsenKit 是 iOS 项目的通用框架

它包含以下功能:

  • 基于 YYModelAFNetworking 的网络请求
  • 自定义 Alert(将更新)
  • 自定义 HUD
  • 自定义 TabBar/Button(将创建)/TextView ... UIKit 模块
  • 常用 Category
  • 调试工具,例如:FPSLabel

安装

JsenKit 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile:

pod 'JsenKit'

使用

JsenNetwork

响应格式

默认响应为 json 格式 : { msg:"msg", data:{}/[] timestemp:1000 code:0 }

带有 "data" 键的值将被创建为数据模型/模型数组

如果您服务器响应格式不像这样,您可以将JsenNetworkingConfig类属性中的responseFormat设置为转换它。

示例

	config.responseFormat =  @{
		JsenNetworkingResponseDataKey       :@"info",
		JsenNetworkingResponseStatusCodeKey :@"codeNumber",
		JsenNetworkingResponseMessageKey    :@"msg",
		JsenNetworkingResponseTimelineKey   :@"time",
	};

如果您的响应需要不同的响应格式,配置customSuccessDataAllKeys将是一个不错的选择,示例代码如下:

    config.customSuccessDataAllKeys = [self configSuccessDataAllKeys];

JsenNetworkingConfig必须初始化。例如:

配置主机

    JsenNetworkingConfig *config = [JsenNetworkingConfig shareConfig];
    config.host = @"http://test.jsen.com/demo";

响应模型 URL和响应数据模型,如果没有数据模型或不想创建模型,可以省略。Jsen_HomeList_API(已定义为"/home/list") API将响应数据JsenHomeCellModel json。

	config.modelClass = @{
		Jsen_HomeList_API:NSClassFromString(@"JsenHomeCellModel")
	};

全局参数

    config.globalParametersBlock = ^NSDictionary *{
        return @{
        		//some keys and values
        };
    };

响应错误代码通知例如:token超时,添加通知

在“JsenNetworkingConfig.h”文件中,JsenNetworkingCustomHttpErrorNotificationKey

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commonNetworkErrorNotification:) name:JsenNetworkingCustomHttpErrorNotificationKey object:nil];

     // resive notification 
     - (void)commonNetworkErrorNotification:(NSNotification *)notifi {
        JsenNetworkingFailedResponse *response = [notifi object];
        NSLog(@"%@",notifi);
     }

**自定义错误代码通知 **如果响应代码是@10030,通知中心将接收到JsenNetworkingCustomHttpErrorNotificationKey的消息


    config.customErrorStatusCode = @{
		@(@10030.integerValue):@"token timeout.please relogin"
	};

请求超时如果您想要更自定义的设置,您应该查看timeoutInterval属性

    config.defaultTimeoutInterval = 30;

网络可达性

当调用startMonitoring时,[JsenNetworkingReachabilityManager manager].currentStatus可以获取网络当前状态

    [[JsenNetworkingReachabilityManager manager] setJsenReachabilityStatusChangeBlock:^(JsenNetworkingReachabilityStatus status) {
        //to do something
    }];
    [[JsenNetworkingReachabilityManager manager] startMonitoring];

请求序列化器设置

[AFHTTPRequestSerializer serializer]是默认设置,您可以通过特定的请求API来自定义,示例

    config.requestSerializerTypeConfig = @{
		Jsen_HomeList_API : @(JsenNetworkingConfigSerializerJSON)
	};

HTTP头部设置

	config.httpHeader = @{
		@"Content-Type":@"application/json"
	};

发送请求

    NSDictionary *parameters = @{
                                 @"id":@123,
                                 @"page":@1
                                 };
    [[JsenNetworkingManager manager] post:Jsen_HomeList_API parameters:parameters progress:nil success:^(JsenNetworkingSuccessResponse * _Nonnull response) {
    		   //success
				JsenHomeCellModel *model = response.data;
				
				//if response json is array 
				//NSArray *cellModels = response.data;
				
    } failed:^(JsenNetworkingFailedResponse * _Nonnull response) {
				//error
    } finished:^{
           //finished
    }];