rubygarage / authorize-me

Authorization with social networks
https://rubygarage.org
MIT License
45 stars 15 forks source link

Deprecate UIWebView APIs #16

Open universeAuth opened 4 years ago

universeAuth commented 4 years ago

Please change UIWebView to WKWebview in ProviderController for avoid Deprecate API

Thank You :)

thepandaofficial commented 4 years ago

Hi Was this ever done?

7va commented 4 years ago

`// // WebViewController.swift // Authorizer // // Created by Radislav Crechet on 5/5/17. // Copyright © 2017 RubyGarage. All rights reserved. //

import UIKit import WebKit

class ProviderController: UINavigationController {

var request: URLRequest!
var redirectUri: String!
var completion: WebRequestService.Completion!

private let cancelButtonTitle = "Cancel"

private var webView: WKWebView!
private var webViewController: UIViewController!

override func viewDidLoad() {
    super.viewDidLoad()

    configureWebView()
    configureWebViewController()

    pushViewController(webViewController, animated: false)
}

private func configureWebView() {
    webView = WKWebView(frame: view.bounds)
    webView.navigationDelegate = self
    webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    webView.load(request)
}

private func configureWebViewController() {
    webViewController = UIViewController()
    webViewController.view.frame = view.bounds
    webViewController.title = title

    webViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: cancelButtonTitle,
                                                                         style: .plain,
                                                                         target: self,
                                                                         action: #selector(cancel))

    webViewController.view.addSubview(webView)
}

func complete(withUrl url: URL?, error: AuthorizeError?) {
    dismiss(animated: true)
    completion(url, error)
}

@objc func cancel() {
    complete(withUrl: nil, error: AuthorizeError.cancel)
}

}

extension ProviderController : WKNavigationDelegate, WKUIDelegate {

//Equivalent of shouldStartLoadWithRequest:
   public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

    if navigationAction.request.url?.absoluteString.hasPrefix(redirectUri) ?? false {
        complete(withUrl: navigationAction.request.url, error: nil)
       decisionHandler(.cancel)

    } else {
        decisionHandler(.allow)
    }

}

//Equivalent of webViewDidStartLoad:
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {

// print("didStartProvisionalNavigation - webView.url: (String(describing: webView.url?.description))") } } `