4

From a previously removed post:

I am struggling to get the Google Drive API to work with Swift, and hoping someone has a suggestion. Here is where I am at so far: I have the Google Drive API installed and working in an Objective-C ...

I am trying to reproduce this example from Google in Swift, but import GTLDrive returns an error in Xcode:

No such module 'GTLDrive.

I am unable to use GTLServiceDrive from the Swift classes. Which combination of CocoaPod + bridging header should I use?

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • 1
    This is a non-trivial question posted 5 hours ago from another users that was removed before it was answered. Solution below. – SwiftArchitect Jul 26 '15 at 20:20

3 Answers3

6

You need 3 things:

(1) Well formed Podfile

platform :ios, '8.0'

target 'GoogleDrive' do
pod 'Google-API-Client/Drive', '~> 1.0'
end

(2) Expose Google API through the bridging headers

#import "GTMOAuth2ViewControllerTouch.h"
#import "GTLDrive.h"

(3) No reference GTLDrive required in the Swift client class

override func viewDidLoad() {
    super.viewDidLoad()
    // ...

    let service:GTLServiceDrive = GTLServiceDrive()
    service.authorizer = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychainForName("Drive API",
        clientID: "YOUR_CLIENT_ID_HERE",
        clientSecret: "YOUR_CLIENT_SECRET_HERE")

    // ...
}
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • Do you know how to solve the issue described here: https://github.com/google/google-api-objectivec-client/issues/103 (without reverting to manual integration - that is, still using "Pods") – emem Mar 15 '16 at 11:13
  • Please post a new question alongside the `Podfile` you are using. You can reference your question in a comment. – SwiftArchitect Mar 15 '16 at 13:13
  • There is already a question regarding this issue: http://stackoverflow.com/q/33581805/2586599 – emem Mar 15 '16 at 14:14
  • Ok, found the answer. Your answer above includes use of 3rd party unofficial pod. See my answer here: http://stackoverflow.com/a/36246255/2586599 – emem Apr 03 '16 at 08:26
2

I just ran into that now in XCode 7.3, and fixed it with the following in the bridging header:

#import <GoogleAPIClient/GTLDrive.h>

Also, if you need the authentication portion:

#import <GTMOAuth2/GTMOAuth2ViewControllerTouch.h>
CodeBender
  • 35,668
  • 12
  • 125
  • 132
1

I threw away Google API Objective C library from Swift altogether and created an interim solution: https://github.com/metaprgmr/GoogleApiProxyForSwift. Incidentally, I used Google Drive as a guinnea pig, where you can find experiments at close to the end of its YouTube presentation. Check it out.