요구사양

1. SDK 설치

문서를 참고하여 BuzzBooster iOS Native SDK를 설치하세요.

Web 프로젝트의 터미널에서 다음을 실행하세요

npm install buzzbooster-js

2. SDK 초기화

BuzzBooster Android SDK를 설치한 후 Android Project에서 appKey 등 필요한 정보를 추가해 SDK를 초기화하세요.

import BuzzBoosterSDK

@main
final class AppDelegateSwift: UIResponder, UIApplicationDelegate {
  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
    ) -> Bool 
  {
    let config = BSTConfig { builder in
      builder.appKey = "APP_KEY"
    }
    BuzzBooster.initialize(with: config)
    return true
  }
}

3. WebView 설정

iOS Native와 Web간 통신을 위해 다음과 같이 구현하세요. Web에서 발생하는 BuzzBooster의 메시지를 WKScriptMessageHandler에서 처리합니다.

import UIKit
import WebKit
import BuzzBoosterSDK

final class WebViewController: UIViewController {
  private lazy var webView: WKWebView = {
    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true
    preferences.javaScriptCanOpenWindowsAutomatically = true
    
    let userContentController = WKUserContentController()
    userContentController.add(self, name: BuzzBoosterWebKit.handlerName)
    userContentController.addUserScript(BuzzBoosterWebKit.userScript)
    
    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences
    configuration.userContentController = userContentController
    
    let webView = WKWebView(
      frame: .zero,
      configuration: configuration
    )

    return webView
  }()
}

extension WebViewController: WKScriptMessageHandler {
  func userContentController(
    _ userContentController: WKUserContentController,
    didReceive message: WKScriptMessage
  ) {
    do {
      try BuzzBoosterWebKit.handle(with: self, message: message)
    } catch {
      Logger.log(error.localizedDescription)
    }
  }
}