Handler

    Handler


    記事の要約

    Shoplive Playerで発生したユーザーUIイベントや状態変更などの通知をクライアントからハンドラ(delegate)関数を介して受け取り、必要な処理をします。


    delegate

    Shoplive iOS SDKのハンドライベントを受け取る関数を宣言し、Shoplive iOS SDKとイベントを接続する propertyです。

    @objc var delegate: ShopLiveSDKDelegate? { get set }

    @objc public protocol ShopLiveSDKDelegate: AnyObject {
        @objc func handleNavigation(with url: URL)
        @objc func handleChangeCampaignStatus(status: String)
        @objc optional func handleChangedPlayerStatus(status: String)
        @objc func handleCampaignInfo(campaignInfo: [String : Any])
    
        @objc func onSetUserName(_ payload: [String : Any])
    
        @objc func handleCommand(_ command: String, with payload: Any?)
        @objc func handleReceivedCommand(_ command: String, with payload: Any?)
    
        @objc func handleError(code: String, message: String)
    
        @objc optional func handleDownloadCoupon(with couponId: String, result: @escaping (ShopLiveCouponResult) -> Void)
        @objc optional func handleCustomAction(with id: String, type: String, payload: Any?, result: @escaping (ShopLiveCustomActionResult) -> Void)
        @objc optional func playerPanGesture(state: UIGestureRecognizer.State, position: CGPoint)
        @objc optional func log(name: String, feature: ShopLiveLog.Feature, campaign: String, payload: [String: Any])
    
    }

    サンプルコード

    class MainViewController: UIViewController {
        ...
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // delegateと現在のクラスを連結します。
            ShopLive.delegate = self
    
        }
        ...
    }
    
    // delegateを接続するクラスはShopLiveSDKDelegate Protocolを実装します。
    extension MainViewController: ShopLiveSDKDelegate {
        func handleNavigation(with url: URL) {
            print("handleNavigation \(url)")
        }
        ...
    }

    handleNavigation(with:)

    Shopliveで商品、バナーなどを選択(タップ)したとき、選択した商品またはバナー情報を伝達します。

    handleNavigation(with url: URL)

    パラメータ

    説明

    url

    商品またはバナー選択時に移動するURL

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleNavigation(with url: URL) {
            print("handleNavigation \(url)")
    
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
        ...
    }

    handleChangeCampaignStatus(status:)

    放送状態が変更された場合、変更された放送状態を伝えます。

    @objc func handleChangeCampaignStatus(status: String)

    パラメータ

    説明

    status

    放送状況


    READY, ONAIR, CLOSED

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleChangeCampaignStatus(status: String) {
            print("handleChangeCampaignStatus \(status)")
    
            switch status {
                case "READY", "ONAIR":
                    break
                case "CLOSED":
                    print("ended campaign")
                    break
            }
        }
        ...
    }

    handleChangedPlayerStatus(status:)

    Shoplive Playerのステータスを受け取ります。

    handleChangedPlayerStatus(status: String)
    

    パラメータ

    説明

    status

    Shoplive Playerのステータス


    CREATED, DESTROYED

    status

    説明

    CREATED

    プレーヤー作成済み

    DESTROYED

    プレーヤー終了

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleChangedPlayerStatus(status: String) {
            print("handleChangedPlayerStatus \(status)")
    
            switch status {
                case "CREATED":
                    break
                case "DESTROYED":
                    break
            }
        }
        ...
    }

    handleCampaignInfo(campaignInfo:)

    現在の放送に関する情報をお伝えします。

    handleCampaignInfo(campaignInfo: [String : Any])

    パラメータ

    説明

    campaignInfo

    現在のキャンペーン情報


    例)title: 放送タイトル

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleCampaignInfo(campaignInfo: [String : Any]) {
    
            campaignInfo.forEach { (key, value) in
                print("campaignInfo key: \(key)  value: \(value)")
            }
    
        }
        ...
    }

    onSetUserName(_ payload:)

    ユーザー名が変更されたときに呼び出されます。

    onSetUserName(_ payload: [String : Any])

    パラメータ

    説明

    payload

    ユーザー情報


    예) userId: ユーザーID

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func onSetUserName(_ payload: [String : Any]) {
            payload.forEach { (key, value) in
                print("onSetUserName key: \(key) value: \(value)")
            }
        }
        ...
    }

    Shoplive Player ステータスの変更

    command
    Shoplive Player 状態が変更されたときに Command を渡します。

    ShopLiveViewTrackEvent

    @objc public enum ShopLiveViewTrackEvent : Int, CaseIterable {
        case viewWillDisAppear
        case viewDidDisAppear
        case pipWillAppear
        case pipDidAppear
        case fullScreenWillAppear
        case fullScreenDidAppear
        
        public var name : String {
            switch self {
            case .viewWillDisAppear:
                return "viewWillDisAppear"
            case .viewDidDisAppear:
                return "viewDidDisAppear"
            case .pipWillAppear:
                return "pipWillAppear"
            case .pipDidAppear:
                return "pipDidAppear"
            case .fullScreenWillAppear:
                return "fullScreenWillAppear"
            case .fullScreenDidAppear:
                return "fullScreenDidAppear"
            }
        }
    }

    パラメータ

    説明

    viewWillDisAppear

    Shoplive Playerが終了状態に変更を開始

    viewDidDisAppear

    Shoplive Playerが終了状態に変更完了

    pipWillAppear

    Shoplive PlayerがPIPモード状態に変更を開始

    pipDidAppear

    Shoplive Player が PIP モード状態に変更完了

    fullScreenWillAppear

    Shoplive Playerがフルスクリーンモード状態に変更を開始

    fullScreenDidAppear

    Shoplive Playerが全画面モード状態に変更完了

    payload

    変更前にShoplive Playerの状態を payload に渡します。

    パラメータ

    説明

    currentStyle

    現在のPresentationStyleのname(String)

    lastStyle

    直前のPresentationStyleのname(String)

    isPreview

    preview か inAppPip かどうか (Optional Bool)

    viewHiddenActionType

    ShopLiveViewHiddenActionType (オプションの文字列)

    ShopLiveViewHiddenActionType

    case

    説明

    onSwipeOut

    Previewをスワイプして終了

    onBtnTapped

    戻るボタンを押して終了

    onClose

    close()関数を呼び出して終了

    onError

    エラーによる終了

    onRestoringPip

    Pipからアプリに戻るとエラーで終了

    onNavigationHandleClose

    ユーザーがnextActionTypeOnHandleNavigationの定義を.CLOSEにしたときに終了

    To be Deprecated

    パラメータ

    説明

    willShopLiveOn

    Shoplive Playerがフルスクリーンに変更を開始

    didShopLiveOn

    Shoplive Playerがフルスクリーンに変更を完了

    willShopLiveOff

    Shoplive PlayerがPIPモードまたは終了状態に変更を開始

    didShopLiveOff

    Shoplive Player が PIP モードまたは終了状態に変更を完了

    payload

    変更前にShoplive Playerの状態を payload に渡します。 

    パラメータ

    説明

    style

    プレゼンテーションスタイルの rawValue (Int)

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleCommand(_ command: String, with payload: Any?) {
            print("handleCommand: \(command)  payload: \(payload)") 
        }
        ...
    }

    handleReceivedCommand(_ command: , with payload:)

    Shoplive iOS SDKのWEBから受信した命令情報を渡します。

    handleReceivedCommand(_ command: String, with payload: Any?)

    パラメータ

    説明

    command

    命令名

    payload

    命令とともに渡されるデータ

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleReceivedCommand(_ command: String, with payload: Any?) {
            switch command {
            case "LOGIN_REQUIRED":
                let alert = UIAlertController(title: command, message: "alert.login.required.description".localized(), preferredStyle: .alert)
                alert.addAction(.init(title: "alert.msg.ok".localized(), style: .default, handler: { _ in
                    /*ログイン
                        1.ログイン画面に移動
                        2.ログインが成功したら、認証ユーザーアカウントを連動してショップライブプレーヤーを再呼び出しします。
                     */ログイン
                    ShopLive.startPictureInPicture()
                    let login = LoginViewController()
                    login.delegate = self
                    self.navigationController?.pushViewController(login, animated: true)
                }))
                alert.addAction(.init(title: "alert.msg.no".localized(), style: .default, handler: { _ in
                    alert.dismiss(animated: true)
                }))
                ShopLive.viewController?.present(alert, animated: true, completion: nil)
                break
            case "CLICK_PRODUCT_CART":
              break
            default:
                break
            }
          
        }
        ...
    }

    Coupon

    クーポン処理が完了したときにクーポンが有効かどうかを設定するための状態です。

    @objc public enum ShopLiveResultStatus: Int, CaseIterable {
        case SHOW // クーポン再アクティブ化
        case HIDE // クーポン消失
        case KEEP // 状態を維持する
    
        public var name: String {
            switch self {
            case .SHOW:
                return "SHOW"
            case .HIDE:
                return "HIDE"
            case .KEEP:
                return "KEEP"
            }
        }
    }

    クーポン処理が完了したときに、通知メッセージがあれば表示されるタイプを設定します。

    @objc public enum ShopLiveResultAlertType: Int, CaseIterable {
        case ALERT // アラート
        case TOAST // トースト
    
        public var name: String {
            switch self {
            case .ALERT:
                return "ALERT"
            case .TOAST:
                return "TOAST"
            }
        }
    }

    @objc public class ShopLiveCouponResult: NSObject {
        var success: Bool // クーポン処理の成功/失敗の有無
        var coupon: String = "" // クーポン ID
        var message: String? // 通知メッセージ
        var couponStatus: ShopLiveResultStatus // 完了後のクーポンステータス
        var alertType: ShopLiveResultAlertType // 通知メッセージ出力タイプ
    
        public init(couponId: String, success: Bool, message: String?, status: ShopLiveResultStatus, alertType: ShopLiveResultAlertType) {
            self.coupon = couponId
            self.success = success
            self.message = message
            self.couponStatus = status
            self.alertType = alertType
        }
    }

    @objc public class ShopLiveCustomActionResult: NSObject {
        var success: Bool // カスタムアクション処理の成功/失敗
        var id: String = "" // カスタムアクションの ID   
        var message: String? // 通知メッセージ
        var couponStatus: ShopLiveResultStatus // 完了後のカスタムアクション状態
        var alertType: ShopLiveResultAlertType // 通知メッセージの出力タイプ
    
        public init(id: String, success: Bool, message: String?, status: ShopLiveResultStatus, alertType: ShopLiveResultAlertType) {
            self.id = id
            self.success = success
            self.message = message
            self.couponStatus = status
            self.alertType = alertType
        }
    }
    

    handleDownloadCoupon(with: result:)

    Shopliveでクーポンを選択(タップ)したときにクライアントにクーポン情報を渡します。クライアントのクーポン処理結果を Shoplive iOS SDK に返す result callback を使用して、Shoplive Player でのクーポンステータスを設定します。

    handleDownloadCoupon(with couponId: String, result: @escaping (ShopLiveCouponResult) -> Void)

    パラメータ

    説明

    couponId

    クーポンID

    result

    結果値をShoplive iOS SDKに渡すresult callback

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleDownloadCoupon(with couponId: String, result: @escaping (ShopLiveCouponResult) -> Void) {
            print("handleDownloadCoupon: \(couponId)")
    
            // クーポン処理完了時に呼び出し (処理結果)
            // 成功
            let couponResult = ShopLiveCouponResult(couponId: couponId, success: true, message: "クーポンのダウンロードが成功しました。", status: .SHOW, alertType: .ALERT)
            // 失敗
            let couponResult = ShopLiveCouponResult(couponId: couponId, success: false, message: "クーポンのダウンロードに失敗しました。", status: .HIDE, alertType: .TOAST)
            result(couponResult)
        }
        ...
    }

    handleCustomAction(with id:, type:, payload:, result:)

    ポップアップでクリックイベントを custom で指定し、ポップアップを選択(タップ)した時、ポップアップ情報を伝達します。ポップアップの id, type (COUPON, BANNER, NOTICE), payloadを渡します。

    handleCustomAction(with id: String, type: String, payload: Any?, result: @escaping (ShopLiveCustomActionResult) -> Void)

    パラメータ

    説明

    id

    ポップアップID

    type

    ポップアップタイプ



    - COUPON, BANNER, NOTICE

    payload

    カスタムデータ

    result

    結果値を Shoplive iOS SDKに渡す result callback

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleCustomAction(with id: String, type: String, payload: Any?, result: @escaping (ShopLiveCustomActionResult) -> Void) {
            print("handleCustomAction \(id) \(type) \(payload.debugDescription)")
    
            // ポップアップ処理が完了すると呼び出される(処理結果)
            // 成功
            let customActionResult = ShopLiveCustomActionResult(id: id, success: true, message: "Coupon download was successful.", status: .SHOW, alertType: .ALERT)
    
            // 失敗
            let customActionResult = ShopLiveCustomActionResult(id: id, success: false, message: "Coupon download was failed.", status: .HIDE, alertType: .TOAST)
    
            result(customActionResult)
        }
        ...
    }

    handleError(code:, message:)

    放送前または放送中に発生したエラー状況に関するメッセージを配信します。

    handleError(code: String, message: String)

    パラメータ

    説明

    code

    エラーコード

    message

    エラーメッセージ

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func handleError(code: String, message: String) {
            print("handleError \(code) \(message)")
        }
        ...
    }

    playerPanGesture(state: , position: )

    PIPモードでウィンドウ位置を動かすと、ウィンドウの座標とジェスチャーイベントの状態を渡します。

    playerPanGesture(state: UIGestureRecognizer.State, position: CGPoint)

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
    	func playerPanGesture(state: UIGestureRecognizer.State, position: CGPoint) {
        	print("gesture state \(state) position \(position)")
    	}
      ...
    }

    log(name: , feature: , campaign: , payload: )

    Shoplive Playerのイベントログを転送します。この情報は、Google Analyticsなどの分析ツールで利用できます。

    log(name: String, feature: ShopLiveLog.Feature, campaign: String, payload: [String: Any])

    ShopLiveLog

    Shoplive Playerのイベントログを転送します。この情報は、Google Analyticsなどの分析ツールで利用できます。

    @objc public class ShopLiveLog: NSObject {
        @objc public enum Feature: Int, CaseIterable {
            case CLICK, SHOW, ACTION
            
            public var name: String {
                switch self {
                case .CLICK:
                    return "click"
                case .ACTION:
                    return "action"
                case .SHOW:
                    return "show"
                }
            }
            
            static func featureFrom(type: String) -> Feature? {
                return Feature.allCases.filter({$0.name == type}).first
            }
        }
        
        public var name: String
        public var campaign: String
        public var feature: Feature
        public var payload: [String: Any] = [:]
        
        public init(name: String, feature: Feature, campaign: String, payload: [String : Any]) {
            self.name = name
            self.feature = feature
            self.campaign = campaign
            self.payload = payload
        }
    }

    サンプルコード

    extension MainViewController: ShopLiveSDKDelegate {
        func log(name: String, feature: ShopLiveLog.Feature, campaign: String, payload: [String : Any]) {
            let eventLog = ShopLiveLog(name: name, feature: feature, campaign: campaign, payload: payload)
            print("eventLog \(eventLog.name)")
        }
    }