diff --git a/DailymotionPlayerSDK.podspec b/DailymotionPlayerSDK.podspec index 04fcf31..4b0a2fc 100644 --- a/DailymotionPlayerSDK.podspec +++ b/DailymotionPlayerSDK.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'DailymotionPlayerSDK' - s.version = '3.4.10' + s.version = '3.5.0' s.summary = 'The Dailymotion iOS player (Swift)' s.homepage = 'https://github.com/dailymotion/dailymotion-swift-player-sdk-ios' s.author = 'Dailymotion' diff --git a/DailymotionPlayerSDK/DMPlayerViewController.swift b/DailymotionPlayerSDK/DMPlayerViewController.swift index 2f0ee9f..7882718 100644 --- a/DailymotionPlayerSDK/DMPlayerViewController.swift +++ b/DailymotionPlayerSDK/DMPlayerViewController.swift @@ -30,9 +30,9 @@ open class DMPlayerViewController: UIViewController { private var baseUrl: URL! fileprivate var isInitialized = false fileprivate var videoIdToLoad: String? - fileprivate var payloadToLoad: String? + fileprivate var payloadToLoad: [String: Any]? - public weak var delegate: DMPlayerViewControllerDelegate? + open weak var delegate: DMPlayerViewControllerDelegate? private var webView: WKWebView! @@ -64,7 +64,7 @@ open class DMPlayerViewController: UIViewController { super.init(coder: aDecoder) } - public func loadWebView(parameters: [String: Any], baseUrl: URL? = nil, accessToken: String? = nil, cookies: [HTTPCookie]? = nil) { + open func loadWebView(parameters: [String: Any], baseUrl: URL? = nil, accessToken: String? = nil, cookies: [HTTPCookie]? = nil) { self.baseUrl = baseUrl ?? DMPlayerViewController.defaultUrl webView = newWebView(cookies: cookies) view = webView @@ -83,21 +83,27 @@ open class DMPlayerViewController: UIViewController { /// /// - Parameter videoId: The video's XID /// - Parameter payload: An optional payload to pass to the load - public func load(videoId: String, payload: String? = nil) { + open func load(videoId: String, payload: [String: Any]? = nil) { guard isInitialized else { self.videoIdToLoad = videoId self.payloadToLoad = payload return } - let js = buildLoadString(videoId: videoId, payload: payload) + let js = buildLoadString(videoId: videoId, payload: payload != nil ? convertPayloadToString(payload: payload!) : nil) webView.evaluateJavaScript(js, completionHandler: nil) } + private func convertPayloadToString(payload: [String: Any]) -> String? { + guard let data = try? JSONSerialization.data(withJSONObject: payload, options: []), + let string = String(data: data, encoding: .utf8) else { return nil } + return string + } + /// Set a player property /// /// - Parameter prop: The property name /// - Parameter data: The data value to set - public func setProp(_ prop: String, data: String) { + open func setProp(_ prop: String, data: String) { guard isInitialized else { return } let js = "player.setProp('\(prop)', \(data))" webView.evaluateJavaScript(js, completionHandler: nil) @@ -195,7 +201,7 @@ open class DMPlayerViewController: UIViewController { return url } - public func toggleControls(show: Bool) { + open func toggleControls(show: Bool) { let hasControls = show ? "1" : "0" notifyPlayerApi(method: "controls", argument: hasControls) } @@ -205,30 +211,30 @@ open class DMPlayerViewController: UIViewController { webView.evaluateJavaScript("player.api('\(method)', \(playerArgument))", completionHandler: nil) } - public func toggleFullscreen() { + open func toggleFullscreen() { notifyPlayerApi(method: "notifyFullscreenChanged") } - public func play() { + open func play() { notifyPlayerApi(method: "play") } - public func pause() { + open func pause() { notifyPlayerApi(method: "pause") } - public func seek(to: TimeInterval) { + open func seek(to: TimeInterval) { notifyPlayerApi(method: "seek", argument: "\(to)") } /// Mute playback - public func mute() { + open func mute() { webView.evaluateJavaScript("player.mute()", completionHandler: nil) } /// Unmute playback - public func unmute() { + open func unmute() { webView.evaluateJavaScript("player.unmute()", completionHandler: nil) } @@ -236,7 +242,7 @@ open class DMPlayerViewController: UIViewController { extension DMPlayerViewController: WKScriptMessageHandler { - public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { + open func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { guard let event = EventParser.parseEvent(from: message.body) else { return } delegate?.player(self, didReceiveEvent: event) } @@ -261,7 +267,7 @@ final class Trampoline: NSObject, WKScriptMessageHandler { extension DMPlayerViewController: WKNavigationDelegate { - public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, + open func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { guard let url = navigationAction.request.url else { decisionHandler(.allow) @@ -278,7 +284,7 @@ extension DMPlayerViewController: WKNavigationDelegate { decisionHandler(.allow) } - public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { + open func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { isInitialized = true if let videoIdToLoad = videoIdToLoad {