MaxHasADHD / TraktKit

Swift wrapper for Trakt.tv API.
MIT License
112 stars 37 forks source link

let queryDict = url.queryDict() is not working #27

Closed dlpigpen closed 5 years ago

dlpigpen commented 5 years ago

I copied and pasted following this code:

 func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        let urlString = url.absoluteString

        let queryDict = url.queryDict() // Parse URL

        if url.host == "auth" {
            if let code = queryDict["code"] as? String { // Get authorization code
                TraktManager.sharedManager.getTokenFromAuthorizationCode(code, completionHandler: nil)
            }
        }
        return true
    }

However the compiler said there is no function name queryDic in url string variable.

gianfrangiamore commented 5 years ago

I'm having the same issue, did you manage to figure out what the issue was?

MaxHasADHD commented 5 years ago

Hey there, sorry about that! The ReadMe is just showing an example of what you should do, queryDict() is an extension I found online awhile ago to parse the URL. You can use the code below for it, or you can do your own parsing. As long as you can get the value of the code parameter.


    func queryDict() -> [String: Any] {
        var info: [String: Any] = [String: Any]()
        if let queryString = self.query{
            for parameter in queryString.components(separatedBy: "&"){
                let parts = parameter.components(separatedBy: "=")
                if parts.count > 1 {
                    let key = parts[0].removingPercentEncoding
                    let value = parts[1].removingPercentEncoding
                    if key != nil && value != nil{
                        info[key!] = value
                    }
                }
            }
        }
        return info
    }
}