네이티브 Side 연동(iOS)
1. Handler 생성
AdPopcornSSPWKScriptMessageHandler 생성 후, apssp
이름으로 scriptMessageHandler를 등록해 줍니다.
AdPopcornSSP SDK의 경우 Objective-C로 개발된 framework이므로, swift 사용 시 [Bridge-Header] 를 작성한 뒤 사용하시기 바랍니다.
#import "AdPopcornSSPWKScriptMessageHandler.h"
@interface AdPopcornSSPController() <WKNavigationDelegate, WKUIDelegate>
{
}
@implementation AdPopcornSSPController
- (void)viewDidLoad {
webViewConfiguration = [[WKWebViewConfiguration alloc] init];
wkContentController = [[WKUserContentController alloc] init];
AdPopcornSSPWKScriptMessageHandler *scriptMessageHandler = [[AdPopcornSSPWKScriptMessageHandler alloc] initWithDelegate:nil];
[wkContentController addScriptMessageHandler:scriptMessageHandler name:@"apssp"];
[webViewConfiguration setUserContentController:wkContentController];
webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) configuration:webViewConfiguration];
scriptMessageHandler.webView = webView;
webView.navigationDelegate = self;
webView.uiDelegate = self;
}
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate{
var webView : WKWebView!
var webViewConfiguration : WKWebViewConfiguration!
var wkContentController : WKUserContentController!
var adpopcornSSPWKScriptMessageHandler : AdPopcornSSPWKScriptMessageHandler!
}
override func viewDidLoad() {
super.viewDidLoad()
webViewConfiguration = WKWebViewConfiguration.init()
wkContentController = WKUserContentController.init()
adpopcornSSPWKScriptMessageHandler = AdPopcornSSPWKScriptMessageHandler(delegate: nil)
wkContentController.add(adpopcornSSPWKScriptMessageHandler, name: "apssp")
webViewConfiguration.userContentController = wkContentController
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), configuration: webViewConfiguration)
adpopcornSSPWKScriptMessageHandler.webView = webView
webView.navigationDelegate = self
webView.uiDelegate = self
}
2. webView 객체 설정
생성된 scriptMessageHandler
에 webView 객체를 설정해 줍니다.
#import "AdPopcornSSPWKScriptMessageHandler.h"
@interface AdPopcornSSPController() <WKNavigationDelegate, WKUIDelegate>
{
}
@implementation AdPopcornSSPController
- (void)viewDidLoad {
webViewConfiguration = [[WKWebViewConfiguration alloc] init];
wkContentController = [[WKUserContentController alloc] init];
AdPopcornSSPWKScriptMessageHandler *scriptMessageHandler = [[AdPopcornSSPWKScriptMessageHandler alloc] initWithDelegate:nil];
[wkContentController addScriptMessageHandler:scriptMessageHandler name:@"apssp"];
[webViewConfiguration setUserContentController:wkContentController];
webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) configuration:webViewConfiguration];
scriptMessageHandler.webView = webView;
webView.navigationDelegate = self;
webView.uiDelegate = self;
}
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate{
var webView : WKWebView!
var webViewConfiguration : WKWebViewConfiguration!
var wkContentController : WKUserContentController!
var adpopcornSSPWKScriptMessageHandler : AdPopcornSSPWKScriptMessageHandler!
}
override func viewDidLoad() {
super.viewDidLoad()
webViewConfiguration = WKWebViewConfiguration.init()
wkContentController = WKUserContentController.init()
adpopcornSSPWKScriptMessageHandler = AdPopcornSSPWKScriptMessageHandler(delegate: nil)
wkContentController.add(adpopcornSSPWKScriptMessageHandler, name: "apssp")
webViewConfiguration.userContentController = wkContentController
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), configuration: webViewConfiguration)
adpopcornSSPWKScriptMessageHandler.webView = webView
webView.navigationDelegate = self
webView.uiDelegate = self
}
3. 델리게이트 설정
#import "AdPopcornSSPWKScriptMessageHandler.h"
@interface AdPopcornSSPController() <WKNavigationDelegate>
{
}
@implementation AdPopcornSSPController
- (void)viewDidLoad {
webViewConfiguration = [[WKWebViewConfiguration alloc] init];
wkContentController = [[WKUserContentController alloc] init];
AdPopcornSSPWKScriptMessageHandler *scriptMessageHandler = [[AdPopcornSSPWKScriptMessageHandler alloc] initWithDelegate:nil];
[wkContentController addScriptMessageHandler:scriptMessageHandler name:@"apssp"];
[webViewConfiguration setUserContentController:wkContentController];
webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) configuration:webViewConfiguration];
scriptMessageHandler.webView = webView;
webView.navigationDelegate = self;
webView.uiDelegate = self;
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
NSString *requestString = navigationAction.request.URL.absoluteString;
if(navigationAction.navigationType == UIWebViewNavigationTypeLinkClicked)
{
decisionHandler(WKNavigationActionPolicyCancel);
NSURL *requestURL = [NSURL URLWithString:requestString];
if(@available(iOS 10, *))
{
[[UIApplication sharedApplication] openURL:requestURL options:@{} completionHandler:^(BOOL success) {
}];
}
return;
}
decisionHandler(WKNavigationActionPolicyAllow);
}
#pragma mark WKUIDelegate
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSString *requestString = navigationAction.request.URL.absoluteString;
NSLog(@"createWebViewWithConfiguration requestURL : %@", requestString);
NSLog(@"navigationAction.navigationType : %ld", (long)navigationAction.navigationType);
if(navigationAction.navigationType == WKNavigationTypeLinkActivated ||
navigationAction.navigationType == WKNavigationTypeOther)
{
NSURL *requestURL = [NSURL URLWithString:requestString];
if(@available(iOS 10, *))
{
[[UIApplication sharedApplication] openURL:requestURL options:@{} completionHandler:^(BOOL success) {
}];
}
else
{
[[UIApplication sharedApplication] openURL:requestURL];
}
}
return nil;
}
class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate{
var webView : WKWebView!
var webViewConfiguration : WKWebViewConfiguration!
var wkContentController : WKUserContentController!
var adpopcornSSPWKScriptMessageHandler : AdPopcornSSPWKScriptMessageHandler!
}
override func viewDidLoad() {
super.viewDidLoad()
webViewConfiguration = WKWebViewConfiguration.init()
wkContentController = WKUserContentController.init()
adpopcornSSPWKScriptMessageHandler = AdPopcornSSPWKScriptMessageHandler(delegate: nil)
wkContentController.add(adpopcornSSPWKScriptMessageHandler, name: "apssp")
webViewConfiguration.userContentController = wkContentController
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), configuration: webViewConfiguration)
adpopcornSSPWKScriptMessageHandler.webView = webView
webView.navigationDelegate = self
webView.uiDelegate = self
}
//pragma mark - WKNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
var requestString = navigationAction.request.url?.absoluteString
if(navigationAction.navigationType == WKNavigationType.linkActivated)
{
decisionHandler(.cancel);
if let url = URL(string: requestString!) {
UIApplication.shared.open(url, options:[:], completionHandler: nil)
}
}
decisionHandler(.allow);
}
//pragma mark WKUIDelegate
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
var requestString = navigationAction.request.url?.absoluteString
if(navigationAction.navigationType == WKNavigationType.linkActivated
|| navigationAction.navigationType == WKNavigationType.other)
{
if let url = URL(string: requestString!) {
UIApplication.shared.open(url, options:[:], completionHandler: nil)
}
}
return nil
}
위 3가지 연동을 hybrid webview에 설정해주면 네이티브와 통신을 위한 준비가 마무리됩니다.
이후에는 [WebPage Side 연동] 를 확인해 웹 페이지 내에 배너, 네이티브 영역을 지정하여 광고를 송출할 준비를 진행합니다.
Last updated