Note
You can configure the iOS SDK to handle Airbridge-related tasks occurring within the in-app website of a hybrid app without modifying the website's code.
The iOS SDK can handle the commands related to event transmission, device settings, and user settings carried out by the Web SDK. Configure the Airbridge.setWebInterface
function before displaying the website on the web view.
YOUR_WEB_TOKEN
is the Web SDK token. You can find it on the [Settings]>[Tokens] page in the Airbridge dashboard.
import UIKit
import WebKit
import Airbridge
class ViewController: UIViewController, WKNavigationDelegate {
let webView = {
let configuration = WKWebViewConfiguration()
let controller = WKUserContentController()
Airbridge.setWebInterface(
controller: controller,
webToken: "YOUR_WEB_SDK_TOKEN"
)
configuration.userContentController = controller
return WKWebView(frame: .null, configuration: configuration)
}()
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
self.view = webView
webView.load(URLRequest(url: URL(string: "YOUR_WEBSITE_URL")!))
}
}
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
...
#import "ViewController.h"
#import <WebKit/WebKit.h>
#import <Airbridge/Airbridge.h>
@interface ViewController () <WKNavigationDelegate>
@end
@implementation ViewController {
WKWebView* webView;
}
- (instancetype)init
{
self = [super init];
if (self) {
WKWebViewConfiguration* configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController* controller = [[WKUserContentController alloc] init];
[Airbridge setWebInterfaceWithController:controller webToken:@"YOUR_WEB_SDK_TOKEN"];
configuration.userContentController = controller;
webView = [[WKWebView alloc] initWithFrame:CGRectNull configuration:configuration];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
webView.navigationDelegate = self;
self.view = webView;
[webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"YOUR_WEBSITE_URL"]]];
}
@end
Add the Airbridge.click function to enable deep links in the web view. Therefore, the Airbridge.click
function must be configured so that the SDK can handle the deep link embedded in the tracking link.
The Airbridge.click
function returns true
when the input address is a tracking link. When the app is launched through that address, the onSuccess
function is called. If there is no internet connection or a different app is launched, the onFailure
function is called.
When the input address is not a tracking link, the Airbridge.click
function returns false
, and no further action is executed.
Implementing webView(_,decidePolicyFor:,decisionHandler:)
in WKWebView
allows you to determine whether to continue or cancel loading a specific URL after it is detected.
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy
) -> Void) {
if let url = navigationAction.request.url {
let isHandled = Airbridge.click(trackingLink: url) {
// when url is tracking link and succeed
// do nothing
} onFailure: { error in
// when url is tracking link and failed
// example: url is another app's tracking link, internet is not connected
webView.load(URLRequest(url: url))
}
if isHandled {
// when url is tracking link
decisionHandler(.cancel)
return
}
}
// when url is not tracking link
decisionHandler(.allow)
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL* url = navigationAction.request.URL;
if (url != nil) {
BOOL isHandled = [Airbridge clickWithTrackingLink:url onSuccess:^{
// when url is tracking link and succeed
// do nothing
} onFailure:^(NSError* error) {
// when url is tracking link and failed
// example: url is another app's tracking link, internet is not connected
[webView loadRequest:[[NSURLRequest alloc] initWithURL:url]];
}];
if (isHandled) {
// when url is tracking link
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
// when url is not tracking link
decisionHandler(WKNavigationActionPolicyAllow);
}
Note that with the hybrid app setup, events that occur in the in-app web view must be collected through the Web SDK only. If collected through the App SDK as well, event duplication will occur. If your in-app web view uses a live mobile website, utilize only the Web SDK for event collection.
Was this page helpful?