YTKNetwork 基本用法

YTKNetwork Basic Guide:YTKNetwork 基本指導(dǎo)

In the article, we will introduce the basic usage of YTKNetwork. 在這片文章中们妥,我們將介紹YTKNetwork的基本使用方法月帝。

YTKNetwork's basic composition YTKNetwork的基本組成

YTKNetwork mainly contains the following classes: YTKNetwork主要包含以下課程:
*NETWorkConfig:it's used for setting global network host address and CDN address. YTKNetworkConfig:用于設(shè)定全球網(wǎng)絡(luò)主機(jī)地址和CDN地址绍申。
*YTKRequest :it's the parent of all the detailed network request classes. All network request classes should inherit it. Every subclass of YTKRequest represents a specific network request. YTKRequest:父類的所有詳細(xì)的網(wǎng)絡(luò)請求汁讼。所有的網(wǎng)絡(luò)請求應(yīng)該繼承它的類僵控。每一子類YTKRequest代表一個特定的網(wǎng)絡(luò)請求欲间。

We will explain the above 2 classes' detailed usage below. 我們將解釋以下2類以上的詳細(xì)用法荤傲。

YTKNetworkConfig class YTKNetworkConfig類

YTKNetworkConfig class has 2 usages: YTKNetworkConfig類有兩個用途:
1.Set global network host address and CDN address. 建立全球網(wǎng)絡(luò)主機(jī)地址和CDN地址耻涛。
2.Manage the filters which implemented YTKUrlFilterProtocol protocol(we will discuss it in pro usage guide)呐伞。 管理的過濾器實現(xiàn)YTKUrlFilterProtocol協(xié)議(我們將討論它在專業(yè)使用指南)敌卓。
We use YTKNetworkConfig to set global network host address because:

我們使用YTKNetworkConfig設(shè)置全球網(wǎng)絡(luò)主機(jī)地址,因為:
1.According to the Do Not Repeat Yourself principle,we should write the host address only once. 根據(jù)不重復(fù)自己的原則,我們應(yīng)該只寫一次主機(jī)地址荸哟。
2.In practise, our testers need to switch host addresses at runtime. YTKNetworkConfig can satisfy such requirement. 在實踐中,我們的測試人員需要在運行時切換主機(jī)地址假哎。YTKNetworkConfig可以滿足這樣的要求。
We should set YTKNetworkConfig's property at the beggining of app launching, the sample is below:

我們應(yīng)該設(shè)置YTKNetworkConfig的屬性在應(yīng)用程序啟動的發(fā)出召喚,下面的示例:

- (BOOL)application:(UIApplication *)application 
   didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   YTKNetworkConfig *config = [YTKNetworkConfig sharedInstance];
   config.baseUrl = @"http://yuantiku.com";
   config.cdnUrl = @"http://fen.bi";
}

After setting, all network requests will use YTKNetworkConfig's baseUrl property as their host addresses, and they will use the cdnUrl property of YTKNetworkConfig as their CDN addresses.

設(shè)置后,所有的網(wǎng)絡(luò)請求將使用YTKNetworkConfig baseUrl作為他們的主機(jī)地址,他們將使用cdnUrl作為YTKNetworkConfig 的CDN地址鞍历。
If we want to switch server address, we can just change YTKNetworkConfig's baseUrl property.

如果我們想要切換服務(wù)器地址,我們可以改變YTKNetworkConfig baseUrl屬性舵抹。

YTKRequest class YTKRequest 類

The design idea of YTKNetwork is that every specific network request should be a object. So after using YTKNetwork, all your request classes should inherit YTKNetwork. Through overwriting the methods of super class, you can build your own specific and distinguished request. The key idea behind this is somewhat like the Command pattern.

YTKNetwork的設(shè)計理念是,每一個特定的網(wǎng)絡(luò)請求應(yīng)該是一個對象。所以使用YTKNetwork后,所有你的要求應(yīng)該繼承YTKNetwork類劣砍。通過重寫父類的方法,您可以構(gòu)建自己的特定的和杰出的請求惧蛹。這背后的關(guān)鍵理念有點像命令模式。
For example, if we want to send a POST request to http://www.yuantiku.com/iphone/register刑枝,with username and password as arguments, then the class should be as following:

例如,如果我們想要發(fā)送一個POST請求到http://www.yuantiku.com/iphone/register,用戶名和密碼作為參數(shù),那么類應(yīng)該如下:

// RegisterApi.h
#import "YTKRequest.h"

@interface RegisterApi : YTKRequest

- (id)initWithUsername:(NSString *)username password:(NSString *)password;

@end


// RegisterApi.m


#import "RegisterApi.h"

@implementation RegisterApi {
    NSString *_username;
    NSString *_password;
}

- (id)initWithUsername:(NSString *)username password:(NSString *)password {
    self = [super init];
    if (self) {
        _username = username;
        _password = password;
    }
    return self;
}

- (NSString *)requestUrl {
    // “http://www.yuantiku.com” is set in YTKNetworkConfig, so we ignore it
    return @"/iphone/register";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPost;
}

- (id)requestArgument {
    return @{
        @"username": _username,
        @"password": _password
    };
}

@end

888888888888888888888888

In above example: 在上面的例子:

Through overwriting requestUrl method, we've indicated the detailed url. Bacause host address has been set in YTKNetworkConfig, we should not write the host address in requestUrl method.
通過覆蓋requestUrl方法,我們表示詳細(xì)的url香嗓。因為在YTKNetworkConfig主機(jī)地址,我們不應(yīng)該寫requestUrl主機(jī)地址的方法。
Through overwriting requestMethod method, we've indicated the use of the POST method.
通過覆蓋requestMethod方法,我們使用POST方法装畅。
Through overwriting requestArgument method, we've provided the POST data. If arguments username and password contain any charaters which should be escaped, the library will do it automatically.
通過覆蓋requestArgument方法,我們提供了POST數(shù)據(jù)靠娱。如果用戶名和密碼參數(shù)包含任何特征應(yīng)該逃,圖書館會自動。
Call RegisterApi 叫RegisterApi
OK, how can we use the RegisterApi? We can call it in the login view controller. After initializing the instance, we can call its start or startWithCompletionBlockWithSuccess method to send the request to the network request queue.

好的,我們?nèi)绾问褂肦egisterApi嗎?我們可以叫它登錄視圖控制器掠兄。初始化實例后,可以調(diào)用它的啟動或startWithCompletionBlockWithSuccess方法發(fā)送請求到網(wǎng)絡(luò)請求隊列像云。

Then we can get network response by block or delegate mechanism.

然后我們可以通過阻止或委托網(wǎng)絡(luò)響應(yīng)機(jī)制。

- (void)loginButtonPressed:(id)sender {
    NSString *username = self.UserNameTextField.text;
    NSString *password = self.PasswordTextField.text;
    if (username.length > 0 && password.length > 0) {
        RegisterApi *api = [[RegisterApi alloc] initWithUsername:username password:password];
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            // you can use self here, retain cycle won't happen
            NSLog(@"succeed");

        } failure:^(YTKBaseRequest *request) {
            // you can use self here, retain cycle won't happen
            NSLog(@"failed");
        }];
    }
}

Kindly be noted that you can use self directly in the block where the retain cycle won't happen. Because YTKRequest will set callback block to nil, so the block will be released right after the network request completed.

請注意,您可以使用直接自我的塊保留周期不會發(fā)生蚂夕。因為YTKRequest將回調(diào)塊為零,所以塊將網(wǎng)絡(luò)請求完成后發(fā)布迅诬。

Besides the block callback, YTKRequest also support delegate callback method. The example is below:

除了塊回調(diào),YTKRequest也支持委托回調(diào)方法。下面的例子是:

- (void)loginButtonPressed:(id)sender {
    NSString *username = self.UserNameTextField.text;
    NSString *password = self.PasswordTextField.text;
    if (username.length > 0 && password.length > 0) {
        RegisterApi *api = [[RegisterApi alloc] initWithUsername:username password:password];
        api.delegate = self;
        [api start];
    }
}

- (void)requestFinished:(YTKBaseRequest *)request {
    NSLog(@"succeed");
}

- (void)requestFailed:(YTKBaseRequest *)request {
    NSLog(@"failed");
}

Verify response JSON 驗證響應(yīng)JSON
The response JSON from the server cannnot be always trusted. Client may crash if the data is returned in faulty format from the server.

來自服務(wù)器的響應(yīng)JSON總不會是可信的婿牍〕薮客戶可能會崩潰,如果數(shù)據(jù)從服務(wù)器返回錯誤的格式。

YTKRequest provides a simple way to verity the respose JSON.

YTKRequest真實性respose JSON提供了一個簡單的方法等脂。

For example, let's say we need to send a GET request to http://www.yuantiku.com/iphone/users address with a argument named userId. The server will return the target user's information, including nickname and level. We shall guarantee that the response type of nickname is string and the type of level is number. To ensure this, we can overwrite the jsonValidator as following:

舉個例子,假設(shè)我們需要發(fā)送一個GET請求到http://www.yuantiku.com/iphone/users地址和一個名叫userId參數(shù)俏蛮。服務(wù)器將返回目標(biāo)用戶的信息,包括昵稱和水平撑蚌。我們將保證昵稱的響應(yīng)類型是字符串和水平數(shù)的類型。為了確保這一點,我們可以覆蓋jsonValidator如下:

- (id)jsonValidator {
    return @{
        @"nick": [NSString class],
        @"level": [NSNumber class]
    };
}
The whole code sample is below:  整個代碼示例如下:

// GetUserInfoApi.h
#import "YTKRequest.h"

@interface GetUserInfoApi : YTKRequest

- (id)initWithUserId:(NSString *)userId;

@end


// GetUserInfoApi.m
#import "GetUserInfoApi.h"

@implementation GetUserInfoApi {
    NSString *_userId;
}

- (id)initWithUserId:(NSString *)userId {
    self = [super init];
    if (self) {
        _userId = userId;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"/iphone/users";
}

- (id)requestArgument {
    return @{ @"id": _userId };
}

- (id)jsonValidator {
    return @{
        @"nick": [NSString class],
        @"level": [NSNumber class]
    };
}

@end

Here is some other samples:這里有一些其他的樣品:

Require return String array: 需要返回字符串?dāng)?shù)組:

- (id)jsonValidator {
    return @[ [NSString class] ];
}
Here is one complex sample from our company:  這是一個復(fù)雜的樣品從我們的公司:
- (id)jsonValidator {
    return @[@{
        @"id": [NSNumber class],
        @"imageId": [NSString class],
        @"time": [NSNumber class],
        @"status": [NSNumber class],
        @"question": @{
            @"id": [NSNumber class],
            @"content": [NSString class],
            @"contentType": [NSNumber class]
        }
    }];
} 

Use CDN address 使用CDN地址
If you need to use CDN address in some of your requests, just overwrite the - (BOOL)useCDN; method, and return YESin the method.

如果你需要使用CDN地址在你的請求,只是覆蓋——(BOOL)useCDN;方法,并返回YESin方法嫁蛇。

For example, if we have a interface for image downloading, and the address is http://fen.bi/image/imageId with the host http://fen.bi as the CDN address. Then the code should be below:

例如,如果我們有一個界面圖片下載,地址是http://fen锨并。bi /圖像/ imageId與宿主http://fen。bi作為CDN地址睬棚。然后下面的代碼應(yīng)該是:

// GetImageApi.h
#import "YTKRequest.h"

@interface GetImageApi : YTKRequest
- (id)initWithImageId:(NSString *)imageId;
@end

// GetImageApi.m
#import "GetImageApi.h"

@implementation GetImageApi {
    NSString *_imageId;
}

- (id)initWithImageId:(NSString *)imageId {
    self = [super init];
    if (self) {
        _imageId = imageId;
    }
    return self;
}

- (NSString *)requestUrl {
    return [NSString stringWithFormat:@"/iphone/images/%@", _imageId];
}

- (BOOL)useCDN {
    return YES;
}

@end

Resumable Downloading 可恢復(fù)的下載
If you want to enable resumable downloading, you just need to overwrite the resumableDownloadPath method and provide a temporary path to save the half-downloaded data.

如果你想使可恢復(fù)的下載,你只需要覆蓋resumableDownloadPath方法和提供一個臨時路徑保存half-downloaded數(shù)據(jù)第煮。

We can modify above example to support resumable downloading.

我們可以修改上面的例子來支持可恢復(fù)的下載。

@implementation GetImageApi {
    NSString *_imageId;
}

- (id)initWithImageId:(NSString *)imageId {
    self = [super init];
    if (self) {
        _imageId = imageId;
    }
    return self;
}

- (NSString *)requestUrl {
    return [NSString stringWithFormat:@"/iphone/images/%@", _imageId];
}

- (BOOL)useCDN {
    return YES;
}

- (NSString *)resumableDownloadPath {
    NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *cachePath = [libPath stringByAppendingPathComponent:@"Caches"];
    NSString *filePath = [cachePath stringByAppendingPathComponent:_imageId];
    return filePath;
}

@end

Cache response data 緩存響應(yīng)數(shù)據(jù)
We've implemented the GetUserInfoApi before, which is used for getting user information.

我們已經(jīng)實現(xiàn)了GetUserInfoApi之前,用于獲取用戶信息抑党。

We may want to cache the response. In the following example, we overwrite the cacheTimeInSeconds method, then our API will automatically cache data for specified amount of time. If the cached data is not expired, the api's start and startWithCompletionBlockWithSuccess will return cached data as a result directly.

我們可能想要緩存響應(yīng)包警。在接下來的例子中,我們覆蓋cacheTimeInSeconds方法,那么我們的API為指定的時間會自動緩存數(shù)據(jù)。如果緩存數(shù)據(jù)沒有過期,api的開始和startWithCompletionBlockWithSuccess將直接返回緩存的數(shù)據(jù)結(jié)果底靠。

@implementation GetUserInfoApi {
    NSString *_userId;
}

- (id)initWithUserId:(NSString *)userId {
    self = [super init];
    if (self) {
        _userId = userId;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"/iphone/users";
}

- (id)requestArgument {
    return @{ @"id": _userId };
}

- (id)jsonValidator {
    return @{
        @"nick": [NSString class],
        @"level": [NSNumber class]
    };
}

- (NSInteger)cacheTimeInSeconds {
    // cache 3 minutes, which is 60 * 3 = 180 seconds
    return 60 * 3;
}

@end

The cache mechanism is transparent to the controller, which means the request caller may get the result right after invoking the request without casuing any real network traffic as long as its cached data remains valid.

緩存機(jī)制對控制器是透明的,這意味著請求調(diào)用者可能會調(diào)用請求的結(jié)果后沒有因為任何真正的網(wǎng)絡(luò)流量,只要其緩存的數(shù)據(jù)仍然有效害晦。

The above code samples are available in the YTKNetworkDemo project.

上面的代碼樣本YTKNetworkDemo項目中是可用的。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末暑中,一起剝皮案震驚了整個濱河市壹瘟,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鳄逾,老刑警劉巖稻轨,帶你破解...
    沈念sama閱讀 211,376評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異雕凹,居然都是意外死亡殴俱,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評論 2 385
  • 文/潘曉璐 我一進(jìn)店門枚抵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來线欲,“玉大人,你說我怎么就攤上這事汽摹±罘幔” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評論 0 347
  • 文/不壞的土叔 我叫張陵逼泣,是天一觀的道長趴泌。 經(jīng)常有香客問我,道長圾旨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,432評論 1 283
  • 正文 為了忘掉前任魏蔗,我火速辦了婚禮砍的,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘莺治。我一直安慰自己廓鞠,他們只是感情好帚稠,可當(dāng)我...
    茶點故事閱讀 65,519評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著床佳,像睡著了一般滋早。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上砌们,一...
    開封第一講書人閱讀 49,792評論 1 290
  • 那天杆麸,我揣著相機(jī)與錄音,去河邊找鬼浪感。 笑死昔头,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的影兽。 我是一名探鬼主播揭斧,決...
    沈念sama閱讀 38,933評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼峻堰!你這毒婦竟也來了讹开?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評論 0 266
  • 序言:老撾萬榮一對情侶失蹤捐名,失蹤者是張志新(化名)和其女友劉穎旦万,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體桐筏,經(jīng)...
    沈念sama閱讀 44,143評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡纸型,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,488評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了梅忌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狰腌。...
    茶點故事閱讀 38,626評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖牧氮,靈堂內(nèi)的尸體忽然破棺而出琼腔,到底是詐尸還是另有隱情,我是刑警寧澤踱葛,帶...
    沈念sama閱讀 34,292評論 4 329
  • 正文 年R本政府宣布丹莲,位于F島的核電站,受9級特大地震影響尸诽,放射性物質(zhì)發(fā)生泄漏甥材。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,896評論 3 313
  • 文/蒙蒙 一性含、第九天 我趴在偏房一處隱蔽的房頂上張望洲赵。 院中可真熱鬧,春花似錦、人聲如沸叠萍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽苛谷。三九已至冷守,卻和暖如春凌唬,著一層夾襖步出監(jiān)牢的瞬間羊赵,已是汗流浹背蹬昌。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留赫蛇,地道東北人绵患。 一個月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像悟耘,于是被迫代替她去往敵國和親落蝙。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,494評論 2 348

推薦閱讀更多精彩內(nèi)容

  • 記得呀 那是小學(xué)幾年級的時候 和小霸王做同桌 那一年做的是一體的長板凳 我起立回答問題 他把凳子無聲無息的撤到后面...
    橘子與長頸鹿閱讀 104評論 0 1
  • 在一次事故中 我喪失了身體的某些記憶 雖然我仍完好的呈現(xiàn)在這里 但是意識已經(jīng)近乎死過一次 某些通道關(guān)閉了 曾經(jīng)自然...
    滿_3539閱讀 190評論 3 1
  • 有時候我覺得:生活不止有眼前的茍且暂幼,還有你眼不能見的茍且筏勒! 但是人活著,總不能因為外力而停止前進(jìn)吧旺嬉。你是愿意做一個...
    妍玖久閱讀 219評論 0 1
  • 〖每日拔拔草〗三度思維空性 1.和陳蓓蓓去華芝整形其實一個私心的念頭就是希望她能接受這種分享經(jīng)濟(jì)模式管行,這樣我就有了...
    lindacheng2017閱讀 127評論 0 0