mobileplayer-ios
mobileplayer-ios copied to clipboard
Add headers to Video URL
I am trying to add headers to the request with no luck. I followed the link given in #140, but it does not seem to work. Here is what I have done so far:
import Foundation
class VideoURLProtocol: NSURLProtocol, NSURLConnectionDelegate{
internal var connection: NSURLConnection!
override class func canInitWithRequest(request: NSURLRequest) -> Bool {
let theURL = request.URL
let scheme = theURL!.scheme
if (scheme == "customProtocol") {
return true
}
return false
}
override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest {
return request
}
override class func requestIsCacheEquivalent(aRequest: NSURLRequest,
toRequest bRequest: NSURLRequest) -> Bool {
return super.requestIsCacheEquivalent(aRequest, toRequest: bRequest)
}
override func startLoading() {
Log.debug("Incoming URL: \((self.request.URL)!)")
let mutableRequest = self.request.mutableCopy() as! NSMutableURLRequest
if let configToken = config?.accessToken, configTokenName = config?.accessTokenFieldName {
mutableRequest.setValue(configToken, forHTTPHeaderField: configTokenName)
}
if let oldUrl = self.request.URL {
let newUrl = NSURLComponents()
newUrl.scheme = "http"
newUrl.port = oldUrl.port
newUrl.host = oldUrl.host
newUrl.path = oldUrl.path
mutableRequest.URL = newUrl.URL
Log.debug("Outgoing URL: \((mutableRequest.URL)!)")
}
self.connection = NSURLConnection(request: mutableRequest, delegate: self)
}
override func stopLoading() {
self.connection?.cancel()
}
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
self.client!.URLProtocol(self, didReceiveResponse: response, cacheStoragePolicy: .Allowed)
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
self.client!.URLProtocol(self, didLoadData: data)
}
func connectionDidFinishLoading(connection: NSURLConnection!) {
self.client!.URLProtocolDidFinishLoading(self)
self.connection = nil
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
self.client!.URLProtocol(self, didFailWithError: error)
self.connection = nil
}
}
Is there an easier way to do so? And if not, do you know why this is not working?
Any help is really appreciated. Thanks!