Using MAP/mPulse SDK with Third-Party Libraries - iOS

Network wrappers will be required for custom NSURLSessions or third-party frameworks to intercept the traffic for API and image.

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 for custom NSURLSession and third-party frameworks.

Shared NSURLSession - Objective-C (no wrapper is required)


NSURLSession *session = [NSURLSession sharedSession];
NSURL *requestURL = [NSURL URLwithString:@"url"];
[[session dataTaskWithURL:requestURL] resume];

Shared NSURLSession - Swift (no wrapper is required)


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

Custom NSURLSession -  Objective-C


NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration
defaultSessionConfiguration];
// ... modify sessionConfig as required by the app ...
[[AkaCommon shared] interceptSessionsWithConfiguration:sessionConfig];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];

NSURL *requestURL = [NSURL URLwithString:@"url"];
[[session dataTaskWithURL : requestURL] resume];

Custom NSURLSession- Swift


let url = URL(string: "url")
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 - Swift


let sessionConfig: URLSessionConfiguration = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
let sessionManager = Alamofire.SessionManager(configuration: sessionConfig)
let request = sessionManager.request("url", method: .get).responseJSON { response in
    let _ = sessionManager
    debugPrint(response)
}

Moya - Swift


static let provider: MoyaProvider<AppApi> = {
let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
sessionConfig.httpAdditionalHeaders = Manager.defaultHTTPHeaders
let manager = Manager(configuration: sessionConfig)
manager.startRequestsImmediately = false
let provider = MoyaProvider<AppApi>(manager: manager, plugins: [NetworkLoggerPlugin(verbose: true)])
return provider
}()

AFNetworking - Objective-C


NSURLSessionConfiguration *configuration=[NSURLSessionConfiguration defaultSessionConfiguration ];
[[AkaCommon shared] interceptSessionsWithConfiguration:sessionConfig];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: configuration];
NSURL *URL = [NSURL URLWithString:@"url"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[dataTask resume];

AFNetworking - Swift


let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
       
let url = URL(string: "url")!
let request: NSURLRequest = NSURLRequest(url: url)
let manager: AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration: sessionConfig)
let task = manager.dataTask(with: request as URLRequest, uploadProgress: { (Progress)  in
        }, downloadProgress: { (Progress) in
              }, completionHandler: { ( data, response, error) in
          })
task.resume()

Alamofire - Swift


let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
       
//Version 4.x
let sessionManager = Alamofire.SessionManager(configuration: sessionConfig)

//Version 5.x
let sessionManager = Alamofire.Session(configuration: sessionConfig)

let url = URL(string: "url")!
sessionManager.request(url, method: .get).responseJSON
{
      response in
    let _ = sessionManager// retain
}

SDWebImage Objective-C 4.x


NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
[[AkaCommon shared] interceptSessionsWithConfiguration:sessionConfig];
[[SDWebImageDownloader sharedDownloader] createNewSessionWithConfiguration:sessionConfig];
[self.imageViewObj sd_setImageWithURL:[NSURL URLWithString:@"url"] placeholderImage:[UIImage imageNamed:@"placeholder"]];

SDWebImage Objective-C 5.x


NSURLSessionConfiguration *mapSessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
[[AkaCommon shared] interceptSessionsWithConfiguration:mapSessionConfig];
SDWebImageDownloaderConfig *sdWebImageDownloaderConfig = [SDWebImageDownloaderConfig defaultDownloaderConfig];
sdWebImageDownloaderConfig.sessionConfiguration = mapSessionConfig;
SDWebImageDownloader *downloader = [[SDWebImageDownloader    alloc]initWithConfig:sdWebImageDownloaderConfig];
[downloader downloadImageWithURL:[NSURL URLWithString:@"url"] completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        self.imageView.image = image;
}];

SDWebImage Swift 4.x


let mapSessionConfig: URLSessionConfiguration = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
SDWebImageDownloader.shared() .createNewSession(with: mapSessionConfig)
self.libraryImageView.sd_setImage(with: URL(string: "url"), placeholderImage: UIImage(named: "placeholder"))

SDWebImage Swift 5.x


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

PINRemoteImage Swift


let mapSessionConfig: URLSessionConfiguration = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
self.libraryImageView.pin_setImage(from: URL(string: "url"))

Kingfisher Swift


let sessionConfig = URLSessionConfiguration.default
AkaCommon.shared().interceptSessions(with: sessionConfig)
               
let downloader = ImageDownloader(name: "KFDownloader")
downloader.sessionConfiguration  = sessionConfig
       
self.imageView.kf.setImage(with: URL(string: "url image"), placeholder: nil, options:
[.downloader(downloader)], progressBlock: { (Int64, _) in
          }) { result in               
                switch result {
                case .success(let value):
                    // The image was set to image view:
                    print(value.image)
                   
                case .failure(let error):
                    print(error) // error
                }
        }

Alamofire Image Swift


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)
       
//Older SDK 3.x
imageDownloader.download(imageURLRequest) {
response in
        let _ = imageDownloader //retain
        if response.result.value != nil {
                        self.imageView.image = response.result.value!
        }
    }    

//4.x version
imageDownloader.download(imageURLRequest) {
response in
      if case .success(let image) = response.result {
          self.imageView.image = image
      }
}

Contact Us

Questions?

If you have any questions, feel free to drop us a line!

Please contact us through our community page or via sdk-sme@akamai.com if you need assistance in debugging integration/upgrade issues.