SafariViewController:如何从URL中获取OAuth令牌?

27

尝试在SafariViewController中使用Facebook OAuth。首先,我使用SafariViewController打开authURL,如果用户已经在Safari中登录了Facebook,则会将其重定向并返回一个OAuth URL,其中包含该特定服务(例如Instagram)的令牌。

响应: https://www.facebook.com/connect/login_success.html#access_token=BLAHTOKENRESPONSE&expires_in=5114338

当SafariViewController重定向后,我想获取响应的URL并存储它,以便我可以获取令牌。以下是我的代码:

import SafariServices

let kSafariViewControllerCloseNotification = "kSafariViewControllerCloseNotification"

import UIKit

// facebook OAuth URL for service
let authURL = NSURL(string: "https://www.facebook.com/dialog/oauth?client_id=3627644767&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=basic_info,email,public_profile,user_about_me,user_activities,user_birthday,user_education_history,user_friends,user_interests,user_likes,user_location,user_photos,user_relationship_details&response_type=token")

class ViewController: UIViewController, SFSafariViewControllerDelegate {

    var safariVC: SFSafariViewController?
    @IBOutlet weak var loginButton: UIButton!

    @IBAction func loginButtonTapped(sender: UIButton) {
        safariVC = SFSafariViewController(URL: authURL!)
        safariVC!.delegate = self
        self.presentViewController(safariVC!, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // not firing the safariLogin function below
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.safariLogin(_:)), name: kSafariViewControllerCloseNotification, object: nil)
    }

    func safariLogin(notification: NSNotification) {
        print("Safari Login call")
        // get the url form the auth callback
        let url = notification.object as! NSURL
        print(url)
        self.safariVC!.dismissViewControllerAnimated(true, completion: nil)
    }

    func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
        print("application call")
        // just making sure we send the notification when the URL is opened in SFSafariViewController
        if (sourceApplication == "com.application.SafariViewTest") {
            NSNotificationCenter.defaultCenter().postNotificationName(kSafariViewControllerCloseNotification, object: url)
            return true
        }
        return true
    }  

}

它打开authURL并重定向到正确的响应URL,但观察者没有触发safariLogin函数来抓取URL。非常感谢任何帮助!

非常感谢!

2个回答

37

iOS 12+

iOS 12测试版已经弃用SFAuthenticationSession(参见下文),转而使用ASWebAuthenticationSession。它看起来与原来的方法完全相同,但需要使用新的AuthenticationServices框架。

iOS 11

iOS 11引入了SFAuthenticationSession,它易于处理。鉴于其性质,此测试版API可能仍会更改,但在互联网上已有一些示例(12)。首先,您需要一个完成处理程序,该处理程序将调用身份验证请求的结果:

let completion : SFAuthenticationSession.CompletionHandler = { (callBack:URL?, error:Error?) in
    guard error == nil, let successURL = callBack else {
        return
    }

    let oauthToken = NSURLComponents(string: (successURL.absoluteString))?.queryItems?.filter({$0.name == "oauth_token"}).first

    // Do what you have to do...
}

那么你只需创建一个SFAuthenticationSession并启动它即可。

let authURL = "https://the.service.you/want/toAuthorizeWith?..."
let scheme = "YOURSCHEME://"
let authSession = SFAuthenticationSession(url: authURL, callbackURLScheme: scheme, completionHandler: completion)
authSession.start()

iOS 10及以下版本

正如评论中有些人指出的那样,接受的答案是不完整的,而且单独使用是行不通的。当浏览Strawberry Code博客文章时,可以找到相关GitHub项目的链接。该项目的README.MD解释了设置的一个关键部分,即将重定向URI添加到Info.plist中。因此,整个过程如下:

AppDelegate.swift

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

   if let sourceApplication = options[.sourceApplication] {
       if (String(describing: sourceApplication) == "com.apple.SafariViewService") {
            NotificationCenter.default.post(name: Notification.Name("CallbackNotification"), object: url)
            return true
        }
    }

    return false
}

ViewController.swift

注册通知,以便在某个合适的地方调用你的处理程序。我建议不要在viewDidLoad()中执行此操作,而是在实际呈现SFSafariViewController之前执行。

NotificationCenter.default.addObserver(self, selector: #selector(safariLogin(_:)), name: Notification.Name("CallbackNotification"), object: nil)
let safariVC = SFSafariViewController(URL: authURL)
safariVC.delegate = self
self.present(safariVC, animated: true, completion: nil)

然后在处理程序中删除观察。

@objc func safariLogin(_ notification : Notification) {

    NotificationCenter.default.removeObserver(self, name: Notification.Name("CallbackNotification"), object: nil)

    guard let url = notification.object as? URL else {
        return
    }

    // Parse url ...

}

请记住,用户可以通过点击“完成”按钮来关闭SFSafariViewController,因此,请确保采用SFSafariViewControllerDelegate协议,并像下面这样取消观察:

func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
    NotificationCenter.default.removeObserver(self, name: Notification.Name("CallbackNotification"), object: nil)
}

Info.plist

为了使其工作,您需要将重定向URI方案添加到Info.plist中:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.YOUR.BUNDLE.IDENTIFIER</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>YOURSCHEME</string>
        </array>
    </dict>
</array>

当然,YOURSCHEME必须与您尝试进行授权的Web服务注册的重定向URI方案匹配。


1
嘿,我按照你写的方法为iOS 10及以下版本添加了一个方案。但是由于某种原因,AppDelegate的方法没有被调用。你有任何想法吗?我的方案看起来像 https://example.com,重定向到 https://example.com/redirect。从我的角度来看,这应该可以工作,对吧? - Błażej
@VyachaslavGerchicov,你有找到任何解决方法吗?我也遇到了同样的问题。 - Vinod Jadhav
@bfx:为了自动解除身份验证网页,服务器应该返回什么类型的JSON响应或操作? - Abhishek Thapliyal

-2
找到问题所在了。一些方法是预iOS 9的,现在已经被弃用了。我还在我创建的ViewController中使用了application函数,但它应该在AppDelagate.swift中定义。例如:

添加到AppDelegate.swift的末尾

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

        print("app: \(app)")
        // print OAuth response URL
        print("url: \(url)")
        print("options: \(options)")

        if let sourceApplication = options["UIApplicationOpenURLOptionsSourceApplicationKey"] {
            if (String(sourceApplication) == "com.testApp.Incognito") {
                NSNotificationCenter.defaultCenter().postNotificationName(kSafariViewControllerCloseNotification, object: url)
                return true
            }
        }
        return true
    }

ViewController.swift

import SafariServices

let kSafariViewControllerCloseNotification = "kSafariViewControllerCloseNotification"

// facebook OAuth URL
let authURL = NSURL(string: "https://www.facebook.com/dialog/oauth?client_id=3627644767&redirect_uri=https://www.facebook.com/connect/login_success.html&scope=basic_info,email,public_profile,user_about_me,user_activities,user_birthday,user_education_history,user_friends,user_interests,user_likes,user_location,user_photos,user_relationship_details&response_type=token")

class ViewController: UIViewController, SFSafariViewControllerDelegate {

    var safariVC: SFSafariViewController?
    @IBOutlet weak var loginButton: UIButton!

    @IBAction func loginButtonTapped(sender: AnyObject) {
        safariVC = SFSafariViewController(URL: authURL!)
        safariVC!.delegate = self
        self.presentViewController(safariVC!, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.safariLogin(_:)), name: kSafariViewControllerCloseNotification, object: nil)
    }

    func safariLogin(notification: NSNotification) {
        // get the url from the auth callback
        let url = notification.object as! NSURL
        // Finally dismiss the Safari View Controller with:
        self.safariVC!.dismissViewControllerAnimated(true, completion: nil)
    } 

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func safariViewControllerDidFinish(controller: SFSafariViewController) {
        controller.dismissViewControllerAnimated(true) { () -> Void in
            print("You just dismissed the login view.")
        }
    }

    func safariViewController(controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
        print("didLoadSuccessfully: \(didLoadSuccessfully)")

    }
}

我有点迷失在你如何获取重定向 URL 来调用应用程序函数方面。我想做类似的事情,复制了你所做的,但我无法让 SFSafariViewController 回调应用程序函数。 - Zack
我也是。我想知道,在重定向之后,SFSafariViewController如何回调应用程序。 - Chamnap
10
嗯...你刚刚从http://strawberrycode.com/blog/sfsafariviewcontroller-and-oauth-the-instagram-example/上复制了代码,却没有注明出处... - AVog

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接