2016-09-16 9 views
0

Swift 3.0を使用してスラックインテグレーションでiOSアプリを開発します。どのように私は、ログインプロセスを渡す:スラック・ログインは機能しません。 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のアプリが開いその後、私は資格入力し、「承認」などの警告持ってタップし、「自分のアプリで開きます?」 私ははいをタップし、スラックコードから得られたと、私は次のリクエストを送信する私のアプリにリダイレクト:

 //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ログインプロセスを渡すより良い方法はありますか?

答えて

0

[OK]を、ミスが代わりに

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

を推奨されていません

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> 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に答えています –

関連する問題