SafariViewController: как получить токен OAuth с URL?
Попытка использовать Facebook OAuth с SafariViewController. Сначала я открываю authURL с SafariViewController, который, если пользователь вошел в Facebook на Safari, перенаправит их и вернет URL OAuth с токеном для этой конкретной службы, например Instagram
ОТВЕТ: https://www.facebook.com/connect/login_success.html
Когда 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 ответа
Догадаться. Некоторые из методов были до iOS 9 и теперь устарели. Я также имел application
функция в ViewController, который я создал, когда он должен был быть определен в 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)")
}
}
iOS 12
Бета-версия iOS 12 уже не поддерживает SFAuthenticationSession (см. ниже) в пользу ASWebAuthenticationSession. Похоже, что он используется точно так же, но требует новой платформы AuthenticationServices.
iOS 11
iOS 11 представила SFAuthenticationSession, который намного проще в обращении. Учитывая его природу, этот бета-интерфейс API все еще может измениться, но в Интернете уже есть пара примеров ( 1, 2). Во-первых, вам нужен обработчик завершения, который вызывается с результатом запроса аутентификации:
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
нажав на 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 перенаправления, который вы зарегистрировали в веб-службе, с которой вы пытаетесь авторизоваться.