Swift SDK Integration
Last updated
Last updated
This SDK is developed in Swift and supports integration with Swift Package Manager (SPM). For new projects, we recommend integrating the Netmera Swift SDK to optimize functionality and streamline development.
Quick Steps Guidelines
Step 1: Create an Apple Push Notification Certificate
Generate your APNS Certificate and add the certificate to the Netmera panel under Developer > Push Backend.
Step 2: Integrate SDK into Your Project
Add Netmera to your Podfile
and run the pod install
command in your terminal.
Step 3: Setup Netmera
Import the Netmera framework in your AppDelegate.swift
file and initialize Netmera.
Step 4: Request Push Notification Authorization
Add NetmeraNotification
to your Podfile
and request push notification authorization from the user.
Step 5: Configure your Xcode Settings for Enabling Push Notifications
Enable push notifications by navigating to Signing & Capabilities > Capability > Push Notifications in Xcode.
Below is the Onboarding Checklist for Swift and Flutter. Follow each item in the list to ensure all essential steps in your Netmera onboarding process are completed successfully.
To enable push notifications, Netmera uses APNS (Apple Push Notification Service). Follow these steps to create and upload the required certificate:
Log in to developer.apple.com with your Apple Developer account.
Generate an Apple Push Notification Certificate for your app.
Export the certificate using Keychain Access on your Mac.
Upload the exported certificate to the Netmera panel by navigating to Developer > Push Backends > iOS in the left menu.
Bundle IDs should match on your project, certificate and panel:
Ensure that the certificate you upload to the panel matches the bundle ID of your project. Additionally, set your project's bundle ID in the panel to ensure consistency. The bundle ID of your project, the certificate, and the one specified in the panel must all align.
Certificate types:
For apps downloaded from the App Store or tested via TestFlight, the certificate type should be set to "prod".
For apps built directly from Xcode, the certificate type must be set to "dev".
If you have problems sending push notifications when you build from Xcode, verify the certificate type on the Developer > Push Backends > iOS page in Panel.
Using the APNS .p8
certificate is highly recommended for the following reasons:
The .p8 certificate is mandatory for enabling features such as Live Activities.
Unlike .p12 certificates, a single .p8 certificate can manage push notifications for multiple applications.
The .p8 certificate does not require yearly renewal.
The .p8 certificate is easier to generate via the Apple Developer Portal and only needs to be downloaded once.
You can integrate the Netmera SDK into your project using either Cocoapods or Swift Package Manager (SPM). Follow the instructions for your chosen method.
Open your project's Podfile
and add the relevant lines for the Netmera features you need:
Run pod install
command in your terminal.
Open Project Settings and navigate to Package Dependencies. Click the "+" button to add a new dependency.
Enter the following repository URL in the search bar. The package details will load automatically.
Click "Add Package" to complete the integration.
In your AppDelegate.swift file, import the necessary Netmera modules based on the features you plan to use:
Notes for SwiftUI projects:
For SwiftUI projects, the AppDelegate.swift file is not included by default. Instead, SwiftUI uses an App
struct marked with @main
to manage the application lifecycle.
You can adapt your implementation by creating an AppDelegate
class and integrating it into the App
struct if needed.
Add the Netmera-Config.plist
file to your project.
Replace the placeholders ({API_KEY}
, {AppGroupName}
, etc.) in the following code snippet with your actual values:
To obtain your SDK API Key, follow these steps:
Go to the Netmera Panel.
Navigate to Developer > API > SDK API Key.
Copy your SDK API Key from this section.
Use this key to replace the {API_KEY}
placeholder in either the Netmera-Config.plist
or your programmatic setup.
Additional Configuration for On-Premises Customers
If you are using Netmera in an on-premises environment, include the following in your Netmera-Config.plist
:
Call the Netmera initialization method in your application(_:didFinishLaunchingWithOptions:)
method:
1. Add NetmeraNotification to Your Podfile
To enable push notifications, add the following line to your Podfile and install it for your app target:
Run the following command in your terminal to install:
2. Request Push Notification Authorization
Request push notification permission from the user by calling the following method in an appropriate place in your code:
Important Note on Interactive Push Buttons
If your application is using push notifications from a provider like Apple, ensure that you call this method to allow the Netmera SDK to handle interactive push notification buttons correctly. Failing to do so may cause issues with the interaction buttons not being properly managed by the SDK.
1. Enable Push Notifications in Xcode
To enable push notifications for your app, follow these steps:
Open your Xcode project.
Navigate to Signing & Capabilities.
Under Capability, select Push Notifications.
2. Implement UNUserNotificationCenterDelegate Methods
In your AppDelegate
, you need to implement the necessary methods for handling push notifications. Add the following code to your didFinishLaunchingWithOptions
method:
Then, implement the following delegate methods to handle the push notifications:
If you want to configure multiple environments for your app (e.g., testing and production), follow the steps below:
Step 1: Copy Netmera-Config.plist Files
Copy the Netmera-Config.plist file for the development environment into the Dev directory.
Copy the Netmera-Config.plist file for the production environment into the Prod directory.
When adding these files to your Xcode project, uncheck Copy items if needed and ensure that no targets are selected under Add to targets.
Step 2: Add Run Script Phase
In Xcode, select your app target.
Go to the Build Phases tab and add a New Run Script Phase.
Name the new phase something like "Setup Netmera Environment Netmera-Config.plist".
Position this phase before the Copy Bundle Resources step.
Step 3: Implement Shell Script for Config File Selection
In the new Run Script Phase, implement the following shell script to copy the correct Netmera-Config.plist based on the build configuration (Debug or Release):
The following script checks if the development and production versions of Netmera-Config.plist exist in their respective directories, and based on the build configuration (Debug or Release), it will copy the correct configuration file into the app bundle.
The Swift SDK integration is complete, and your devices are now ready to receive the following types of push notifications from the Netmera Dashboard:
Standard Push Notifications
Interactive Push Notifications
Widgets
Push Notifications with Deeplinks