我使用Swift 3.0開發了使用Slack集成的iOS應用程序。我如何通過登錄過程:「打開你的應用程序」Slack登錄無效。 Swift 3.0
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瀏覽器的應用程序打開時,我輸入憑據,點擊「授權」,並有類似警報 我點擊yes和重定向到我的應用程序,在那裏我發送一個請求與鬆弛代碼獲得:
//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登錄過程?
作爲你的建議是很好的漁獲物和幫助解決的問題,都回答upvoted –