Kumar, Anish

Upgrade mPulse SDK - version 20.32 and above for iOS

 

Introduction:

mPulse SDK version 20.32 and above is much easier to integrate and manage. mPulse now also works seamlessly with MAP SDK with smaller combined SDK size. As part of this re-architecture, some of the existing integration approaches have changed. To upgrade mPulse SDK to version 20.32 and above, you have to follow the upgrade steps. This document tells you how to upgrade the MAP SDK.

 

Prerequisites:

  • An iOS app using the mPulse SDK with an associated API/license key. 

  • Familiarity with mPulse features and configurations available with the SDK. For more information, see the Configuration Guide.

 

Sample Project: Download

We’ve created a sample project that you can use to validate the setup and functionality of the SDK. You can use this sample app to compare against your legacy and make sure everything is working correctly, as outlined in the step below.

Upgrade to mPulse SDK 20.32 or above

How you upgrade the SDK depends on how you added it. 

  • Cocoa Pod - If you have added the SDK through Cocoa Pod, then pod update will add the latest framework.

  • If you added the SDK through a ZIP file, you need to remove old SDK of mPulse, and add the latest frameworks manually.

  • Deprecated methods should be removed and be updated with the methods explained below. The instructions to do that are listed here. 

 

License

Deprecated:

Manual addition of the license is deprecated - MPulse:mPulse.initialize(withAPIKey: "key"). Add the license info in Plist, as shown below. Note that you can continue to use the same license and features.

Update:

Files

Initialization

Swift:

Deprecated: MPulse.initialize(withAPIKey: "key")

Update: AkaCommon.configure()

 

Objective-C:

Deprecated: [MPulse initializeWithAPIKey:@"key"];

Update: [AkaCommon configure];

 

Comparing the old SDK vs new Akamai Common Framework

In the following steps, the column on the left shows the existing code. Replace this code with the code in the column on the right.  

License

Legacy SDK

Akamai Common Framework SDK

mPulse.initialize(withAPIKey: "key")

save

 

Header file

Add AkaCommon and mPulse to the header file.

Legacy SDK

Akamai Common Framework SDK

#import <MPulse/MPulse.h>

#import <AkaCommon/AkaCommon.h>
#import <MPulse/MPulse.h>

Initialization

Use AkaCommon.configure to initialize the mPulse framework.

Legacy SDK

Akamai Common Framework SDK

mPulse.initialize(withAPIKey: "key")

AkaCommon.configure()

 Debug log

Use the AkaCommon shared instance to call debug method.

Legacy SDK

Akamai Common Framework SDK

MPulse.setDebug(true)

AkaCommon.shared().debugConsoleEnabled = true

 Calling methods

There is no change in how you call mPulse methods, you can call through the mPulse shared instance, MPulse.sharedInstance().

Interceptors

In the previous version of the mPulse SDK, the SDK automatically intercepted traffic to provide the desired functionality. In the Akamai Common Framework SDK version, there is a new concept of an interceptor, which requires a wrapper for custom NSURLSession or third-party frameworks. 

By default, mPulse intercepts traffic through NSURLSession. Hence, for this scenario you don’t need to use a wrapper to intercept traffic going through NSURLSession. 

NSURLSession, no change required

let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
    }
task.resume()

To intercept traffic for custom session NSURLSession or third-party frameworks, use the interceptSessions() method, which sets up NSURLSession configurations to pass requests through the SDK's URL handler. Examples are provided below for a custom NSURLSession, and also for third party frameworks.

Custom session NSURLSession

let url = URL(string: "http://www.akamai.com/")
let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
let session = URLSession.init(configuration: sessionConfig)
let task = session.dataTask(with: url!) {(data, response, error) in
}
task.resume()

Alamofire for API

let sessionManager = Alamofire.SessionManager(configuration: sessionConfig)
sessionManager.request(apiURL, method: .get).responseJSON
{
response in
let _ = sessionManager// retain
  if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
          print("Data: \(utf8Text)")
    }
}

 

AFNetworking for API

let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
     
let manager: AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration: sessionConfig)
let task = manager.dataTask(with: uRLRequest, uploadProgress: { (Progress)  in
  }, downloadProgress: { (Progress) in
        }, completionHandler: { ( data, response, error) in
        })
task.resume()

 

Kingfisher for Image

let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
   
let downloader = ImageDownloader(name: "KFDownloader")
downloader.sessionConfiguration  = sessionConfig
imageView.kf.setImage(with: URL(string: "image url"), options: [.downloader(downloader)])

 

AlamofireImage for Image

let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
     
let imageDownloader = ImageDownloader(
            configuration: sessionConfig,    //add session config to the downloader
            downloadPrioritization: .fifo,
            maximumActiveDownloads: 4,
            imageCache: AutoPurgingImageCache()
)


let imageURL = URL(string: "image url")!
let imageURLRequest = URLRequest(url: imageURL)
imageDownloader.download(imageURLRequest) { response in
            let _ = imageDownloader //retain
          if response.result.value != nil {
            self. imageView.image = response.result.value!
            }
}

SDWebimage for Image

let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
     
let sdWebImageDownloaderConfig = SDWebImageDownloaderConfig.default
sdWebImageDownloaderConfig.sessionConfiguration = sessionConfig
let downloader = SDWebImageDownloader.init(config: sdWebImageDownloaderConfig)
downloader .downloadImage(with: URL(string: "image url")) { (UIImage, Data, Error, Bool) in
            self.imageView.image = UIImage
}