结合使用WKWebView和OAuthSwift

时间:2018-09-05 20:07:24

标签: ios wkwebview sfsafariviewcontroller

我正在使用OAuthSwift为我的iOS应用实现登录。使用标准库功能可以正常工作。

但是,我想自定义登录页面视图,我不喜欢SafariURLHandler,而是想使用自己的视图控制器来呈现Web视图。

OAuthSwift文档状态: 例如,您可以通过提供显示Web视图的控制器(UIWebView,WKWebView)将Web视图嵌入到应用程序中。然后,此控制器必须实现OAuthSwiftURLHandlerType才能将URL加载到Web视图中

但是,我不知道如何实现此目的。

为便于调试,这是我的应用程序的简化版本:

import UIKit
import OAuthSwift
import SafariServices

class ViewController: UIViewController {

    let oauthSwift = OAuth1Swift(
        consumerKey: "...",
        consumerSecret: "...",
        requestTokenUrl: "https://api.discogs.com/oauth/request_token",
        authorizeUrl: "https://www.discogs.com/oauth/authorize",
        accessTokenUrl: "https://api.discogs.com/oauth/access_token"
    )

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        initAuthFlow()
    }

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

    fileprivate func initAuthFlow() -> Void {

        let handler = SafariURLHandler(viewController: self, oauthSwift: self.oauthSwift)
        handler.presentCompletion = {
            print("Safari presented")
        }
        handler.dismissCompletion = {
            print("Safari dismissed")
        }

        handler.factory = { url in
            let controller = SFSafariViewController(url: url)
            // Customize it, for instance
            if #available(iOS 10.0, *) {

                controller.preferredBarTintColor = UIColor.red
            }
            return controller
        }

        oauthSwift.authorizeURLHandler = handler

        guard let callbackURL = URL(string: "oauth-discogs://oauth-callback") else { return }

        oauthSwift.authorize(withCallbackURL: callbackURL, success: { credential, response, params in
            self.showTokenAlert(name: "Discogs Login", credential: credential)
        }) { error in
            print(error.localizedDescription)
        }
    }

    fileprivate func showAlertView(title: String, message: String) {
        #if os(iOS)
            let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
        #elseif os(OSX)
            let alert = NSAlert()
            alert.messageText = title
            alert.informativeText = message
            alert.addButton(withTitle: "Close")
            alert.runModal()
        #endif
    }

    fileprivate func showTokenAlert(name: String?, credential: OAuthSwiftCredential) {
        var message = "oauth_token:\(credential.oauthToken)"
        if !credential.oauthTokenSecret.isEmpty {
            message += "\n\noauth_token_secret:\(credential.oauthTokenSecret)"
        }
        self.showAlertView(title: name ?? "Service", message: message)

    }
}

如何创建一个ViewController,导入类似WebKit之类的东西,并使用它来呈现我的登录页面?

我尝试过-

let handler = LoginWebView()
oauthSwift.authorizeURLHandler = handler as! OAuthSwiftURLHandlerType

LoginWebView 为-

import UIKit
import OAuthSwift
import WebKit

class LoginWebView: UIViewController, WKNavigationDelegate {

    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://www.some-url.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }
}

但是这失败了,因为它无法转换类型。

0 个答案:

没有答案