Vlad Alexeev
Vlad Alexeev

Reputation: 2214

Get - response from URL via UIWebView SWIFT

I'm trying to learn IOS programming, really first steps, and can't find Swift Examples for my issue with a complete class code. The Web page is being loaded, but I never get a response. It's obviously something with "extension" part and I probably don't need delegate

import UIKit

class VkLoginViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!
    var delegate:UIWebViewDelegate!

    override func viewDidLoad() {
        super.viewDidLoad()
        let link = String.localizedStringWithFormat("http://api.vk.com/oauth/authorize?client_id=%@&scope=email,photos,offline&redirect_uri=http://api.vk.com/blank.html&display=touch&response_type=token", Setup.VK_AUTH_SCHEME)
        NSLog(link)
        delegate = self
        UIWebView.loadRequest(webView)(NSURLRequest(URL: NSURL(string: link)!))
    }

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


}

extension VkLoginViewController : UIWebViewDelegate{
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        let str = request.URL?.absoluteString
        NSLog(str!)
        return true
    }
}

I never get to second NSLog's message

Upvotes: 2

Views: 4604

Answers (1)

iSashok
iSashok

Reputation: 2446

Modify your code to

class VkLoginViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let link = String.localizedStringWithFormat("http://api.vk.com/oauth/authorize?client_id=%@&scope=email,photos,offline&redirect_uri=http://api.vk.com/blank.html&display=touch&response_type=token", Setup.VK_AUTH_SCHEME)
        NSLog(link)
        webView.delegate = self
        webView.loadRequest(webView)(NSURLRequest(URL: NSURL(string: link)!))
    }

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

}

extension VkLoginViewController : UIWebViewDelegate{
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

        let str = request.URL?.absoluteString
        NSLog(str!)
        return true
    }
}

Hope this help

Upvotes: 4

Related Questions