MaxHasADHD / TraktKit

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

Can you please provide some guidelines? #23

Closed Michelasso closed 5 years ago

Michelasso commented 6 years ago

First of all thanks for this great project. At least judging at the code it looks impressive! Still I have problems make it running. The examples in the README.md file aren't working. My biggest issue is with

func application(application: UIApplication, handleOpenURL url: NSURL) -> 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
}

"url.queryDict()" for starting doesn't exist. So I had to make a class extension, which I don't know if does the right thing:

extension URL {
    var queryDictionary: [String: String]? {
        guard let query = URLComponents(string: self.absoluteString)?.query else { return nil}

        var queryStrings = [String: String]()
        for pair in query.components(separatedBy: "&") {

            let key = pair.components(separatedBy: "=")[0]

            let value = pair
                .components(separatedBy:"=")[1]
                .replacingOccurrences(of: "+", with: " ")
                .removingPercentEncoding ?? ""

            queryStrings[key] = value
        }
        return queryStrings
    }
}

Most importantly

func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool

renamed to

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

(since I am using Swift 4.2) never gets called. Is there any change you can provide a Swift 4.0 example or at least some better guidelines/documentation about how to authenticate? Also for macOS, and in case some ideas for tvOS as well (I migrated the framework to tvOS, it compiles and links, but I have no idea about how to start the first communication). There isn't

SFSafariViewController(url: oauthURL)

in either. Thanks!

Michelasso commented 6 years ago

I have managed to launch the Safari view controller. The setClientID in the readme sample had some spaces in them, so the oauthURL was always nil. With this line now it connects to the Trakt.tv login screen:

TraktManager.sharedManager.setClientID(clientID: "Client-ID", clientSecret: "Secret", redirectURI: "Redirect-URI")

I get an oauth error, though after logging in trakt with my Google account (the one I always use).

MaxHasADHD commented 5 years ago

Hey there, so sorry I haven't replied sooner! Just saw this now. I will work on updating the ReadMe to be easier, and I'll look into providing a sample project. You are correct in the first message that queryDict() does not exist. I've been using an extension I found online in my personal project, the important thing is just getting the value of the code parameter passed back. I posted the exact code for the extension I'm using in https://github.com/MaxHasADHD/TraktKit/issues/27

Michelasso commented 5 years ago

All right, thanks. I eventually went forward and managed to make a small (tvOS) app. Then in August I have got stuck as well, due to many personal issues. But I promised myself to get my hands on it again, so once I'll do it I will pull a request for the tvOS port (made by me). That includes an extra module to do the typical Trakt authorisation using a generated 8 chars code (well, to be honest I have stolen it from another Trakt project in Github - forgot its name - and modified it).