Search code examples
gmail-apiios11swift4xcode9

iOS Gmail API - Sample code to retrieve Gmail messages for a given label in Swift 4 / Xcode9


first question for me on stackoverflow.com so hopefully I am doing it correctly!

I currently have the following code (Xcode 9 / Swift 4) using the example provided in the Gmail API web site which works fine:

let query = GTLRGmailQuery_UsersMessagesList.query(withUserId: "me")
service.executeQuery(query,
                         delegate: self,
                         didFinish: #selector(displayResultWithTicket2(ticket:finishedWithObject:error:))

However instead of retrieving all emails for "me", I just want to retrieve the emails for a given label. Google has an example here in its API reference where one of the parameters can be a label but unfortunately I cannot find the equivalent Swift code. They "only" cover: Java, .Net, Php, Python and Javascript.

My question is about: how do you code this in Swift? The method above "GTLRGmailQuery_UsersMessagesList.query" only accepts a User Id.

Alternatively the example mentions the use of HTTP request and parameters (in particular "q"), can I use that in Swift and how?

Thanks!


Solution

  • Oh my, it was soooo simple! After the first line of code (let query...), you just need to call the properties of "query", e.g.

    let query = GTLRGmailQuery_UsersMessagesList.query(withUserId: "me")
    query.q = "is_unread"
    service.executeQuery(query,
                             delegate: self,
                             didFinish: #selector(displayResultWithTicket2(ticket:finishedWithObject:error:))
    

    Lots to learn for me! But hopefully that will be useful to someone!