-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathYouTubePlayerViewController.swift
68 lines (55 loc) · 1.86 KB
/
YouTubePlayerViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#if os(macOS)
import AppKit
#else
import UIKit
#endif
// MARK: - YouTubePlayerBaseViewController
#if os(macOS)
/// The YouTubePlayerBase NSViewController
public class YouTubePlayerBaseViewController: NSViewController {}
#else
/// The YouTubePlayerBase UIViewController
public class YouTubePlayerBaseViewController: UIViewController {}
#endif
// MARK: - YouTubePlayerViewController
/// The YouTube player view controller.
public final class YouTubePlayerViewController: YouTubePlayerBaseViewController {
// MARK: Properties
/// The YouTubePlayer
public let player: YouTubePlayer
// MARK: Initializer
/// Creates a new instance of ``YouTubePlayerViewController``
/// - Parameters:
/// - player: The YouTubePlayer
public init(
player: YouTubePlayer
) {
self.player = player
super.init(nibName: nil, bundle: nil)
}
/// Initializer with NSCoder is unavailable.
/// Use `init(player:)`
@available(*, unavailable)
public required init?(
coder aDecoder: NSCoder
) { nil }
/// Deinit
deinit {
Task { [weak player] in
try? await player?.pause()
}
}
// MARK: View-Lifecycle
/// View did load
public override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.player.webView)
self.player.webView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.player.webView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.player.webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.player.webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
self.player.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
])
}
}