Yurii Koval Yurii Koval - 2 months ago 12
iOS Question

Slack Login does't work. Swift 3.0

I develop iOS app with Slack integration using Swift 3.0. How I pass login process:

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


Then Safari app opens, I enter credential, tap "Authorize" and have alert like "Open in your app?"
I tap yes and redirecting to my app, where I send next request with gained from Slack code:

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


}

Code worked in Xcode 8 beta, but when I updated to Xcode 8 functions in extension are not called after redirect from Slack website.

What is wrong?
Is there a better way to pass Slack login process?

Answer

Ok, mistake is really stupid... Instead of

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

which is deprecated

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

has to be implemented.

So this should be in your 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()
    }
}
Comments