Mobile Development 28 min read

AFNetworking Deep Dive: Request/Response Serialization, Session Management, and Security

This article provides a comprehensive overview of AFNetworking 3.x, covering its evolution, core components such as AFURLSessionManager, request and response serialization, task handling, session management, network activity indicator, reachability monitoring, and security policies including SSL pinning and certificate validation, with detailed code examples.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
AFNetworking Deep Dive: Request/Response Serialization, Session Management, and Security

AFNetworking is a widely used iOS networking library that abstracts NSURLSession and provides a high‑level API for HTTP requests.

The library has evolved through three major versions, with AFNetworking 3.x built on NSURLSession and organized around AFURLSessionManager, AFHTTPSessionManager, and related serializer classes.

Request serialization is handled by AFURLRequestSerialization, offering AFHTTPRequestSerializer, AFJSONRequestSerializer, and AFPropertyListRequestSerializer, which construct NSMutableURLRequest objects, set headers, and encode parameters.

Response serialization is performed by AFURLResponseSerialization subclasses such as AFJSONResponseSerializer and AFImageResponseSerializer, converting NSURLResponse data into usable objects.

Each network task is represented by a manager (AFHTTPSessionManager) that creates NSURLSessionDataTask, NSURLSessionUploadTask, or NSURLSessionDownloadTask, and the library tracks tasks via AFURLSessionManagerTaskDelegate, supporting progress monitoring through NSProgress and KVO.

AFNetworkActivityIndicatorManager listens to AFNetworkingTaskDidResumeNotification, AFNetworkingTaskDidSuspendNotification and AFNetworkingTaskDidCompleteNotification to show or hide the iOS network activity indicator with activation and completion delays.

AFNetworkReachabilityManager monitors network status using SCNetworkReachability, providing callbacks for changes in connectivity.

Security is managed by AFSecurityPolicy, which can enforce standard CA validation or SSL pinning (certificate or public‑key pinning). Developers can configure allowInvalidCertificates, validatesDomainName, and provide custom pinned certificates.

Example code snippets illustrate creating a GET request, configuring a request serializer, handling multipart form uploads, and setting up SSL pinning.

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager GET:@"/api/data" parameters:nil progress:nil success:nil failure:nil];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
policy.allowInvalidCertificates = YES;
manager.securityPolicy = policy;
iOSnetworkingAFNetworkingSSL PinningRequest SerializationResponse Serialization
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.