A wrapper around the Google Photos API.
I wanted to consume the Google Photos API in Swift but at the time of writing there is no framework that does it in a simple way.
So why not share my own take?
List of implemented methods:
[x] Authentication
[x] Albums
[x] Shared albums
[x] MediaItems
To run the example project, clone the repo, and run pod install
from the Example directory first.
To install through CocoaPods add pod 'GPhotos'
to your Podfile and run pod install
.
Setup a OAuth 2.0 client ID as described here, download the .plist
file and add it to the project.
In AppDelegate.swift
configure GPhotos when the application finishes launching
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
var config = Config()
config.printLogs = false
GPhotos.initialize(with: config)
// Other configs
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let gphotosHandled = GPhotos.continueAuthorizationFlow(with: url)
// other app links
return gphotosHandled
}
GPhotos.isAuthorized
will return true if a user is already authorized.
GPhotos.logout()
clears the session information and invalidates any token saved.
GPhotos.authorize(with scopes:)
by default starts the authentication process with openid
scope. Will be executed only if there are new scopes. As per Google recommendation, you should gradually add scopes when you need to use them, not on the first run. The method will return a boolean indicating the success status, and an error if any.
GPhotos.switchAccount(with scopes:)
by default starts the authentication process with openid
scope. Will ignore current authentication scopes. The method will return a boolean indicating the success status, and an error if any.
Save an instance of GPhotosApi
to be able to use pagination between different features.
create(album:)
returns the newly created album objectlist()
loads sequential pages of items every time it is called.reloadList()
loads always the first page.get(id:)
returns the Album
for the provided id.share(id
)
shares an album with the provided id with possible options and returns the ShareInfo
of the album.unshare(id:)
returns a boolean indicating the success status.addEnrichment(id
position)
Adding or removing items from an album only requires the set of media items ids.
addMediaItems(id
)
removeMediaItems(id
)
list()
and reloadList()
have the same use as in Albumsget(id:)
returns the MediaItem
for the provided id.getBatch(ids:)
returns the MediaItems
for the provided array of ids.search(with request:)
loads sequential pages of items every time it is called. Results are based on filters in the request. If no filters are applied it will return the same results as list()
reloadSearch(with request:)
loads always the first page.upload(images:)
takes an array of UIImage
and uploads them one by one to the user’s library. These uploads will count towards storage in the user’s Google Account.get(token:)
returns the details of the shared album.join(token:)
and leave(token:)
take the sharing token as argument and either join or leave the shared album if the token is correct.list()
and reloadList()
have the same use as in AlbumsGPhotos is available under the MIT license. See the LICENSE file for more info.