Control how your app responds to URLs in push notifications and inbox messages by implementing the URL handling protocol. The SDK passes URLs from CloudPages push notifications, OpenDirect push notifications, and inbox messages to your app, but doesn’t automatically present them. Implement the URLHandlingDelegate protocol to open URLs in Safari, display them in your app using SFSafariViewController, or trigger custom app logic based on URL parameters.
To implement URL handling, follow these steps.
Implement the URLHandlingDelegate (v8.x) or MarketingCloudSDKURLHandlingDelegate (v7.x) protocol in your app.
Use setURLHandlingDelegate (v8.x) or sfmc_setURLHandlingDelegate: method to set a delegate for the protocol.
Implement the protocol method sfmc_handleURL:type:.
When the SDK receives an OpenDirect or CloudPages push notification, it passes a NSURL value to sfmc_handleURL:type:. This value contains the push notification or inbox message, and includes the URL. A type value also reflects the source of the URL, which is either SFMCURLTypeCloudPages or SFMCURLTypeOpenDirect.
If the development language is Swift, make the class that implements URLHandlingDelegate in version 8 or later or MarketingCloudSDKURLHandlingDelegate in version 7 Objective-C compatible. To make the delegate compatible, prefix the class with @objc and extend NSObject, as shown in these examples.
Important
This code example shows how to handle URLs using version 10 or higher of the SDK.
1// Framework import2// The SFMCSDK module is included in the dependency packages for the SDK. Don't install it3// separately or list it in your podfile.4import SFMCSDK5import MarketingCloudSDK6import PushFeatureSDK78// Make sure your class adopts the protocol9@objc class MyClass: NSObject, URLHandlingDelegate1011...12// Set the delegate somewhere in your application code after configuring the SDK13 PushFeature.requestSdk { pushFeature in14 pushFeature?.setURLHandlingDelegate(self)15}16...1718// EXAMPLE IMPLEMENTATIONS19// Implement the protocol method and have iOS handle the URL itself20func sfmc_handleURL(_ url: URL, type: String){21 if UIApplication.shared.canOpenURL(url) == true{22 if #available(iOS 10.0, *){23 UIApplication.shared.open(url, options: [:], completionHandler: { success in24 if success {25 print("url (url) opened successfully")26}else{27 print("url (url) could not be opened")28}29})30}else{31 if UIApplication.shared.openURL(url) == true{32 print("URL (url) opened successfully")33}else{34 print("Couldn't open URL (url)")35}36}37}38}3940// Implement the protocol method and use SFSafariViewController to present the41// URL within your app42func sfmc_handleURL(_ url: URL, type: String){43 let safariViewController = SFSafariViewController(url: url)44 window?.topViewController()?.present(safariViewController, animated: true){}45}4647// Implement the protocol method and take app-specific actions based on the URL48func sfmc_handleURL(_ url: URL, type: String){49 let queryItems = URLComponents(string: url.absoluteString)?.queryItems50 for item: URLQueryItem? in queryItems ?? []{51 // do something in your application based on the parameters in the URL52}53}
For version 8.1 of the SDK, use this code.
1// Framework import2// The SFMCSDK module is included in the dependency packages for the SDK. Don't install it3// separately or list it in your podfile.4import SFMCSDK5import MarketingCloudSDK67// Make sure your class adopts the protocol8@objc class MyClass: NSObject, URLHandlingDelegate910...11// Set the delegate somewhere in your application code after configuring the SDK12SFMCSdk.requestPushSdk { mp in13 mp.setURLHandlingDelegate(self)14}15...1617// EXAMPLE IMPLEMENTATIONS18// Implement the protocol method and have iOS handle the URL itself19func sfmc_handleURL(_ url: URL, type: String){20 if UIApplication.shared.canOpenURL(url) == true{21 if #available(iOS 10.0, *){22 UIApplication.shared.open(url, options: [:], completionHandler: { success in23 if success {24 print("url (url) opened successfully")25}else{26 print("url (url) could not be opened")27}28})29}else{30 if UIApplication.shared.openURL(url) == true{31 print("url (url) opened successfully")32}else{33 print("url (url) could not be opened")34}35}36}37}3839// Implement the protocol method and use SFSafariViewController to present the40// URL within your app41func sfmc_handleURL(_ url: URL, type: String){42 let safariViewController = SFSafariViewController(url: url)43 window?.topViewController()?.present(safariViewController, animated: true){44}45}4647// Implement the protocol method and take app-specific actions based on the URL48func sfmc_handleURL(_ url: URL, type: String){49 let queryItems = URLComponents(string: url.absoluteString)?.queryItems50 for item: URLQueryItem? in queryItems ?? []{51 // do something interesting in your application based on the parameters in the URL52}53}
For version 8.0 of the SDK, use this code.
1// Framework import2// The SFMCSDK module is included in the dependency packages for the SDK. Don't install it3// separately or list it in your podfile.4import SFMCSDK5import MarketingCloudSDK67// Make sure your class adopts the protocol8@objc class MyClass: NSObject, URLHandlingDelegate910...11// Set the delegate somewhere in your application code after configuring the SDK12// and confirming that the SDK is operational13 mp.setURLHandlingDelegate(self)14...15// EXAMPLE IMPLEMENTATIONS16// Implement the protocol method and have iOS handle the URL itself17func sfmc_handleURL(_ url: URL, type: String){18 if UIApplication.shared.canOpenURL(url) == true{19 if #available(iOS 10.0, *){20 UIApplication.shared.open(url, options: [:], completionHandler: { success in21 if success {22 print("url (url) opened successfully")23}else{24 print("url (url) could not be opened")25}26})27}else{28 if UIApplication.shared.openURL(url) == true{29 print("url (url) opened successfully")30}else{31 print("url (url) could not be opened")32}33}34}35}3637// Implement the protocol method and use SFSafariViewController to present the38// URL within your app39func sfmc_handleURL(_ url: URL, type: String){40 let safariViewController = SFSafariViewController(url: url)41 window?.topViewController()?.present(safariViewController, animated: true){42}43}4445// Implement the protocol method and take app-specific actions based on the URL46func sfmc_handleURL(_ url: URL, type: String){47 let queryItems = URLComponents(string: url.absoluteString)?.queryItems48 for item: URLQueryItem? in queryItems ?? []{49 // do something in the app based on the parameters in the URL50}51}
For version 7 of the SDK, use this code.
1// Framework import2import MarketingCloudSDK34// Make sure your class adopts the protocol5@objc class MyClass: NSObject, MarketingCloudSDKURLHandlingDelegate67...8// Set the delegate somewhere in your application code after configuring the SDK9MarketingCloudSDK.sharedInstance().sfmc_setURLHandlingDelegate(self)10...11// EXAMPLE IMPLEMENTATIONS12// Implement the protocol method and have iOS handle the URL itself13func sfmc_handleURL(_ url: URL, type: String){14 if UIApplication.shared.canOpenURL(url) == true{15 if #available(iOS 10.0, *){16 UIApplication.shared.open(url, options: [:], completionHandler: { success in17 if success {18 print("url (url) opened successfully")19}else{20 print("url (url) could not be opened")21}22})23}else{24 if UIApplication.shared.openURL(url) == true{25 print("url (url) opened successfully")26}else{27 print("url (url) could not be opened")28}29}30}31}3233// Implement the protocol method and use SFSafariViewController to present the34// URL within your app35func sfmc_handleURL(_ url: URL, type: String){36 let safariViewController = SFSafariViewController(url: url)37 window?.topViewController()?.present(safariViewController, animated: true){38}39}4041// Implement the protocol method and take app-specific actions based on the URL42func sfmc_handleURL(_ url: URL, type: String){43 let queryItems = URLComponents(string: url.absoluteString)?.queryItems44 for item: URLQueryItem? in queryItems ?? []{45 // do something in the app based on the parameters in the URL46}47}