IOS博客項目搭建-18-項目分層思想

本節將對項目分層,對整個項目進行重構,首先對網絡請求框架進行重構,封裝。json

##目前項目存在的問題:api

###一、對第三方框架依賴性太強,如AFNetworking\AFN網絡請求。### 首頁獲取數據,發微博,若是AFN升級,新版本更新大或中止維護,那麼就會出現問題,若是須要換框架則會很痛苦。 怎麼解決該問題呢?通過思索後,咱們能夠本身封裝一個HttpTool工具類,這個工具類專門用來發送網絡請求,它把咱們用的第三方框架封裝起來,下次控制器直接請求HttpTool,若是AFN升級,只需修改HttpTool類便可,這就是分層的好處。網絡

AFN

###封裝HttpTool網絡請求工具類框架

***IWHttpTool.h ***ide

//
//  IWHttpTool.h
//
//   封裝整個項目的GET\POST請求

#import <Foundation/Foundation.h>

@interface IWHttpTool : NSObject

/**
 * 發送一個POST請求
 *
 * @param url       請求路徑
 * @param params    請求參數
 * @param success   請求成功後的回調
 * @param failure   請求失敗後的回調
 *
 */
+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success failure:(void(^)(NSError *error))failure;

@end

**實現方法IWHttpTool.m **工具

//
//  IWHttpTool.m
//  ItcastWeibo
//  Created by kaiyi on 16-5-29.

#import "IWHttpTool.h"
#import "AFNetworking.h"

@implementation IWHttpTool

+ (void)postWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // AFNetworking\AFN
    // 1.建立請求管理對象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    // 2.發送請求
    [mgr GET:url parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        if(success){
            success(responseObject);
        }

    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        if(failure){
            failure(error);
        }

    }];

}
@end

###將項目中使用的AFN網絡請求用封裝的工具類HttpTool替換post

/**
 *  經過code換取一個accessToken
 redirect_uri	true	string	回調地址,需需與註冊應用裏的回調地址一致。
 */
- (void)accessTokenWithCode:(NSString *)code
{
    // 1.封裝請求參數
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"client_id"] = IWAppKey;
    params[@"client_secret"] = IWAppSecret;
    params[@"grant_type"] = @"authorization_code";
    params[@"code"] = code;
    params[@"redirect_uri"] = IWRedirectURI;

    // =====***HttpTool***========
    // 2.發送請求,請求成功後,須要調用這整個successBlock
    [IWHttpTool postWithURL:@"https://api.weibo.com/oauth2/access_token" params:params success:^(id json)
    {
        // 4.先將字典轉爲模型
        IWAccount *account = [IWAccount accountWithDict:json];

        // 5.存儲模型數據
        [IWAccountTool saveAccount:account];

        // 6.新特性\去首頁
        [IWWeiboTool chooseRootController];

        // 7.隱藏提醒框
        [MBProgressHUD hideHUD];

    } failure:^(NSError *error)
    {
        // 隱藏提醒框
        [MBProgressHUD hideHUD];

    }];

    /*
    // AFNetworking\AFN
    // 1.建立請求管理對象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    // 2.封裝請求參數
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"client_id"] = IWAppKey;
    params[@"client_secret"] = IWAppSecret;
    params[@"grant_type"] = @"authorization_code";
    params[@"code"] = code;
    params[@"redirect_uri"] = IWRedirectURI;

    // 3.發送請求
    [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          // 4.先將字典轉爲模型
          IWAccount *account = [IWAccount accountWithDict:responseObject];

          // 5.存儲模型數據
          [IWAccountTool saveAccount:account];

          // 6.新特性\去首頁
          [IWWeiboTool chooseRootController];

          // 7.隱藏提醒框
          [MBProgressHUD hideHUD];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 隱藏提醒框
        [MBProgressHUD hideHUD];
    }];
     */
}

##IWHttpTool.m Get請求封裝ui

//  IWHttpTool.h

/**
 *  發送一個GET請求
 *
 *  @param url     請求路徑
 *  @param params  請求參數
 *  @param success 請求成功後的回調
 *  @param failure 請求失敗後的回調
 */
+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure;

//  IWHttpTool.m
+ (void)getWithURL:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    // 1.建立請求管理對象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    // 2.發送請求
    [mgr GET:url parameters:params
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          if (success) {
              success(responseObject);
          }
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          if (failure) {
              failure(error);
          }
      }];
}

###項目中用IWHttpTool的getWithURL:方法替換原來的get請求url

//  IWHomeViewController.m
/**
 *  得到用戶信息
 */
- (void)setupUserData
{
    // 1.建立請求管理對象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];

    // 2.封裝請求參數
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = [IWAccountTool account].access_token;
    params[@"uid"] = @([IWAccountTool account].uid);

    // 3.發送請求***====***
    [IWHttpTool getWithURL:@"https://api.weibo.com/2/users/show.json" params:params success:^(id json) {
        // 字典轉模型
        IWUser *user = [IWUser objectWithKeyValues:json];
        // 設置標題文字
        [self.titleButton setTitle:user.name forState:UIControlStateNormal];
        // 保存暱稱
        IWAccount *account = [IWAccountTool account];
        account.name = user.name;
        [IWAccountTool saveAccount:account];
    } failure:^(NSError *error) {

    }];

    /*

    // 3.發送請求

    [mgr GET:@"https://api.weibo.com/2/users/show.json" parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {

         // 字典轉模型
         IWUser *user = [IWUser objectWithKeyValues:responseObject];
         // 設置標題文字
         [self.titleButton setTitle:user.name forState:UIControlStateNormal];
         // 保存暱稱
         IWAccount *account = [IWAccountTool account];
         account.name = user.name;
         [IWAccountTool saveAccount:account];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

     }];
     */
}

注意:[IWHttpTool getWithURL:@"https://api.weibo.com/2/users/show.json" params:params success:^(id json) {} 中的json爲發送請求後返回的數據,由於類型未知,因此這裏用id通用類型表示。code

相關文章
相關標籤/搜索