Skip to content

levzem/SwiftGmailClient

Repository files navigation

Swift Gmail SDK

A Swift library for interacting with the Gmail API, allowing developers to easily integrate Gmail functionalities into their iOS and macOS applications.

Supported Features

This is still a work in progress and my first Swift project, however, feel free to open an issue and request a feature. They are quick to add.

  • labels
  • messages (partial support)

Installation

You can install the Swift Gmail SDK using the Swift Package Manager. Add the following dependency to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/levzem/SwiftGmailClient", from: "0.1.9")
],
targets: [
    .target(
        name: "YourApp",
        dependencies: ["SwiftGmailClient"]
    )
]

Usage

To use the Swift Gmail client in your application, follow these steps:

  1. Acquire the user's OAuth 2.0 credentials: Before using this client, you will need to ask the user to authenticate your application with their Google account. This typically involves using the Google Sign-In SDK or manually handling OAuth 2.0 flows.

  2. Import the SDK: In your Swift file, import the Swift Gmail module.

    import SwiftGmailClient
  3. Initialize the client: Initialize the client with your user's OAuth 2.0 credentials.

     let gmailService = GmailService(
        credentialsProvider: SimpleCredentialsProvider(accessToken: <ACCESS_TOKEN>)
     )
    let gmailService = GmailService(
       credentialsProvider: RefreshingCredentialsProvider(
             accessToken: <ACCESS_TOKEN>, 
             refreshToken: <REFRESH_TOKEN>, 
             clientID: <OAUTH_CLIENT_ID>,
             clientSecret: <OAUTH_CLIENT_SECRET>
       )
    )
  4. List user's labels: To list a user's email labels, create a ListLabelsRequest object and call the list method.

    let result = await gmail.users.labels.list()
    switch result {
          case .success(let response):
               print("Labels found: \(response.labels)")
          case .failure(let error):
               print("Failed to fetch user's labels: \(error.localizedDescription)")
          }
     }
  5. The call pattern mirrors the Google Gmail REST resources, so you can refer to the Gmail API documentation for more details on available methods and their parameters.

Testing

To run the tests, you can use the following command in your terminal:

swift test

You will need to set up some OAuth credentials. You can do this by creating a .env file and adding the following lines:

GMAIL_OAUTH_ACCESS_TOKEN=<YOUR_ACCESS_TOKEN>
GMAIL_OAUTH_REFRESH_TOKEN=<YOUR_REFRESH_TOKEN>
GMAIL_OAUTH_CLIENT_ID=<YOUR_CLIENT_ID>
GMAIL_OAUTH_CLIENT_SECRET=<YOUR_CLIENT_SECRET>

Alternatively, you can set the environment variables directly in your terminal.

I would like to improve this in the future, but for now, this is the best way to test the library.

About

A lightweight Swift package for seamless Gmail REST API integration with automatic OAuth2 token refreshing.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages