2016-09-16 7 views
0

Swift 3.0을 사용하여 Slack 통합으로 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) 
} 

그리고 사파리 응용 프로그램은 내가 자격 증명을 입력 열립니다 탭 "권한 부여"와 같은 경고가를 나는 네 누르고 내가 멋대로 코드에서 얻은 함께 다음 요청을 보내 내 앱에 리디렉션 :

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

} 코드는 엑스 코드 8 베타에서 일하지만,

나는 엑스 코드로 확장 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에 답 풍어와 문제를 해결하는 데 도움이,이므로 –

관련 문제