SafariViewController: How to grab OAuth token from URL?

前端 未结 2 1742
时光说笑
时光说笑 2020-12-08 05:01

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 re

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 05:23

    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

    Added at end of 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)")
    
        }
    }
    

提交回复
热议问题