2

I have a log when I load youtube URL in WKWebView.
I have searched the same title of my question in stackOverFlow.
But it didn't work for me.
What's wrong with my code?
And I use swift4 & xcode9.2.
Thanks.

Transport also set true.
open

Warning like this:

Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service

import UIKit
import WebKit

class DetailViewController: UIViewController {

var videoId: String = ""
var videoTitle: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    loadUI()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func loadUI() {

    view.backgroundColor = UIColor.white

    naviSetting()
    webViewSetting()
}

func naviSetting() {

    self.title = videoTitle
}

func webViewSetting() {

    let webview = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))

    let url = URL(string: "https://www.youtube.com/watch?v=\(self.videoId)")
    let request = URLRequest(url: url!)
    webview.load(request)
    self.view.addSubview(webview)
}
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
tolerate_Me_Thx
  • 387
  • 2
  • 7
  • 21

2 Answers2

1

I have tried to encode parameter and Its working fine for me. Please check below code.

var wkWebView = WKWebView()

override func viewDidLoad()
{
    super.viewDidLoad()

    self.automaticallyAdjustsScrollViewInsets = false
    self.wkWebView.translatesAutoresizingMaskIntoConstraints = false
    self.wkWebView.frame =  CGRect.init(x: 0, y: 0, width: self.wkWebView.frame.size.width, height: self.wkWebView.frame.size.height)
    self.view.addSubview(wkWebView)

    loadUrl()
}
override func viewWillLayoutSubviews()
{
    super.viewWillLayoutSubviews()
    self.wkWebView.frame =  CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    self.wkWebView.contentMode = .scaleAspectFit
}
func loadUrl()
{
    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.allowsInlineMediaPlayback = true

    let url_join = "v=1p38GWfCIhQ"

    let myURL = URL(string: "https://www.youtube.com/watch?")
    var myRequest = URLRequest(url: myURL!)
    myRequest.httpMethod = "POST"
    myRequest.httpBody = url_join.data(using: String.Encoding.utf8)
    wkWebView.load(myRequest)
}

I hope It will also work for you. :)

iOS Developer
  • 538
  • 4
  • 10
0

For me this Code worked fine. Notice, I Used http:// and not https://. But you already have set the settings for http:// to work.enter image description here

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

@IBOutlet weak var webView: WKWebView!
private let url = URL(string: "http://www.google.com")

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let request = URLRequest(url: url!)
    webView = WKWebView(frame: self.view.frame)
    self.view.addSubview(webView)
    webView.navigationDelegate = self
    webView.load(request)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}
nani
  • 183
  • 2
  • 15