Objective-c开发教程--如何在项目中使用AFNetworking

本文介绍了如何在iOS项目中使用AFNetworking框架进行网络请求,并提供了获取网络图片的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 = [URLTmpstringByAddingPercentEscapesUsingEncoding: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];



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值