Swift:WKWebView无法使用标题

时间:2017-03-14 16:04:16

标签: ios swift video-streaming wkwebview

我有一个安全的私人网址,我必须通过它传递两个标头才能获得响应。 当我尝试使用URL将数据加载到WKWebView时,如果它是一个Image,它会在WKWebView上显示Image而没有任何问题,但如果它是一个Video它只是显示一个带有播放按钮的空白屏幕,当我点击时在播放按钮上没有任何反应。我等了近20分钟,认为视频正在加载,即便没有回复。

识别正确的mimetype和内容类型

有人可以看看我的代码并告诉我哪里出错了。如果必须对URL或后端进行任何更改,应该做什么以便URL将流式传输视频或在webview上播放它们。

这是我的代码:

import UIKit
import Foundation
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {

var request:NSMutableURLRequest!
var wkwebview: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    tryload()

    wkwebview = WKWebView(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: self.view.frame.height - 20), configuration: WKWebViewConfiguration())

    self.view.addSubview(wkwebview)
}

func tryload() {

    let headers = [
        "auth-token": "+XXXXX:XXXX:XXX",
        "Accept-Encoding": "",
        ]

    let request = NSMutableURLRequest(url: NSURL(string: "http://example.com/api/assets/download/a235c923-ca7g-42be-81dd-89b14d0?Id=1&type=ASSET&otherId=9")! as URL,
                                      cachePolicy: .useProtocolCachePolicy,
                                      timeoutInterval: 10.0)

    request.httpMethod = "GET"
    request.allHTTPHeaderFields = headers

    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            print("the error is \(error)")
        } else {
            let httpResponse = response as? HTTPURLResponse
            print("The HTTP response is \(httpResponse)")
            print("the response is \(response?.mimeType)")
            print("the data is \(data)")

            self.wkwebview.load(request as URLRequest)
        }
    })

    dataTask.resume()

}
}

0 个答案:

没有答案