当前位置: 首页 > 面试题库 >

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

程胤运
2023-03-14
问题内容

尝试在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。例如

在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)")

    }
}


 类似资料:
  • 我正在开发一个web应用程序来从“https://api.ebay.com/sell/analytics/v1/traffic_report”获取数据 我有一个ebay开发者帐户,clientID:MyClientId ClientSecret:MyClientSecret AppID:MyAppId 要实现这一点,我需要一个OAuth令牌 我浏览url https://signin.ebay.c

  • 我需要用函数SSJS from mJson()读一个URL。例如Notes View的数据访问API http://{host}/{database}/api/data/collections/name/{name} 我该怎么做? P. S我认为(我不知道是否是真的),如果我使用Java代码(例如类URLReader从这个博客,我失去作者/读者的功能,因为是我的服务器,而不是当前用户执行读取流?

  • 问题内容: 关闭。 此问题不符合堆栈溢出准则。它当前不接受答案。 想改善这个问题吗? 更新问题,使其成为Stack Overflow 的主题。 6年前关闭。 我想从URL获取值,以按ID从数据库中选择数据。我想要ID值。 例如,如果我打开数据库, 我想获取id = 12的值。 如果我打开,则 获取数据库中id = 7的值,依此类推。 我有一些代码: 问题答案: 网址: 码:

  • 问题内容: 我正在尝试获取Firebase存储桶中文件的“长期持久下载链接”。我已将其权限更改为 我的javacode看起来像这样: 当我运行它时,我得到的uri链接看起来像 com.google.android.gms.tasks.zzh@xxx 问题1.我是否可以从中获得类似于以下内容的下载链接:https : //firebasestorage.googleapis.com/v0/b/pro

  • 你好,我来自从URL获取JSON对象 我试着去买一顶帽子- 有人能告诉我我做错了什么吗?

  • 问题内容: 我正在尝试使用Apache Lucene进行令牌化,但我对从中获取令牌的过程感到困惑。 最糟糕的是,我正在查看JavaDocs中解决我的问题的注释。 http://lucene.apache.org/java/3_0_1/api/core/org/apache/lucene/analysis/TokenStream.html#incrementToken%28%29 不知何故,应该使用