Log Upload in iOS Application Extensions¶
Why Upload Logs¶
An app writes logs while it runs. Uploading those logs to SAP Mobile Services lets you see what happened on real users' devices. This helps you diagnose bugs, understand problems that end users report, and get this insight without requiring access to the physical device.
An iOS application extension runs in its own process, separate from the main app. From version 26.11 the SAP BTP SDK for iOS extends the log-upload feature to iOS application extensions. Uploading those logs lets you diagnose problems that happen inside the extension, which the main app's logs do not capture.
The extension does not fetch its own log settings. Instead it reuses the same log settings the main app already received from SAP Mobile Services. The main app shares these settings through a shared keychain store, and the extension reads and applies them automatically before uploading. If no shared settings are found, the extension uses default settings as a safe fallback.
Supported Application Extensions¶
Version 26.11 of the SAP BTP SDK for iOS supports log upload from the below mentioned extensions.
- Widget (WidgetKit)
- App Intents extension
- Notification Service extension
- Notification Content extension
- watchOS app
Prerequisites¶
-
Enable Anonymous Access In the mobile services cockpit, set up Anonymous Access for your application and use the generated API key in the extension code (see the example below).
-
Share Log Settings Enable Keychain Sharing on both the app target and the extension target, and make sure the same access group is the first entry in each target's list. If you skip this step, the extension still works, but it uses default log settings instead of the main app's settings.
Code Example¶
First, set up the log uploader. Do this when your app extension starts, so the uploader is ready early.
import SAPCommon
import SAPFoundation
private let logger = Logger.shared(named: "MyAppExtensionLogger")
do {
try SAPcpmsLogUploader.attachToRootLogger()
Logger.root.logLevel = .debug
} catch {
print("Error in attaching to root logger: \(error)")
}
Next, write log messages in your code the same way you normally do.
logger.debug("this is a debug log from app extension")
To upload the logs, add the following code inside your application extension:
import SAPFoundation
import SAPCommon
func uploadExtensionLogs() async {
// An instance of SAPURLSession for network call.
let sapURLSession = SAPURLSession()
// Mobile Services connection parameters.
let appId = "<your-application-id>"
let settingsParameters = SAPcpmsSettingsParameters(backendURL: URL(string: "<your-mobile-services-host>")!, applicationID: appId)
// Set up API key authentication.
let apiKey = "<api-key>"
let config = APIKeyAuthenticationConfig(apikeys: [apiKey], isAPIKeyAccessOnly: true)
let discovery = DefaultAPIKeyCredentialDiscovery(using: config)
let apiKeyObserver = APIKeyAuthenticationObserver(apikeyCredentialDiscovery: discovery, isAPIKeyAccessOnly: true)
// Register the required observers on the session.
sapURLSession.register(apiKeyObserver)
sapURLSession.register(SAPcpmsObserver(applicationID: appId))
// Upload the extension logs.
do {
try await SAPcpmsLogUploader.uploadExtensionLogs(sapURLSession: sapURLSession, settingsParameters: settingsParameters)
} catch {
// handle error
}
}
Security Note¶
As a good practice, do not store the API key as plain text in your code or app bundle. The SAP BTP SDK for iOS recommends using the Obfuscator class from SAPCommon to turn the key into obfuscated bytes (of type [UInt8]), and using those bytes in your code.
This is a one-time process:
- Paste the API key (from the mobile services cockpit) in your code temporarily.
- Run the code once to obfuscate the key string into
keyBytes(of type[UInt8]). - Use
keyBytesin your code, reading the key back withObfuscator().deobfuscate(keyBytes). - Remove the plain API key and the obfuscation code from your project.
Here is the one-time code to obfuscate the key and build the authentication config:
import SAPCommon
let obfuscator = Obfuscator()
let keyBytes = obfuscator.obfuscate("<api-key-from-mobile-services-cockpit>")
let apiKey = obfuscator.deobfuscate(keyBytes)
let apiKeyConfig = APIKeyAuthenticationConfig(apikeys: [apiKey], isAPIKeyAccessOnly: false, allowAnonymousAccessFlag: true)