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

时间:2016-08-07 21:33:50

标签: swift facebook oauth swift2 sfsafariviewcontroller

尝试将Facebook OAuth与SafariViewController一起使用。首先,我使用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 个答案:

答案 0 :(得分:24)

iOS 12

iOS 12 Beta已经弃用了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的博客文章中,可以找到link to the related GitHub project。该项目的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)

然后删除处理程序中的observance:

@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按钮来关闭Done,因此请务必采用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必须与您尝试向其提供的网络服务注册的重定向URI方案相匹配。

答案 1 :(得分:0)

想出来。有些方法是iOS 9之前的版本,现已弃用。我应该在Date3 = format(txtfrom,"mm/dd/yyyy") For x = 0 to datediff("d",txtFrom,txtto) select RecNo from tblA where date3 between tblA.FromDate and tblA.ToDate rs.open <--- check my record count if everything good then Date3 = dateadd("d",1,Date3) next 中定义的ViewController中创建了application函数。例如

在AppDelegate.swift结束时添加

AppDelagate.swift

<强> ViewController.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
    }