码迷,mamicode.com
首页 > Web开发 > 详细

Object-C开发教程--如何在项目中使用AFNetworking

时间:2015-07-12 23:21:26      阅读:1512      评论:0      收藏:0      [点我收藏+]

标签:object-c   开发教程   ios   iphone   app   

AFNetworking 是 iOS 一个使用很方便的网络开发框架。今天我们就简单介绍如何在我们的项目中使用它。

1、从官网下载最新的AFNetworking代码。

2、将AFNetWorking和UIKit+AFNetworking文件夹导入项目 
3、添加类库 Security.framework、MobileCoreServices.framework、SystemConfiguration.framework 
4、在使用的地方导入

#import "AFNetworking.h"

#import "UIKit+AFNetworking.h"

5、网络url访问测试。

    NSString *URLTmp = @"http://www.coneboy.com";

    NSString *URLTmp1 = [URLTmp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    //转码成UTF-8  否则可能会出现错误

    URLTmp = URLTmp1;

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: URLTmp]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"Success: %@", operation.responseString);

        NSString *requestTmp = [NSString stringWithString:operation.responseString];

        NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];

        //系统自带JSON解析

        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];

        NSLog(@"success:%@",resultDic);

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

        NSLog(@"Failure: %@", error);

        //[SVProgressHUD dismissWithError:@"提交失败,请重试"];

    }];

    [operation start];

6、如何获取一张网络图片

NSString *url = @"http://www.uimaker.com/uploads/allimg/111101/1_111101085050_3.jpg";

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 150.0f, 200.0f, 200.0f)];

    [imageView setImageWithURL:[NSURL URLWithString:url]];

    [self.view addSubview:imageView];



版权声明:本文为博主原创文章,未经博主允许不得转载。

Object-C开发教程--如何在项目中使用AFNetworking

标签:object-c   开发教程   ios   iphone   app   

原文地址:http://blog.csdn.net/wanglixin1999/article/details/46854263

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!