Platform Services
The Platform Services are provided by the Adobe Experience Platform Mobile SDKs as part of the Mobile Core extension. These services provide shared functionality throughout the SDK that can be shared by extensions. For example, services provide shared functionality for networking, data queuing, caching, and more.
This feature is only available in Android Core 1.8.0 or later and iOS Core 3.0.0 or later.
Service
Android SDK
iOS SDK
Core 1.8.0 +
AEPCore 3.0.0 +
DataQueue
Core 1.8.0 +
AEPCore 3.0.0 +
Core 1.8.0 +
AEPCore 3.0.0 +

Accessing Services

The MobileCore extension provides a shared ServiceProvider, responsible for accessing the current set of provided services.
The following code snippet shows how to access Network Service as an example.
Android
iOS (AEP 3.x)
1
import com.adobe.marketing.mobile.services.*;
2
3
// Get an instance of the current network service
4
Networking networkService = ServiceProvider.getInstance().getNetworkService();
Copied!
1
import AEPServices
2
3
// Get an instance of the current network service
4
let networkService = ServiceProvider.shared.networkService
Copied!

Override network stack

This section walks through the steps necessary to create a custom network override, and register it with the SDK.
This feature is only available in Android Core version 2.5.0 or later and iOS Core version 2.6.0 or later.
Android
iOS

1. Create custom HTTPConnectionPerformer implementation

The HTTPConnectionPerformer class is an abstract base class that must be subclassed. This class contains one required method, connect, which must be overridden. Optionally, it's possible to override the shouldOverride method if you want to conditionally override network requests (if you do not override this method, all requests will be overridden by default).
Example
This is just an implementation example. For more information about handling network requests correctly in your mobile application, see HttpURLConnection.
1
package com.adobe.example;
2
3
import java.io.BufferedOutputStream;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.OutputStream;
7
import java.net.HttpURLConnection;
8
import java.net.MalformedURLException;
9
import java.net.URL;
10
import java.util.Map;
11
import com.adobe.marketing.mobile.AndroidNetworkServiceOverrider.HTTPConnectionPerformer;
12
import com.adobe.marketing.mobile.AndroidNetworkServiceOverrider.Connection;
13
14
// Sample implementation of HTTPConnectionPerformer for AEP SDK Network Override.
15
class SampleHTTPConnectionPerformer extends HTTPConnectionPerformer {
16
// Modifications here would allow for conditional overriding based on the url/method.
17
// Overriding this method is optional, by default it will always return true (meaning
18
// all network requests should be overridden).
19
@Override
20
public boolean shouldOverride(String url, String method) {
21
return true;
22
}
23
24
// Overriding the connect method is required. This method must perform a synchronous
25
// (blocking) network request and return an object conforming to the
26
// Connection interface.
27
@Override
28
public Connection connect(String url, String method, byte[] payload, Map<String, String> headers, int connectionTimeoutSeconds, int readTimeoutSeconds) {
29
try {
30
final URL dest = new URL(url);
31
final HttpURLConnection con = (HttpURLConnection) dest.openConnection();
32
con.setReadTimeout(readTimeoutSeconds * 1000);
33
con.setConnectTimeout(connectionTimeoutSeconds * 1000);
34
35
con.setRequestMethod(method);
36
37
for (final Map.Entry<String, String> entry : headers.entrySet()) {
38
con.setRequestProperty(entry.getKey(), entry.getValue());
39
}
40
41
if (method.equals("POST") && payload != null) {
42
con.setFixedLengthStreamingMode(payload.length);
43
}
44
45
con.connect();
46
47
if (method.equals("POST") && payload != null) {
48
final OutputStream os = new BufferedOutputStream(con.getOutputStream());
49
os.write(payload);
50
os.flush();
51
os.close();
52
}
53
54
final InputStream inputStream = con.getInputStream();
55
final int responseCode = con.getResponseCode();
56
final String responseMessage = con.getResponseMessage();
57
58
return new AndroidNetworkServiceOverrider.Connection() {
59
@Override
60
public InputStream getInputStream() {
61
return inputStream;
62
}
63
64
@Override
65
public int getResponseCode() {
66
return responseCode;
67
}
68
69
@Override
70
public String getResponseMessage() {
71
return responseMessage;
72
}
73
74
@Override
75
public String getResponsePropertyValue(String responsePropertyKey) {
76
final String responseHeaderValue = con.getHeaderField(responsePropertyKey);
77
return responseHeaderValue;
78
}
79
80
@Override
81
public void close() {
82
if (inputStream != null) {
83
try {
84
inputStream.close();
85
} catch (final Exception ex) {
86
}
87
}
88
con.disconnect();
89
}
90
};
91
92
93
} catch (final MalformedURLException ex) {
94
return CONNECTION_ERROR_URL;
95
96
} catch (final IOException ex) {
97
return CONNECTION_ERROR_IO;
98
}
99
}
100
}
Copied!
Your implementation must return an object conforming to the Connection interface when the connection has succeded. If the connection does not succeed, you will need to return these constants (depending on the scenario):
  • For all URL parsing / malformed URL issues, return HTTPConnectionPerformer.CONNECTION_ERROR_URL.
  • For any IOExceptions or any other errors, return HTTPConnectionPerformer.CONNECTION_ERROR_IO.

2. Register your interface with the SDK.

This step should occur prior to any other interactions with the AEP SDK. While it's possible to register the network override at any point during the application lifecycle, the override will only function for network requests performed after the registration has taken place.
1
import com.adobe.marketing.mobile.AndroidNetworkServiceOverrider;
2
import com.adobe.marketing.mobile.MobileCore;
3
4
public void onCreate() {
5
super.onCreate();
6
7
// Register network override prior to making any other calls to the AEP SDK
8
AndroidNetworkServiceOverrider.setHTTPConnectionPerformer(new SampleHTTPConnectionPerformer());
9
10
// First call to AEP SDK
11
MobileCore.setApplication(this);
12
//... continue with initialization / registering extensions.
13
}
Copied!

1. Conform to the ACPHttpConnectionPerformer protocol

The ACPHttpConnectionPerformer is a protocol which must be conformed to in order to override the network stack. It provides two methods which must be implemented:
  1. 1.
    shouldOverride, which takes a URL and an HTTP method. Return true if you want the network stack to be overridden by the performer, or false if not.
  2. 2.
    requestUrl, which is the URL request override with a completion block.

2. Create an ACPHttpConnection and pass relevant data from your NSURLSessionDataTask completion block

The completion block for the requestUrl method takes an ACPHttpConnection as its parameter. The ACPHttpConnection is used when overriding the network stack in place of the internal network connection implementation and represents the response to an HTTP request. It is to be created using the NSURLResponse and NSData from your url request response. In the case of a Network Error, or timeout, the ACPHttpConnection* is expected to be nil.
Example
This is just an implementation example. For more information about handling network requests correctly in your mobile application, see NSURLSessionConfiguration and NSMutableURLRequest.
Objective-C
1
#import <Foundation/Foundation.h>
2
#import "ACPNetworkServiceOverrider.h"
3
4
@interface SamplePerformerOverrider : NSObject<ACPHttpConnectionPerformer>
5
6
@end
7
8
@implementation SamplePerformerOverrider {
9
void (^requestUrlCompletion)(ACPHttpConnection*);
10
}
11
12
// Modifications here would allow for conditional overriding based on the url/method.
13
// In this example it always returns true to override all network requests.
14
- (BOOL) shouldOverride: (NSURL*) url method: (NSString*) method {
15
return true;
16
}
17
18
// Network request override with a completion block. The provided parameters should be configured on the network request.
19
- (void) requestUrl: (NSURL*) url httpCommand: (NSString*) command connectPayload: (NSString*) payload requestPropertyDict: (NSDictionary<NSString*, NSString*>*) requestProperty connectTimeout: (NSTimeInterval) connectTimeout readTimeout: (NSTimeInterval) readTimeout completion: (void (^) (ACPHttpConnection*)) completion {
20
21
requestUrlCompletion = completion;
22
23
// Create the NSURLSessionConfiguration with the provided timeouts
24
NSURLSessionConfiguration* config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
25
config.URLCache = nil;
26
config.timeoutIntervalForRequest = readTimeout;
27
config.timeoutIntervalForResource = connectTimeout;
28
29
NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
30
31
// Create an NSURLRequest with the provided request parameters
32
NSMutableURLRequest* request = [NSMutableURLRequest new];
33
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
34
[request setHTTPMethod:command];
35
[request setURL:url];
36
37
if (payload.length > 0 && [@"POST" isEqualToString:[command uppercaseString]]) {
38
[request setHTTPBody:[payload dataUsingEncoding:NSUTF8StringEncoding]];
39
}
40
41
for (NSString *key in requestProperty) {
42
NSString* value = requestProperty[key];
43
[request setValue:value forHTTPHeaderField:key];
44
}
45
46
// Start the request
47
NSURLSessionDataTask* task;
48
task = [session dataTaskWithRequest:request
49
completionHandler: ^ (NSData * data, NSURLResponse * response, NSError * error) {
50
if(!error) {
51
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
52
53
// ***** NOTE the creation of the ACPHttpConnection outlined in step 2.*****
54
ACPHttpConnection* connOverride = [[ACPHttpConnection alloc] initWithResponse:httpResponse data:data];
55
completion(connOverride);
56
} else {
57
completion(nil);
58
}
59
}];
60
[task resume];
61
}
62
63
@end
Copied!
Swift
1
import Foundation
2
import ACPCore
3
4
class SamplePerformerOverrider: NSObject, ACPHttpConnectionPerformer {
5
6
// Modifications here would allow for conditional overriding based on the url/method.
7
// In this example it always returns true to override all network requests.
8
func shouldOverride(_ url: URL, method: String) -> Bool {
9
return true
10
}
11
12
// Network request override with a completion block. The provided parameters should be configured on the network request.
13
func request(_ url: URL, httpCommand command: String, connectPayload payload: String, requestPropertyDict requestProperty: [String : String], connectTimeout: TimeInterval, readTimeout: TimeInterval, completion: @escaping (ACPHttpConnection?) -> Void) {
14
15
// Create the URLSessionConfiguration with the provided timeouts
16
let config = URLSessionConfiguration.ephemeral
17
config.urlCache = nil
18
config.timeoutIntervalForRequest = readTimeout
19
config.timeoutIntervalForResource = connectTimeout
20
21
let session = URLSession(configuration: config)
22
23
// Create an NSURLRequest with the provided request parameters
24
let request = NSMutableURLRequest.init(url: url)
25
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData
26
request.httpMethod = command
27
28
if !payload.isEmpty && "POST" == command.uppercased() {
29
request.httpBody = payload.data(using: .utf8)
30
}
31
32
for property in requestProperty {
33
request.setValue(property.value, forHTTPHeaderField: property.key)
34
}
35
36
// Start the request
37
let task = session.dataTask(with: url, completionHandler: { (data, response, error) in
38
if error == nil {
39
let httpResponse = response as? HTTPURLResponse
40
// create ACPHttpConnection object with the data received and call the completion handler
41
let connectionOverride = ACPHttpConnection(response: httpResponse, data: data)
42
completion(connectionOverride)
43
} else {
44
completion(nil)
45
}
46
})
47
task.resume()
48
}
49
}
Copied!

3: Register your ACPHttpConnectionPerformer with the SDK

This step should occur prior to any other interactions with the AEP SDK. While it's possible to register the network override at any point during the application lifecycle, the override will only function for network requests performed after the registration has taken place.
Example
Objective-C
1
#import "SamplePerformerOverrider.h"
2
#import "ACPCore.h"
3
4
@implementation AppDelegate
5
6
-(BOOL)application:(UIApplication *)application
7
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions {
8
...
9
[ACPNetworkServiceOverrider setHttpConnectionPerformer:[[SamplePerformerOverrider alloc] init]];
10
...
11
[ACPCore start:^{
12
...
13
}];
Copied!
Swift
1
import ACPCore
2
3
@UIApplicationMain
4
class AppDelegate: UIResponder, UIApplicationDelegate {
5
var window: UIWindow?
6
func applicationDidFinishLaunching(_ application: UIApplication) {
7
...
8
ACPNetworkServiceOverrider.setHttpConnectionPerformer(SamplePerformerOverrider())
9
ACPCore.start {
10
...
11
}
Copied!
Last modified 1mo ago
Export as PDF
Copy link