Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the correct way for iOS platform to call background interface?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the correct way for the iOS platform to call the background interface". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the correct way for the iOS platform to call the background interface?"

FKApiInvoker

IOS platform network interface call encapsulation

Installation

Pod 'FKApiInvoker'

Use

1. To initialize the configuration, you can put it in the-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (nullable NSDictionary *) launchOptions method of Appdelegate, or wherever you think it is appropriate, assuming that the json format returned by the backend is as follows:

{"code": 0, "msg": "success", "data": {"name": "CHAT", "age": 18}}

Where code is the error code (0 indicates successful processing, 127represents token expiration), msg is the prompt message, and data is the specific business data.

Then do the following configuration:

FKApiInvokerConfig* config = [[FKApiInvokerConfig alloc] initWithBaseUrls:@ [@ "http://www.httpbin.org",@"http://www.httpbin.org",@"http://www.httpbin.org",@"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 is invalid, jump to login page}

two。 Call the backend API

[FKApiInvoker fire:@ "post" path:@ "/ personinfo/login" param:nil headers:headers body:body responseModelClass: [LoginResp class] success: ^ (LoginResp* model) {/ / successful processing} failure: ^ (NSError * error) {/ / failure handling}]

3. Working with mock data

[FKApiInvoker fireWithMockData:mockData method:@ "post" path:@ "/ personinfo/login" param:nil headers:headers body:body responseModelClass: [LoginResp class] success: ^ (LoginResp* model) {/ / successful processing} failure: ^ (NSError * error) {/ / failure handling}]

Other circumstances

1. Business data, error codes and error messages returned by the backend:

{"code": 0, "msg": "success", "name": "CHAT", "age": 18}

Change it to this when initializing the configuration:

FKApiInvokerConfig* config = [[FKApiInvokerConfig alloc] initWithBaseUrls:@ [@ "http://www.httpbin.org",@"http://www.httpbin.org",@"http://www.httpbin.org",@"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 is invalid, jump to login page}

That is, the parameter respDataKey passes nil.

two。 If the generic header may change after initializing the configuration, which is common after the user logs in, a field like token needs to be added to the generic header, so call the following method:

[[FKApiInvoker sharedInvoker] configCommonHeaders:@ {@ "token": @ "token string"}]

3. Need to make other more flexible http requests in json format?

You can directly use another library I wrote: JsonModelHttp

In fact, the bottom layer of FKApiInvoker is also dependent on JsonModelHttp.

At this point, I believe you have a deeper understanding of "what is the correct way for the iOS platform to call the background interface". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report