SafariViewController:如何从 URL 中获取 OAuth 令牌? [英] SafariViewController: How to grab OAuth token from URL?
问题描述
尝试在 SafariViewController 中使用 Facebook OAuth.首先,我使用 SafariViewController 打开 authURL,如果用户在 Safari 上登录 Facebook,它将重定向它们并返回带有该特定服务令牌的 OAuth URL,例如Instagram
Trying to use Facebook OAuth with the SafariViewController. First I open the authURL with SafariViewController, which if the user is logged in to Facebook on Safari, will redirect them and return an OAuth URL with the token for that specific service, e.g. Instagram
当 SafariViewController 重定向时,我想获取响应 URL 并存储它,以便我可以获取令牌.这是我的代码:
When SafariViewController has redirected I want to grab the response URL and store it so I can grab the token. Here is my code:
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.任何帮助将不胜感激!
It opens the authURL and redirects to the correct response URL, but the observer does not fire the safariLogin function to grab the URL. Any help would be much appreciated!
非常感谢!
推荐答案
想通了.一些方法是 iOS 9 之前的,现在已弃用.我在创建的 ViewController 中也有 application
函数,但它应该在 AppDelagate.swift
中定义.例如
Figured it out. Some of the methods were pre iOS 9 and now deprecated. I also had the application
function in the ViewController I created when it should have been defined in the AppDelagate.swift
. For example
在 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)")
}
}
这篇关于SafariViewController:如何从 URL 中获取 OAuth 令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!