0

I created a class that implements NSURLProtocol which will tells me about the UIWebView requests. I am looking to tell the UI that we hit a URL of interest and run code back on the ViewController to access the WebView.

I believe Protocols are the solution but cant seem to wrap my head around how to get this to work. Any suggestions and code example would be much appreciated. Here is what I've tried so far.

UI View 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")
        }
    }

Here is my NSURLProtocol implemented class along with the attempted protocol(which please tell me if its the wrong approach)

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()
}
user516883
  • 8,868
  • 23
  • 72
  • 114

1 Answers1

0

The best way to achieve this would probably be to post an NSNotification from your protocol when you match the URL.

In CUrlProtocol, when you find a match (notification name can be your choosing):

let notification:NSNotification = NSNotification(name: "MyURLMatchedNotification", object: nil)
NSNotificationCenter.defaultCenter().postNotification(notification)

In your WebViewController:

// During init (or viewDidAppear, if you only want to do it while its on screen)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "getUserToken", name: "MyURLMatchedNotification", object: nil)

...

// During dealloc (or viewWillDisappear)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "MyURLMatchedNotification", object: nil)

You can also pass something with your notification, if you need information from that request. Just set the object parameter when you create your notification and change your getUserToken to accept a single parameter that has a type of NSNotification and access its object property.

Mean Dinosaur
  • 376
  • 1
  • 12