AFN的基本使用

這篇文章會(huì)講到:
1.網(wǎng)絡(luò)狀態(tài)
2.post請(qǐng)求
3.get請(qǐng)求
4.actionsheet
5.從圖片庫(kù)和相機(jī)上傳圖片
6.我遇到的一個(gè)小問(wèn)題,不知道問(wèn)什么

1.檢測(cè)網(wǎng)絡(luò)狀態(tài)

    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"%ld",(long)status);
    }];
    [mgr startMonitoring];

2.POST請(qǐng)求

#pragma mark - post 請(qǐng)求  返回json
- (void)postJson
{
    //1.創(chuàng)建一個(gè)操作請(qǐng)求的管理證
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    //請(qǐng)求參數(shù)
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"username"] = @"zhangjl";
    dict[@"password"] = @"zhjl";
    NSString *url = @"http://localhost/01/post.php";
    [mgr POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSLog(@"formdata--- %@",formData);
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"請(qǐng)求成功-%@",responseObject[@"message"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"請(qǐng)求失敗--%@",error);
    }];
}

3.GET請(qǐng)求

#pragma mark - 獲得數(shù)據(jù)
- (void)getData
{
    //1.創(chuàng)建請(qǐng)求對(duì)象管理
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    //聲明一下 服務(wù)器返回使用http 默認(rèn)的方式返回?cái)?shù)據(jù),不是xml json比如下載文件
    //不要對(duì)數(shù)據(jù)庫(kù)返回的數(shù)據(jù)格式化
    //如果是文件下載 肯定是這個(gè)
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    //2.接下來(lái)發(fā)送get請(qǐng)求
    NSString *url = @"http://localhost/01/get.php";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = @"張D大調(diào)玲";
    params[@"password"] = @"zhjl";
    [mgr GET: url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //請(qǐng)求成功的時(shí)候調(diào)用
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",dict[@"message"]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //請(qǐng)求失敗時(shí)候調(diào)用
        NSLog(@"請(qǐng)求失敗");
    }];
    
}

注意:要注意根據(jù)不同的情況不同的格式處理
mgr.responseSerializer = [AFHTTPResponseSerializer serializer];

4.UIActionsheet

//創(chuàng)見(jiàn):
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"請(qǐng)選擇圖片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相冊(cè)",nil ];
    self.sheet = sheet;
    [sheet showInView:self.view.window];

//遵守協(xié)議 實(shí)現(xiàn)方法

遵守這個(gè)協(xié)議:UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//index就是第幾個(gè)按鈕
}

5.遇到的問(wèn)題

早就謝了這篇文章,但是遇到了一些問(wèn)題,一直沒(méi)有解決,我還是發(fā)布出去吧!希望遇到過(guò)這個(gè)錯(cuò)誤的朋友告訴我一下!

錯(cuò)誤描述:直接把AFN拖入到項(xiàng)目中,引入.h文件,使用相關(guān)方法就報(bào)錯(cuò)?。ㄔ趧e人那里創(chuàng)建發(fā)送給我就可以使用,自己創(chuàng)建就不行)
錯(cuò)誤截圖:

Snip20150623_2.png

別人建的項(xiàng)目都有這里邊都有很多afn的文件,我這個(gè)沒(méi)有!

Snip20150623_4.png

Snip20150623_1.png

我嘗試了什么pch很多方法,都不對(duì),感覺(jué)這個(gè)應(yīng)該是 一個(gè)很菜的錯(cuò)誤,希望大家誰(shuí)遇到過(guò)告訴我謝謝。不勝感激!??!

這個(gè)很菜錯(cuò)誤解決了:解決在下一章文章:folder與group

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容