UIWebView リクエストについて教えてくれる NSURLProtocol を実装するクラスを作成しました。対象の URL にヒットし、ViewController でコードを実行して WebView にアクセスすることを UI に伝えたいと考えています。
プロトコルが解決策であると信じていますが、これを機能させる方法について頭を悩ませているようには見えません。提案とコード例は大歓迎です。これが私がこれまでに試したことです。
UI ビュー Controller.swift
class WebViewController: UIViewController,WebAuthDelegate {
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://example.com")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onBackClick(sender: AnyObject) {
if(webView.canGoBack){
webView.goBack()
}
}
@IBAction func onFwdClick(sender: AnyObject) {
if(webView.canGoForward){
webView.goForward()
}
}
@IBAction func onRefresh(sender: AnyObject) {
webView.reload()
}
func getUserToken() {
print("RUN THIS AFTER I HIT URL IN URL PROTOCAL CLASS")
}
}
これが私の NSURLProtocol 実装クラスと試行されたプロトコルです(間違ったアプローチであるかどうか教えてください)
class CUrlProtocol: NSURLProtocol {
//let delegate: WebAuthDelegate? = nil
override class func canInitWithRequest(request: NSURLRequest) -> Bool {
print("URL = \(request.URL!.absoluteString)")
if request.URL!.absoluteString == "http://api-dev.site.com/token" {
//Tell the UI That we now have the info and we can access the UIWebView Object
}
return false
}
}
protocol WebAuthDelegate{
func getUserToken()
}