松懈登录不起作用。SWIFT 3.0 [英] Slack Login does't work. Swift 3.0

查看:0
本文介绍了松懈登录不起作用。SWIFT 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SWIFT 3.0开发带有Slack集成的iOS应用程序。如何通过登录流程:

func loginToSlackWithSafariBrowser() {
    let scope = "channels%3Awrite+users%3Aread"
    let clientId = "...myClientId"
    let redirect_uri = "myAppDeepLink://"
    let authURL = NSURL(string: "https://slack.com/oauth/authorize?client_id=(clientId)&scope=(scope)&redirect_uri=(redirect_uri)")

    guard let url  = authURL else { return }
    UIApplication.shared.openURL(url as URL)
}
然后Safari应用程序打开,我输入凭据,点击"授权",出现类似"在您的应用程序中打开?"的提示。 我点击是,然后重定向到我的应用程序,在那里我发送下一个带有Gain From Slack代码的请求:

        //AppDelegate.swift
extension UIApplicationDelegate {
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    self.exchangeCodeInURL(codeURL: url as NSURL)
    return true
}

func exchangeCodeInURL(codeURL : NSURL) {
    let clientId = "...myClientId"
    let clientSecret = "...myclientSecret"
    if let code = codeURL.query {
        let request = NSMutableURLRequest(url: NSURL(string: "https://slack.com/api/oauth.access?client_id=(clientId)&client_secret=(clientSecret)&code=(code)") as! URL)
        request.httpMethod = "GET"
        request.setValue("application/json", forHTTPHeaderField: "Accept")
        URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            print(response)
            guard let unwrappedData = data else {return}
            do {
                if let rootObject = try JSONSerialization.jsonObject(with: unwrappedData, options: []) as? NSDictionary {
                    //Save and handle the token
                    print(rootObject)
                }
            }
            catch {
                print(error)
            }
        }).resume()
    }
}

}

代码在Xcode 8测试版中工作,但当我更新到Xcode 8时,从Slack网站重定向后,扩展中的函数不会被调用。

出了什么问题? 是否有更好的方式通过Slack登录过程?

推荐答案

好吧,犯错真的很愚蠢...而非

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool

已弃用

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool

必须实施。

因此这应该在您的AppDelegate

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { //don't miss to implement this!
    return true
}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool{
    self.exchangeCodeInURL(codeURL: url)
    return true
}

func exchangeCodeInURL(codeURL : URL) {
    let clientId = "...myClientId"
    let clientSecret = "...myclientSecret"
    if let code = codeURL.query {
        guard let url = URL(string: "https://slack.com/api/oauth.access?client_id=(clientId)&client_secret=(clientSecret)&(code)") else {return} //as code = "code=Xxxx&state=" you don't have to extract code from string, this query works good
        let request = NSMutableURLRequest(url: url)
        request.setValue("application/json", forHTTPHeaderField: "Accept")
        request.httpMethod = "GET"
        URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
            print(response)
            guard let unwrappedData = data else {return}
            do {
                if let rootObject = try JSONSerialization.jsonObject(with: unwrappedData, options: []) as? NSDictionary {
                    //Save and handle the token
                    print(rootObject)
                }
            }
            catch {
                print(error)
            }
        }).resume()
    }
}

这篇关于松懈登录不起作用。SWIFT 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆