WebAVPlayerController valueForUndefinedKey,this class is not key value coding-compliant for the key playingOnMatchPointDevice - swift5

Problem Encountered with below action :
[<WebAVPlayerController 0x600001308000> valueForUndefinedKey:]: this class is not key value coding-compliant for the key playingOnMatchPointDevice." 0x00006000025e05a0
Action Tested on iOS 15+:
hit Landscape(Full Screen) icon on the Video and the video crashes when it's full screen
I had been searching and could not find any solution on this problem. I am using WKWebView in Xcode 13.
I followed this link
iOS15 Webview using `Video` causes crashes - `WebAVPlayerController valueForUndefinedKey - playingOnMatchPointDevice `
But the problem is still not solved. Your help is greatly appreciated.
here the code
import UIKit
import WebKit
class TitlePreviewViewController: UIViewController,WKUIDelegate {
private let titleLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = .systemFont(ofSize: 22, weight: .bold)
label.text = "Harry potter"
return label
}()
private let overviewLabel: UILabel = {
let label = UILabel()
label.font = .systemFont(ofSize: 18, weight: .regular)
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.text = "This is the best movie ever to watch as a kid!"
return label
}()
lazy var webView: WKWebView = {
let configuration = WKWebViewConfiguration()
configuration.allowsInlineMediaPlayback = true
let webView = WKWebView(frame:.zero,configuration: configuration)
if #available(iOS 10.0, *) {
configuration.mediaTypesRequiringUserActionForPlayback = []
} else {
configuration.requiresUserActionForMediaPlayback = false
}
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
view.addSubview(webView)
view.addSubview(titleLabel)
view.addSubview(overviewLabel)
configureConstraints()
webView.uiDelegate = self
webView.navigationDelegate = self
}
func configureConstraints() {
let webViewConstraints = [
webView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
webView.heightAnchor.constraint(equalToConstant: 300)
]
let titleLabelConstraints = [
titleLabel.topAnchor.constraint(equalTo: webView.bottomAnchor, constant: 20),
titleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
]
let overviewLabelConstraints = [
overviewLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 15),
overviewLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
overviewLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor)
]
NSLayoutConstraint.activate(webViewConstraints)
NSLayoutConstraint.activate(titleLabelConstraints)
NSLayoutConstraint.activate(overviewLabelConstraints)
}
public func configure(with model: TitlePreviewViewModel) {
titleLabel.text = model.title
overviewLabel.text = model.titleOverview
guard let url = URL(string: "https://www.youtube.com/embed/\(Video Url )") else {
return
}
webView.load(URLRequest(url: url))
}
}
extension TitlePreviewViewController: WKNavigationDelegate{
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
}
Thanks

Related

WKWebView - Margin Top

I'm starting swift.
Code is a webview without superior navigation, constrain moves the webview from the top leaving an upper margin.
see the image
Image Result
My code
import UIKit
import WebKit
class ViewController: UIViewController,WKNavigationDelegate, WKUIDelegate {
lazy var webView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
func setupUI() {
self.view.backgroundColor = .red
self.view.addSubview(webView)
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
webView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
webView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
webView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor)
])
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
let myURL = URL(string: "https://www.google.com.br/")
let myRequest = URLRequest(url: myURL!,cachePolicy: NSURLRequest.CachePolicy.useProtocolCachePolicy,timeoutInterval: 10.0)
webView.load(myRequest)
}
}
Solved. Just add:
self.navigationController? .isNavigationBarHidden = true

fetch core data date as string to display on label

I want my swift code to save 1 date in core data. on lbl1 i want the first core data date to be display. The label should only display the first date saved. Here is my code below which is not working. Nothing is being displayed on lbl1 when the function is called. I a link to my github profile below with this exact code.
https://github.com/redrock34/fetch
import UIKit
import CoreData
class ViewController: UIViewController {
var foodItems = [Food]()
var moc:NSManagedObjectContext!
var appDelegate = UIApplication.shared.delegate as? AppDelegate
var btn = UIButton()
var lbl1 = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
moc = appDelegate?.persistentContainer.viewContext
[btn,lbl1].forEach{
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}
NSLayoutConstraint.activate([
btn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.2),
btn.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
btn.topAnchor.constraint(equalTo: view.topAnchor),
btn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
lbl1.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.2),
lbl1.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
lbl1.topAnchor.constraint(equalTo: btn.bottomAnchor),
lbl1.leadingAnchor.constraint(equalTo: view.leadingAnchor),
])
btn.backgroundColor = .orange
lbl1.backgroundColor = .systemTeal
btn.addTarget(self, action: #selector(addFoodToDatabase(_:)), for: .touchUpInside)
}
#objc func addFoodToDatabase(_ sender: UIButton) {
let foodItem = Food(context: moc)
foodItem.added = NSDate() as Date
appDelegate?.saveContext()
place()
}
func place(){
let foodRequest:NSFetchRequest<Food> = Food.fetchRequest()
// 2
// 3
do {
try foodItems = moc.fetch(foodRequest)
}catch {
print("Could not load data")
}
lbl1.text = moc.name
}
}

Why doesn't my UILabel change when I press the button?

By pressing a button in my app, the value of a variable falls by 3. While this happens without any issues, the label which uses string interpolation to show that variable as its text (label.text) does not reflect the change.
How can I make it so pressing the button changes the value of the UILabel?
import UIKit
class ViewController: UIViewController {
var token = 5
let theButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("button", for: .normal)
button.backgroundColor = .systemPink
button.addTarget(self, action: #selector(theButtonPressed), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
#objc func theButtonPressed() {
if token >= 3 {
token -= 3
print("ok done")
} else {
print("nope")
}
}
lazy var tokenLabel: UILabel = {
let label = UILabel()
label.text = "\(token)"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBlue
view.addSubview(tokenLabel)
view.addSubview(theButton)
theButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
theButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
theButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
theButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
tokenLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 60).isActive = true
tokenLabel.heightAnchor.constraint(equalToConstant: 300).isActive = true
tokenLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
tokenLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
}
You need to update the label's text when the button is pressed.
#objc func theButtonPressed() {
if token >= 3 {
token -= 3
print("ok done")
tokenLabel.text = "\(token)" // <- update
} else {
print("nope")
}
}
Or, you can observe the property token and change the label when a new value is set.
var token = 5 {
didSet {
tokenLabel.text = "\(token)"
}
}
#objc func theButtonPressed() {
if token >= 3 {
token -= 3
print("ok done")
} else {
print("nope")
}
}

Save with UserDefaults that checkbox is check or uncheck swift

I solved the problem
override func viewDidLoad() {
super.viewDidLoad()
let save = UserDefaults.standard.bool(forKey: "RememberMe")
self.circleBox.isChecked = save
}
#objc func checkboxvalue(sender: Checkbox) {
if sender.isChecked == true {
labelcheckbox.text = ("Beni")
action((Any).self)
UserDefaults.standard.set(true, forKey:"RememberMe");
}else{
labelcheckbox.text = ("")
UserDefaults.standard.set(false, forKey:"RememberMe");
}
}
I've designed a recall checkbox. But when I log out of the application, I want the checkbox value to be set to UserDefaults as true or false. So I want the checkbox to remember true or false when I get into the application.
lazy var circleBox: Checkbox = {
let squareBox = Checkbox(frame: CGRect(x: 22, y: 290, width: 25, height: 25))
squareBox.tintColor = .black
squareBox.borderStyle = .square
squareBox.checkmarkStyle = .square
squareBox.uncheckedBorderColor = .lightGray
squareBox.borderWidth = 1
squareBox.addTarget(self, action: #selector(checkboxvalue(sender:)), for: .valueChanged)
return squareBox
}()
#objc func checkboxvalue(sender: Checkbox) {
if sender.isChecked == true {
labelcheckbox.text = ("Remember me")
action((Any).self)
}else{
labelcheckbox.text = ("Don't remember")
}
}

Square UIImageView and UITextField in UITableViewCell

I'm trying to create a cell that contains a square UIImageView and a UITextField. If the text fits the width of the screen, then everything works fine. But as soon as I change the text to a longer one, the presentation breaks down. There are no messages in the console about the correctness of the constraints.
When the text in the UITextField fits on the screen or UITextField contains only placeholder everything works fine.
But if the text is long enough, the presentation breaks down.
The code with which I create the view:
extension TextFieldWithImageTVCell {
private func initView() {
selectionStyle = .none
initImgView()
initTextField()
contentView.addSubview(imgView)
contentView.addSubview(textField)
// debug code
imgView.backgroundColor = UIColor.black
textField.backgroundColor = UIColor.blue
}
private func initImgView() {
imgView = UIImageView(frame: CGRect.zero)
imgView.translatesAutoresizingMaskIntoConstraints = false
}
private func initTextField() {
textField = UITextField(frame: CGRect.zero)
textField.translatesAutoresizingMaskIntoConstraints = false
textField.isUserInteractionEnabled = false
}
}
The code with which I create constraints:
extension TextFieldWithImageTVCell {
private func initConstraints() {
initConstraintsImgView()
initConstraintsTextField()
}
private func initConstraintsImgView() {
imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor).isActive = true
let margins = contentView.layoutMarginsGuide
imgView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
imgView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
imgView.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
}
private func initConstraintsTextField() {
textField.leadingAnchor.constraint(equalTo: imgView.trailingAnchor, constant: 8).isActive = true
let margins = contentView.layoutMarginsGuide
textField.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
textField.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
textField.bottomAnchor.constraint(equalTo: margins.bottomAnchor).isActive = true
}
}
The part of code with which I configure cell:
cell.textContent = subject
cell.placeholder = "Предмет"
cell.isEditingEnabled = true
guard let textField = cell.textField else {
assertionFailure("error in ItemSubject::configureCellForRow")
return
}
textField.delegate = self
textField.clearButtonMode = .always
textField.adjustsFontSizeToFitWidth = true
textField.minimumFontSize = 10
Cell properties:
extension TextFieldWithImageTVCell {
var placeholder: String? {
set { textField.placeholder = newValue }
get { return textField.placeholder }
}
var textContent: String? {
set { textField.text = newValue }
get { return textField.text }
}
var isEditingEnabled: Bool {
set { textField.isUserInteractionEnabled = newValue }
get { return textField.isUserInteractionEnabled }
}
}
What constraints do I need to add to achieve the desired result?
Why are my constraints not enough?