问题
In an iOS app I'm developing, I need to add Google+ sign-in feature. The app is developed in Swift.
I added the GooglePlus iOS SDK through CocoaPods. Please note that I have CocoaPods v0.36 beta installed which officially supports Swift. I've integrated many Objective-C libraries with Swift projects this way before.
The pod installation was successful. Then I needed to import the framework to files where I wanted to use its methods so I added the below lines to the top of the file.
import GooglePlus
import GoogleOpenSource
But when I build the project I get the following errors.
No such module 'GooglePlus'
No such module 'GoogleOpenSource'
What am I doing wrong here? Am I specifying the correct names?
Edit: I'm aware of using the bridging header. But the latest CocoaPods supports adding Obj-C libraries to projects. It automatically converts them to frameworks so that you could use them the Swift way. I've done it before with some other Obj-C libraries without the bridging header. It fails with Google+'s iOS SDK.
回答1:
You'll need to create a bridging header where you'll import the Objective-C classes that you want to expose to Swift. You can see the documentation on how this works here.
The easiest way to create one is to create a Objective-C class in your project. Then Xcode will ask if you'd like to create a bridging header, just say Yes and then delete the class you created.
In this header import the necessary Google frameworks, then you'll have access to those classes in Swift.
回答2:
If you haven't already, setup your application in the Google developer console.
If you want to configure using Cocoapods, you just need to produce a Podfile:
target 'swiftsignin' do
pod 'googleplus-ios-sdk'
end
Then run pod install
from the folder containing your project and open the generated project.xcworkspace.
If you are configuring using just the SDK, download the Google+ SDK and include it in your project.
The first part of this is to force the creation of a bridging header by adding an Obj-C file to your project. Within the bridging header, include the Google+ libraries:
// example bridging header...
#ifndef swiftsignin_Bridging_h
#define swiftsignin_Bridging_h
#import <GooglePlus/GooglePlus.h>
#import <GoogleOpenSource/GoogleOpenSource.h>
#endif
Next, somewhere in your code, e.g. the ViewController, configure the Sign-in object:
// Configure the sign in object.
var signIn = GPPSignIn.sharedInstance()
signIn.shouldFetchGooglePlusUser = true
signIn.clientID = kClientId
signIn.shouldFetchGoogleUserEmail = toggleFetchEmail.on
signIn.shouldFetchGoogleUserID = toggleFetchUserID.on
signIn.scopes = [kGTLAuthScopePlusLogin]
signIn.trySilentAuthentication()
signIn.delegate = self
Naturally, kClientId
needs to be defined somewhere to point to your client ID.
In your app delegate, you will need to add an open URL handler that accepts the authorization code when the OAuth 2.0 auth steps complete and passes it to the authorization library:
// Added to handle the Authorization code returned from sign-in.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return GPPURLHandler.handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
}
In your app info.plist, add a URL handler that has the scheme and identifier set to your app's bundle name.
I have created a demo Google+ Sign-in Swift app on GitHub that might be helpful for you.
来源:https://stackoverflow.com/questions/28130394/google-plus-with-cocoapods-in-swift