Skip to content

Instantly share code, notes, and snippets.

@kgleong
kgleong / UIViewControllerTransitioningDelegate.swift
Last active December 29, 2018 03:22
UIViewControllerTransitioningDelegate
extension MyViewController: UIViewControllerTransitioningDelegate {
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
}
@kgleong
kgleong / UIViewControllerAnimatedTransitioning.swift
Last active December 29, 2018 05:16
UIViewControllerAnimatedTransitioning
extension MyViewController: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 2.0
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// Retrieve the view controllers participating in the current transition from the context.
let fromView = transitionContext.viewController(forKey: .from)!.view!
let toView = transitionContext.viewController(forKey: .to)!.view!
@kgleong
kgleong / GoogleSignInAppDelegate.swift
Last active December 31, 2018 04:22
Google Sign In App Delegate - Client ID
import GoogleSignIn
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GIDSignIn.sharedInstance()?.clientID = "769307225775-llbr5f6avrd35u7rpto9p700p2g900qr.apps.googleusercontent.com"
return true
}
}
@kgleong
kgleong / GoogleSignInAppDelegate_OpenURL.swift
Created December 31, 2018 04:28
Google Sign In App Delegate Open URL
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let sourceApplication = options[.sourceApplication] as? String
let annotation = options[.annotation]
return GIDSignIn.sharedInstance()?.handle(url, sourceApplication: sourceApplication, annotation: annotation) ?? false
}
@kgleong
kgleong / GSignIn_VC_AddButton.swift
Last active December 31, 2018 05:03
Google Sign In - View Controller - Add Sign In Button
import GoogleSignIn
class ViewController: UIViewController {
let googleSignInButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(googleSignInButton)
googleSignInButton.setTitle("Google Sign In", for: .normal)
@kgleong
kgleong / GoogleSignIn_VC_Delegates.swift
Last active December 31, 2018 05:34
Google Sign In - View Controller - Delegate Conformance
extension ViewController: GIDSignInDelegate, GIDSignInUIDelegate {
// MARK: - GIDSignInDelegate
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
// A nil error indicates a successful login
googleSignInButton.isHidden = error == nil
}
}
@kgleong
kgleong / GoogleSignInViewController_Singleton.swift
Last active December 31, 2018 06:55
Google Sign In - View Controller - Configure GIDSignIn singleton
import GoogleSignIn
class ViewController: UIViewController {
// ...
override func viewDidLoad() {
super.viewDidLoad()
/***** Configure Google Sign In *****/
@kgleong
kgleong / DriveViewController_Scopes.swift
Last active January 1, 2019 01:20
Google Drive - View Controller - Add Scope
import GoogleSignIn
import GoogleAPIClientForREST
import GTMSessionFetcher
class ViewController: UIViewController {
let googleDriveService = GTLRDriveService()
override func viewDidLoad() {
// ...
GIDSignIn.sharedInstance()?.scopes = [kGTLRAuthScopeDriveFile]
@kgleong
kgleong / GoogleDriveViewController_Authorizer.swift
Last active January 1, 2019 01:35
Google Drive - View Controller - Authorizer
import GTMSessionFetcher
extension ViewController: GIDSignInDelegate, GIDSignInUIDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
// A nil error indicates a successful login
if error == nil {
// Use the authenticated user's credentials when querying the Google Drive API.
self.googleDriveService.authorizer = user.authentication.fetcherAuthorizer()
} else {
self.googleDriveService.authorizer = nil
@kgleong
kgleong / GoogleDriveViewControllerAuthorizer.swift
Last active January 19, 2019 17:13
Google Drive - Set Authorizer and User
import GTMSessionFetcher
extension ViewController: GIDSignInDelegate, GIDSignInUIDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
// A nil error indicates a successful login
if error == nil {
// Include authorization headers/values with each Drive API request.
self.googleDriveService.authorizer = user.authentication.fetcherAuthorizer()
self.googleUser = user
} else {