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 WebView.
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 WebViews. Webviews do not inherently support deep links; therefore, the Airbridge.click
function must be used to configure the SDK to handle the deep link within the tracking link.
If the input address is a tracking link, the Airbridge.click
function returns true
and calls the onSuccess
function when the app is launched through the address. If there is no internet connection or a different app is launched, the onFailure
function is called.
If the input address is not a tracking link, 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);
}
Make sure to collect the events that occur in the in-app WebView through the Web SDK in a hybrid app setting. When collecting such events through the App SDK, event duplication will occur.
このページは役に立ちましたか?
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);
}