Skip to content

Commit

Permalink
Merge pull request #13 from Ekhoo/master
Browse files Browse the repository at this point in the history
Method Access Control update + Simplify the load customization
  • Loading branch information
JanGorman committed Jul 28, 2017
2 parents 461ab57 + c33b37e commit 91364c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DailymotionPlayerSDK.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
38 changes: 22 additions & 16 deletions DailymotionPlayerSDK/DMPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -205,38 +211,38 @@ 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)
}

}

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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 91364c0

Please sign in to comment.