Sagit. Framework For IOS development Framework tutorial 6: Network request STHttp,

Source: Internet
Author: User

Sagit. Framework For IOS development Framework tutorial 6: Network request STHttp,
Preface:

IOS article. Today, I will add a Sagit tutorial;

Although I feel that there is no audience for IOS articles, I should try to write it ,-_-〜

Sagit Open Source Address: https://github.com/cyq1162/Sagit

Today, we primarily share network requests, that is, the usage of STHttp:

STHttp is the source code for Processing Network API requests.

1. Description of calling external API functions:

For network requests, Sagit is currently a secondary encapsulation of AFNetworking!

Therefore, this third-party component is referenced in the framework.

After encapsulation, the following APIs are called:

Typedef void (^ Success) (STModel * result); typedef void (^ Error) (NSString * errMsg );//! Provide basic network requests (get, post, upload (Image upload) @ interface STHttp: NSObject @ property (nonatomic, strong) STMsgBox * msgBox;-(instancetype) init :( STMsgBox *) msgBox;-(void) get :( NSString *) url paras :( NSDictionary *) paras success :( Success) succese;-(void) get :( NSString *) url paras :( NSDictionary *) paras success :( Success) success error :( Error) error;-(void) post :( NSString *) url paras :( NSDictionary *) paras success :( Success) success; -(void) post :( NSString *) url paras :( NSDictionary *) paras success :( Success) success error :( Error) error;-(void) upload :( NSString *) url data :( NSData *) data success :( Success) success;-(void) upload :( NSString *) url data :( NSData *) data success :( Success) success error :( Error) error; -(void) upload :( NSString *) url paras :( NSDictionary *) paras success :( Success) success;-(void) upload :( NSString *) url paras :( NSDictionary *) paras success :( Success) success error :( Error) error;-(void) setHeader :( NSString *) key v :( NSString *) value; + (instancetype) share; + (instancetype) using withloading; //-(void) networkState; @ end

There are only three common methods: get, post, and upload ).

2. Call Method

There are two methods for calling this class.

1. Use [self. http...] directly under the Controller inherited from STController

The base class STController implements two sub-class interfaces by default (msgBox: pop-up message window, http: Network request)

For example:

[self.http get:UrlQuestionRank paras:nil success:^(STModel *result) {        if (result.success)        {            NSMutableArray<id> *data=(NSMutableArray<id>*)result.msg[@"data"];            STFirstTable.source=data;            [STFirstTable reloadData];        }    }];

2. Global calls can be made anywhere: use [Sagit. Http...]

Sagit is the initial namespace of A Class Library. Many common functions are started by opening the Sagit header.

For example:

// Obtain points [Sagit. http get: UrlIntegralShareBlog paras: nil success: ^ (STModel * result) {if (result. success & result. msg) {NSString * data = (NSString *) result. msg; if (data & data. isInt) {// modify the total number of current points Sagit. global. user. user. integral = data. integerValue ;}}];
3. project code example

Next, we will share with you the Code related to IT connection apps:

A. get usage: Load user information
-(Void) loadUserInfo :( NSString *) userID loadComplete :( LoadComplete) loadComplete {if (! [NSString isNilOrEmpty: self. Token]) {NSMutableDictionary * dic = nil; if (! [NSString isNilOrEmpty: userID]) {dic =@{@ "UserID": userID };}// check whether there is cache if (userID) {PersonalModel * mode = [Sagit. cache get: userID]; if (mode) {loadComplete (mode); return ;}} [Sagit. http get: UrlUserInfo paras: dic success: ^ (STModel * result) {PersonalModel * user = nil; if (result. success) {user = [[PersonalModel alloc] initWithObject: result. msg];} if (user & userID = nil) {self. user = user;} else {[Sagit. Cache set: userID value: user];} if (loadComplete! = Nil) {loadComplete (user) ;}}] ;}else if (loadComplete! = Nil) {loadComplete (nil );}}

Wood has 〜

B. post usage: Logon account
-(Void) LoginClick :( UIButton *) sender {if (! [Self isMatch: @ "mobile phone number" name: @ "UserName" regex: RexMobile] |! [Self isMatch: @ "password" name: @ "password" regex: nil]) {return;} NSMutableDictionary * para = [self formData]; [para setValue: @ (UserAccountType) forKey: @ "AccountType"]; [self. http post: UrlLogin paras: para success: ^ (STModel * result) {if (result. success) {Sagit. global. token = (NSString *) result. msg; [STNew (@ "MainController") asRoot];} else {[self. msgBox prompt :( NSString *) result. msg] ;}}] ;}
Interface diagram:

C. upload usage: upload images
-(Void) headImageClick :( UIButton *) btn {[btn. imageView pick: ^ (NSData * data, UIImagePickerController * picker, NSDictionary <NSString *, id> * info) {[self. http upload: UrlUploadPhoto paras: @ {@ "photo": data, @ "PhotoType": @ "2"} success: ^ (STModel * result) {if (result. success) {[self key: @ "uploadPhoto" value: @ "1"]; [self. msgBox prompt: @ "the Avatar is uploaded successfully! "]; [Btn image: data]; // corner: YES];} else {[self. msgBox prompt: @" An error occurred while uploading the Avatar! "] ;}}] ;}Edit: YES] ;}
Interface diagram:

4. entity class of returned results

STModel is the result of the agreed format.

@interface STModel : STModelBase@property (nonatomic, assign) BOOL success;@property (retain, nonatomic) id<NSObject> msg;@end
Msg, which can be converted based on the specific returned data. 5. STHttp is extended in IT connection.

Let's share with you the following: what are the extensions of IT connections:

A. prefix is simplified for URLs defining APIs.

/** Register */# define UrlReg @ "/user/register"/** login */# define UrlLogin @ "/user/login"

Add the prefix of the host to the extended functions.

So extended rewrite: reSetUrl function:

@ Implementation STHttp (IT)-(NSString *) reSetUrl :( NSString *) url {if (! [Url hasPrefix: @ "http: //"] &! [Url hasPrefix: @ "https: //"]) {if ([url startWith: @ "/photos/"] | [url startWith: @ "/qrcode/"]) {url = [ImageHost append: url];} else {url = [ApiHost append: url] ;}} return url;}-(void) reSetHeader {[self setHeader: @ "ver" v: AppVersionNum]; [self setHeader: @ "prod" v: STNumString (AppProdType)]; if (Sagit. global. token) {[self setHeader: @ "token" v: Sagit. global. token] ;}}- (void) showError :( NSString *) errMsg {I F (self. msgBox! = Nil) {[self. msgBox alert: @ "network connection error"] ;}}

B. Extended rewrite: reSetHeader function, used to set some fixed request headers.

C. Extended: showError, used to display the error information of network requests in a unified manner.

Summary:

Although this article introduces network requests, the shared code is also a complete functional module in the IT connection.

Sagit framework makes IOS development easier. You have the value !!!

By the way: IOS Sagit development framework QQ group: 702724292

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.