尝试在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。任何帮助将非常感激!
非常感谢!
弄清楚了。其中一些方法是iOS 9之前的版本,现已不推荐使用。application当我应该在中定义了ViewController时,我也拥有了该函数AppDelagate.swift。例如
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)") } }