2016-09-16 213 views
0

我使用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登錄過程?

回答

0

好,錯誤是非常愚蠢的......而不是

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() 
    } 
} 
1

您可能需要在將代碼發送到oauth.access GET之前解析代碼。當我在XCode 8和Swift 2.3上運行它時,我的代碼包含「code = Xxxx & state =」。

我正在解決同樣的問題,並感謝您的問題,因爲它幫助我開始了身份驗證過程。

+0

作爲你的建議是很好的漁獲物和幫助解決的問題,都回答upvoted –