# AFNetworking

iOS minimum versions

  • SDK: 9.0

# Add SDK

Install Codavel SDK into your project through CocoaPods, a dependency manager for Objective-c and Swift.

  • Install CocoaPods, by running the following commands in a terminal:
sudo gem install cocoapods
pod setup
1
2
  • Then, create a ‘Podfile’ in the same directory as your Xcode project. This can be done by running:
pod init
1
  • Afterwards, add the following content to your ‘Podfile’:
target "MyTargetName" do
  platform :ios, '9.0'
  use_frameworks!
  (...)
  pod 'BolinaInterceptor', :http=>'https://artifact.codavel.com:8081/artifactory/ios/BolinaInterceptor_0.10.14.zip'
  (...)
end 
1
2
3
4
5
6
7
  • Install the dependencies in your project using the command:
pod install
1
  • Build your app, with the required dependencies, by opening the generated Xcode workspace (.xcworkspace file) instead of the project file:
open <YourProjectName>.xcworkspace
1

# Start interceptor

Make sure that, before performing any HTTP/HTTPS request (a good place would be the didFinishLaunchingWithOptions method in your AppDelegate), you import and start the interceptor.

  • Import Bolina interceptor:
  • Objective-C
#import <BolinaInterceptor/BolinaInterceptor.h>
1
  • Initialize the Bolina Configuration and set the Bolina Configuration domain name:
  • Objective-C
BolinaConfiguration *config = [BolinaConfiguration initBolinaConfiguration];
[config setDomain:@"<BOLINA_SERVER_URL>"];
1
2
  • [optional] Configure your Origin:
  • Objective-C
[config addBolinaURLReplacement:@"<your.cdn.com>" url:@"<your.origin.com>"];
1

With this configuration, all requests intercepted by Bolina that would originally be sent to <your.cdn.com>, will be sent to a Bolina Server, that then will fetch the resource from <your.origin.com>. In case the Bolina transfer fails, the requests will be sent to <your.cdn.com>, as before. You can add multiple entries to be replaced. Otherwise, without this configuration, Codavel Performance Service servers will perform the same request as your HTTP clients would, to your current CDN or Origin.

  • Start the Bolina interceptor:
  • Objective-C
[BolinaInterceptor startInterceptor:@"<APP_ID>" withSecret:@"<APP_SECRET>" withConfiguration:config];
1

WARNING

Check the interceptor lifecycle for more details on how to do it

  • Add the Bolina interceptor as a configuration of AFURLSessionManager and use that session manager to perform HTTP/HTTPS requests:
  • Objective-C
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
[BolinaInterceptor registerBolina:configuration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
1
2
3

# Stop interceptor

  • Make sure you stop the interceptor whenever you don’t need it:
  • Objective-C
[BolinaInterceptor stopInterceptor];
1

WARNING

Check the interceptor lifecycle for more details on how to do it

# Other options to integrate

In case you use AFNetwork’s default shared manager, we recommend creating a new AFHTTPSessionManager registered with Bolina as a Singleton, as shown bellow, and use that instance to perform HTTP/HTTPS requests.

BolinaAFNetworkingManager.h

  • Objective-C
#import "AFNetworking.h"

@interface BolinaAFNetworkingManager : AFHTTPSessionManager 

+ (id)sharedManager;

@end
1
2
3
4
5
6
7

BolinaAFNetworkingManager.m

  • Objective-C
#import "BolinaAFNetworkingManager.h"
#import <BolinaInterceptor/BolinaInterceptor.h>

@implementation BolinaAFNetworkingManager

+ (id)sharedManager {
    static BolinaAFNetworkingManager *sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[self alloc] init];
    });
    return sharedManager;
}

- (id)init {
  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  [BolinaInterceptor registerBolina:configuration];
  
  return [super initWithSessionConfiguration:configuration];
}

@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  • You can then use this manager by using this call:
  • Objective-C
BolinaAFNetworkingManager *sharedManager = [BolinaAFNetworkingManager sharedManager];
(...)
1
2