`
janedoneway
  • 浏览: 580559 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

AFNetworking

 
阅读更多

AFNetworking on github: https://github.com/AFNetworking/AFNetworking

 


 

AFNetworking is a delightful networking library for iOS and Mac OS X. It's built on top of NSURLConnectionNSOperation, and other familiar Foundation technologies. It has a modular architecture with well-designed, feature-rich APIs that are a joy to use. For example, here's how easy it is to get JSON from a URL:

NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/public_timeline.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"Public Timeline: %@", JSON);
} failure:nil];
[operation start];

Perhaps the most important feature of all, however, is the amazing community of developers who use and contribute to AFNetworking every day. AFNetworking powers some of the most popular and critically-acclaimed apps on the iPhone, iPad, and Mac.

Choose AFNetworking for your next project, or migrate over your existing projects—you'll be happy you did!

How To Get Started

Overview

AFNetworking is architected to be as small and modular as possible, in order to make it simple to use and extend.

Core HTTP Requests HTTP Client Images
AFURLConnectionOperation An NSOperation that implements the NSURLConnection delegate methods.
AFHTTPRequestOperation A subclass of AFURLConnectionOperation for requests using the HTTP or HTTPS protocols. It encapsulates the concept of acceptable status codes and content types, which determine the success or failure of a request.
AFJSONRequestOperation A subclass of AFHTTPRequestOperation for downloading and working with JSON response data.
AFXMLRequestOperation A subclass of AFHTTPRequestOperation for downloading and working with XML response data.
AFPropertyListRequestOperation A subclass of AFHTTPRequestOperation for downloading and deserializing objects withproperty list response data.
AFHTTPClient Captures the common patterns of communicating with an web application over HTTP, including:
  • Making requests from relative paths of a base URL
  • Setting HTTP headers to be added automatically to requests
  • Authenticating requests with HTTP Basic credentials or an OAuth token
  • Managing an NSOperationQueue for requests made by the client
  • Generating query strings or HTTP bodies from an NSDictionary
  • Constructing multipart form requests
  • Automatically parsing HTTP response data into its corresponding object representation
  • Monitoring and responding to changes in network reachability
AFImageRequestOperation A subclass of AFHTTPRequestOperation for downloading and processing images.
UIImageView+AFNetworking Adds methods to UIImageView for loading remote images asynchronously from a URL.

Example Usage

XML Request

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]];
AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
  XMLParser.delegate = self;
  [XMLParser parse];
} failure:nil];
[operation start];

Image Request

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];

API Client Request

// AFGowallaAPIClient is a subclass of AFHTTPClient, which defines the base URL and default HTTP headers for NSURLRequests it creates
[[AFGowallaAPIClient sharedClient] getPath:@"/spots/9223" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Name: %@", [responseObject valueForKeyPath:@"name"]);
    NSLog(@"Address: %@", [responseObject valueForKeyPath:@"address.street_address"]);
} failure:nil];

File Upload with Progress Callback

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];

Streaming Request

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];

AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];
operation.outputStream = [NSOutputStream outputStreamToMemory];
[operation start];

Requirements

AFNetworking requires either iOS 4.0 and above, or Mac OS 10.6 (64-bit with modern Cocoa runtime) and above.

AFNetworking uses NSJSONSerialization if it is available. If your app targets a platform where this class is not available you can include one of the following JSON libraries to your project for AFNetworking to automatically detect and use.

ARC Support

AFNetworking will transition its codebase to ARC in a future release.

If you are including AFNetworking in a project that uses Automatic Reference Counting (ARC) enabled, you will need to set the-fno-objc-arc compiler flag on all of the AFNetworking source files. To do this in Xcode, go to your active target and select the "Build Phases" tab. Now select all AFNetworking source files, press Enter, insert -fno-objc-arc and then "Done" to disable ARC for AFNetworking.

  • 大小: 82.7 KB
分享到:
评论

相关推荐

    AFNetworking4.0+封装

    在AFNetworking 4.0+版本中,开发者进行了更多的封装,使得功能更加完善,同时也提升了性能和用户体验。本文将深入探讨AFNetworking 4.0+封装的几个关键知识点,包括请求缓存、离线下载、显示缓存大小、删除缓存以及...

    AFNetWorking直接引入工程

    AFNetworking是中国iOS开发者广泛使用的网络请求库,它是由Alamofire团队为Objective-C开发者设计的。这个库使得处理网络请求变得更加简单和高效。在“AFNetWorking直接引入工程”这个主题下,我们将深入探讨如何将...

    AFNetworking的常用封装 请求

    AFNetworking是iOS开发中常用的网络请求库,它基于Objective-C编写,为iOS和macOS平台提供了强大的网络请求处理能力。本篇文章将详细讲解AFNetworking的常用封装和如何进行请求操作,帮助开发者更好地理解和使用这个...

    AFnetworking

    AFNetworking是iOS和macOS平台上的一款强大的开源网络库,由Alamofire Software Foundation维护。它以其简洁、易用和高效而备受开发者喜爱,主要用于处理HTTP网络请求,包括上传和下载任务。在iOS应用开发中,...

    ios-AFNetworking的下载、请求、上传.zip

    这个压缩包“ios-AFNetworking的下载、请求、上传.zip”显然包含了关于如何使用AFNetworking进行网络下载、请求以及上传的示例代码,虽然上传部分可能存在问题。接下来,我们将深入探讨AFNetworking的核心功能和使用...

    AFNetWorking

    AFNetWorking是iOS和macOS平台上广泛使用的网络请求库,由Alamofire Software Foundation维护。这个库是Objective-C编写的,为开发者提供了强大的HTTP网络请求功能,简化了网络编程的工作。其中,`...

    AFNetworking发送post请求出现999错误1

    AFNetworking发送post请求出现999错误解决方案 从给定的文件信息中,我们可以提炼出以下知识点: 1. AFNetworking是一个基于HTTP/HTTPS的网络请求框架,用于简化iOS和OS X应用程序中的网络交互。 在AFNetworking...

    AFNetworking2.5使用Demo

    **AFNetworking 2.5 使用详解** AFNetworking 是一个强大的 iOS 和 macOS 开发中的网络库,它基于 NSURLConnection、NSURLSession 和其他系统框架构建,提供了一套简洁易用的接口来处理 HTTP 请求和响应。在 ...

    超强的AFNetWorking封装框架

    在“超强的AFNetWorking封装框架”中,我们将会探讨如何利用AFNetworking进行POST和GET请求,以及如何对其进行优化封装,以实现更简单、更方便的网络操作。 1. **AFNetworking基础** - AFNetworking基于苹果的...

    AFNetworking3.0.3

    **AFNetworking 3.0.3:iOS网络编程的强大工具** AFNetworking,作为一个广受欢迎的开源库,是iOS开发中的重要组件,特别是在处理网络请求和数据传输方面。这个版本3.0.3提供了稳定和高效的解决方案,使得开发者...

    AFNetworking第三方网络数据请求类-iOS开发必备

    在iOS开发中,AFNetworking能够极大地提升开发效率,因为它简化了网络数据的获取、上传和下载过程。 AFNetworking的核心功能包括: 1. **GET与POST请求**:AFNetworking提供了简单的方法来发起GET和POST请求。GET...

    AFNetWorking--iOS

    **AFNetWorking——iOS平台的网络请求库** 在iOS应用开发中,网络请求是不可或缺的一部分,用于获取服务器上的数据或提交用户操作。AFNetWorking,作为iOS平台上最流行且功能强大的网络请求库,极大地简化了网络...

    基于AFNetworking的接口封装

    在iOS开发中,网络请求是应用与服务器交互的基础,而AFNetworking是Objective-C及Swift中最常用的网络库之一。本文将深入探讨如何基于AFNetworking进行接口封装,以提高代码的可复用性和可维护性。 首先,...

    AFNetworking 3.0 图片上传

    在iOS开发中,网络请求是不可或缺的一部分,而AFNetworking作为苹果平台最受欢迎的网络库,极大地简化了网络操作。本教程将深入讲解如何使用AFNetworking 3.0实现图片上传功能,这对于任何涉及到用户交互和多媒体...

    AFNetworking.3.0-兼容ipv6.zip

    在标题"AFNetworking.3.0-兼容ipv6.zip"中提到的“3.0”指的是AFNetworking的版本号,这表明我们关注的是该库的一个特定更新,即3.0版。这个版本对IPv6的支持是其重要的特性之一。描述中提到的“已实现兼容ipv6网络...

    AFNetWorking的常用封装上传下载断点续传等

    AFNetworking是iOS和macOS平台上的一个强大的网络请求库,由Alamofire Software Foundation开发并维护。这个库基于NSURLSession,并提供了简洁的API设计,使得网络请求变得更加简单易用。在这个主题中,我们将深入...

    AFNetWorking库从服务器下载图片Demo

    AFNetWorking库是iOS平台上非常流行的网络请求库,由Alamofire Software Foundation维护。这个库使得处理网络请求,包括数据加载、文件下载以及图片加载等任务变得简单高效。在本Demo中,我们将专注于如何使用...

Global site tag (gtag.js) - Google Analytics