preface

In iOS development, unless you’re working on a standalone app, you’re going to have to make interface calls, also known as API calls.

In this respect, each company defines the interface in a different format, which makes our code not universal.

Even if you do private work, you will encounter a wide variety of interface formats. So is there a universal way to deal with these non-universal formats?

I try to find the constants in the changing interface formats and make those constants configurable so that interface calls can be made in a way that doesn’t change.

These constants include the following:

  • Error code
  • The error message
  • Business data

Error codes can be classified as follows:

  • successful
  • failure
  • Token failure

Based on the above analysis, an open source library is formed, which is expected to facilitate everyone’s development to a certain extent. Open source: github.com/wochen85/FK…

The following is a detailed description of the use:

FKApiInvoker

IOS network interface invocation encapsulation

The installation

pod 'FKApiInvoker'
Copy the code

use

1. Initialize the configuration, which can be placed in the Appdelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptionsIn the method, or wherever you see fit,

Assume that the json format returned in the background is as follows:

{"code": 0."msg":"Success"."data": {"name":"CHAT"."age"18}} :Copy the code

In the command, code indicates the error code (0 indicates that the processing is successful, 127 indicates that the token has expired), MSG indicates a prompt message, and data indicates specific service data

Do the following configuration:

FKApiInvokerConfig* config = [[FKApiInvokerConfig alloc] initWithBaseUrls:@[@"http://www.httpbin.org"The @"http://www.httpbin.org"The @"http://www.httpbin.org"The @"http://www.httpbin.org"] commonHeaders:@{@"test": @"test"} respCodeKey:@"code" respMsgKey:@"msg" respDataKey:@"data"successCode:0 tokenExpiredCode:127]; [[FKApiInvoker sharedInvoker] configInvoker:config]; [FKApiInvoker sharedInvoker]. TokenExpiredBlk = ^{//token invalid, redirect login page};Copy the code

2. Invoke the background API

[FKApiInvoker fire:@"post" path:@"/personinfo/login"ResponseModelClass :[LoginResp * model] success:^(LoginResp* model) { Failure :^(NSError *error) {// failure handling}];Copy the code

3. Use mock data

[FKApiInvoker fireWithMockData:mockData method:@"post" path:@"/personinfo/login"ResponseModelClass :[LoginResp * model] success:^(LoginResp* model) { Failure :^(NSError *error) {// failure handling}];Copy the code

Other situations

1. Level of service data returned by the background with error codes and error messages:

{"code": 0."msg":"Success"."name":"CHAT"."age"18} :Copy the code

Change the initial configuration to this:

FKApiInvokerConfig* config = [[FKApiInvokerConfig alloc] initWithBaseUrls:@[@"http://www.httpbin.org"The @"http://www.httpbin.org"The @"http://www.httpbin.org"The @"http://www.httpbin.org"] commonHeaders:@{@"test": @"test"} respCodeKey:@"code" respMsgKey:@"msg"respDataKey:nil successCode:0 tokenExpiredCode:127]; [[FKApiInvoker sharedInvoker] configInvoker:config]; [FKApiInvoker sharedInvoker]. TokenExpiredBlk = ^{//token invalid, redirect login page};Copy the code

That is, the respDataKey argument is passed nil

2. If the common header may change after the initial configuration, as is often the case after the user login, the common header needs to be added with an exampletokenThe field of

Then call the following method:

[[FKApiInvoker sharedInvoker] configCommonHeaders:@{@"token": @"token string"}];
Copy the code

3. Need to make other HTTP requests in JSON format that are more flexible?

You can just use another library I wrote: JsonModelHttp

In fact, FKApiInvoker also relies on JsonModelHttp at the bottom.