Integrate IOS SDK (Swift)
This is a guide to help developers get up to speed with proxi.cloud iOS SDK. These step-by-step instructions are written for Objective-C developers. For instructions in Swift please click here.
Remember to set-up your application in the panel before integrating the SDK into your app. Go to https://panel.proxi.cloud and generate an API key for your app.
Demo app
Run pod try ProxicloudSDK
in a terminal will open the proxi.cloud demo project.
Select the PCDemoApp
target and run on device.
Alternatively you can access to the repository here.
Quickstart
Cocoapods
The easiest way to integrate the iOS SDK is via CocoaPods. If you’re new to CocoaPods, visit their getting started documentation.
cd your-project-directory
pod init
open -a Xcode Podfile
Once you’ve initialized CocoaPods, just add the proxi.cloud pod to your target:
target 'YourApp' do
pod 'ProxicloudSDK'
end
Now you can install the dependencies in your project:
pod install
open <YourProjectName>.xcworkspace
Using the SDK
Import the ProxicloudSDK
import ProxicloudSDK
import UserNotifications
Setup the PCManager with an API key and a delegate
You can find your API key on the proxi.cloud panel in the “Apps” section.
The proxi.cloud SDK uses an event bus for events dispatching.
During setup, you pass the class instance that will receive the events as the delegate
.
PCManager.shared().setApiKey(kAPIKey, delegate: self)
Before starting the scanner, we need to ask permission to use the Location services.
If you want to receive events while the app is innactive, you need to pass YES
to the requestLocationAuthorization
. If you pass NO
, the app will receive events only when active.
PCManager.shared().requestLocationAuthorization(true)
Important
Be sure to add the
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
andNSLocationWhenInUseUsageDescription
key to your plist file and the corresponding string to explain to the user why the app requires access to location.You need also to enable the Access WiFi Information capability for your app in Xcode.
Now, we can start the SDK preferably in the location authorisation callback
@objc public func onPCEventLocationAuthorization(_ event: PCEventLocationAuthorization) {
if event.locationAuthorization == .authorized {
PCManager.shared().startMonitoring()
}
}
Start receiving push notifications
Add UNUserNotificationCenterDelegate
protocol to AppDelegate and register with neccessary methods.
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
guard error == nil else {
// print error content here
return
}
}
return true
}
...
Action types
SDK support three action types: notification, website and deeplink. You can perform a different action for each of them in AppDelegate
method (switch statement contains sample actions)
// This function will be called if user receive notification in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
var action: String? = nil
var actionType: PCActionType = .Notification
if let userInfo = notification.request.content.userInfo as? [String : Any] {
action = PCEnums.getActionUuid(userInfo)
actionType = PCEnums.getActionType(userInfo)
}
switch actionType {
case .Notification:
let alertController = UIAlertController(title: notification.request.content.title, message: notification.request.content.body, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: { alert in
// report conversion: "notification clicked"
if let action = action {
PCManager.shared().report(.successful, forCampaignAction: action)
}
})
alertController.addAction(okAction)
self.window?.rootViewController?.present(alertController, animated: true)
case .Website, .DeepLink:
guard let stringUrl = notification.request.content.userInfo["url"] as? String,
let url = URL(string: stringUrl) else {
return
}
let alertController = UIAlertController(title: notification.request.content.title, message: notification.request.content.body, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { _ in
// report conversion: "notification clicked"
if let action = action {
PCManager.shared().report(.successful, forCampaignAction: action)
}
UIApplication.shared.open(url)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.present(alertController, animated: true)
}
}
// This function will be called right after user tap on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
var action: String? = nil
var actionType: PCActionType = .Notification
if let userInfo = response.notification.request.content.userInfo as? [String : Any] {
action = PCEnums.getActionUuid(userInfo)
actionType = PCEnums.getActionType(userInfo)
}
switch actionType {
case .Notification:
PCManager.shared().report(.successful, forCampaignAction: action)
case .Website, .DeepLink:
guard let stringUrl = response.notification.request.content.userInfo["url"] as? String,
let url = URL(string: stringUrl) else {
return
}
PCManager.shared().report(.successful, forCampaignAction: action)
UIApplication.shared.open(url)
}
}
IDFA Support
- Add imports
import AdSupport import AppTrackingTransparency
-
Add to info.plist
NSUserTrackingUsageDescription
key and usage-description string, which displays as a system-permission alert request. The usage-description string tells the user why the app is requesting permission to use data for tracking the user or the device. -
Ask for permission and pass the IDFA value to SDK using::
if #available(iOS 14, *) { ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in if status == .authorized { let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString PCManager.shared().setIDFAValue(idfa) } }) } else { if ASIdentifierManager.shared().isAdvertisingTrackingEnabled { let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString PCManager.shared().setIDFAValue(idfa) } }
Reporting custom events
Starting from v2.1.x, the SDK library allows to report custom events which then can be agreggated and analysed in the proxi.cloud platform. To use this functionality simply call the ReportEvent method when the event you wish to report occurs. The SDK will automatically add current timestamp and location (if it can be established) to the event and send it back to the proxi.cloud platform for further aggregation where it can be used to create custom reports and analytics. Below is an example showing how to report particular product view within the app.
PCManager.shared().reportEvent("product_viewed", withValue: "Awesome Product 500g")l
Instant push messages via Firebase Cloud Messaging
-
Add Notification Service Extension to xCode project Details here
-
Create provisioning profile for extensions bundle identifier
- Add new target in Podfile
target 'yourNotificationServiceExtensionName' do pod 'Firebase/Messaging' end
- Install the dependencies in your project:
pod install
- Modify function in created extension
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) if let bestAttemptContent = bestAttemptContent { if let _ = bestAttemptContent.userInfo["eid"] { bestAttemptContent.userInfo["action"] = NSUUID().uuidString bestAttemptContent.userInfo["sender_id"] = "proxi_fcm" } FIRMessagingExtensionHelper().populateNotificationContent(bestAttemptContent, withContentHandler: contentHandler) } }
and add
import Firebase
- Modify User notifications center delegate functions
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){ // add this line PCManager.shared()?.passResponseToPcSdk(for: notification) } func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void){ // add this line PCManager.shared()?.passResponseToPcSdk(for: response.notification) }
Further information
For more information and support contact us at [email protected].
Dependencies
The proxi.cloud SDK 2.x.x requires iOS 10.0 and uses:
- JSONModel version 1.1 for JSON parsing
- UICKeyChainStore version 2.0 for keychain access
- tolo version 1.0.1 for event communication
- objc-geohash version 0.0.2 for geocoding system