# Alamofire

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:
  • Swift
import BolinaInterceptor
1
  • Initialize the Bolina Configuration and set the Bolina Configuration domain name:
  • Swift
guard let config = BolinaConfiguration.initBolinaConfiguration() as? BolinaConfiguration else {
    // error initialising Bolina's configuration
    return
}
config.setDomain("<BOLINA_SERVER_URL>")
1
2
3
4
5
  • [optional] Configure your Origin:
  • Swift
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:
  • Swift
BolinaInterceptor.start("<APP_ID>", withSecret: "<APP_SECRET>", with: config)
1

WARNING

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

  • Register Bolina interceptor in the configuration of Alamofire.SessionManager and use that session manager to perform HTTP/HTTPS requests:
  • Swift
let configuration = URLSessionConfiguration.default
BolinaInterceptor.registerBolina(configuration)
manager = Alamofire.Session(configuration: configuration)
1
2
3

# Stop interceptor

  • Make sure you stop the interceptor whenever you don’t need it:
  • Swift
BolinaInterceptor.stop()
1

WARNING

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

# Other options to integrate

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

  • An example of how to create one is below:
  • Swift
class BolinaAlamofireManager {

    static let shared = BolinaAlamofireManager()
    private var manager: Alamofire.Session = Alamofire.Session()
    
    init() {
        let configuration = URLSessionConfiguration.default
        BolinaInterceptor.registerBolina(configuration)
        manager = Alamofire.Session(configuration: configuration)
    }
}
1
2
3
4
5
6
7
8
9
10
11
  • Then you are able to use this manager in your application using this call:
  • Swift
BolinaAlamofireManager.shared.manager.request((...))
1