afnetworking-promisekit

AFNetworking-PromiseKit

Welcome to the documentation page for AFNetworking-PromiseKit! This page provides detailed information and examples on how to use AFNetworking-PromiseKit, an extension that combines the power of AFNetworking and PromiseKit to simplify asynchronous network requests in your iOS or macOS applications.

Installation

To install AFNetworking-PromiseKit, you need to follow these steps:

  1. Add the following line to your Podfile:

    
        pod 'AFNetworking-PromiseKit'
        
  2. Run the following command in the terminal:

    
        pod install
        
  3. Import the necessary headers in your source files:

    
        #import "AFNetworking+PromiseKit.h"
        

Usage

AFNetworking-PromiseKit provides convenient PromiseKit-based wrappers around AFNetworking’s classes and methods. Below are some examples of how to use this extension:

Making a GET Request

To make a GET request using AFNetworking-PromiseKit, use the following code:


AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlString = @"https://api.example.com/endpoint";
NSURL *url = [NSURL URLWithString:urlString];

[[manager GET:url.absoluteString parameters:nil]
    then:^(id responseObject) {
        NSLog(@"Request succeeded with response: %@", responseObject);
    }]
    catch:^(NSError *error) {
        NSLog(@"Request failed with error: %@", error);
    }];

Making a POST Request

To make a POST request using AFNetworking-PromiseKit, use the following code:


AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlString = @"https://api.example.com/endpoint";
NSURL *url = [NSURL URLWithString:urlString];

NSDictionary *parameters = @{
    @"key1": @"value1",
    @"key2": @"value2"
};

[[manager POST:url.absoluteString parameters:parameters]
    then:^(id responseObject) {
        NSLog(@"Request succeeded with response: %@", responseObject);
    }]
    catch:^(NSError *error) {
        NSLog(@"Request failed with error: %@", error);
    }];

Uploading a File

To upload a file using AFNetworking-PromiseKit, use the following code:


AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *urlString = @"https://api.example.com/upload";
NSURL *url = [NSURL URLWithString:urlString];

NSURL *fileURL = [NSURL fileURLWithPath:@"/path/to/file"];

[[manager POST:url.absoluteString parameters:nil constructingBodyWithBlock:^(id formData) {
    [formData appendPartWithFileURL:fileURL name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
}]
    then:^(id responseObject) {
        NSLog(@"Request succeeded with response: %@", responseObject);
    }]
    catch:^(NSError *error) {
        NSLog(@"Request failed with error: %@", error);
    }];

Conclusion

With AFNetworking-PromiseKit, you can easily leverage the power of asynchronous network requests in your iOS or macOS applications. This extension combines the reliability of AFNetworking with the simplicity of PromiseKit, providing a seamless experience for handling network requests in your projects.

For more detailed information and additional examples, please refer to the official documentation and GitHub repository of AFNetworking-PromiseKit.