import UIKit
import GoogleSignIn
import Google
class ViewController: UIViewController,GIDSignInUIDelegate, GIDSignInDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let gidSingIn = GIDSignIn()
GIDSignIn.sharedInstance().uiDelegate = self
gidSingIn.delegate = self
GIDSignIn.sharedInstance().delegate = self
var configureError:NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
let button = GIDSignInButton(frame:CGRectMake(0,0,30, 200))
button.center = self.view.center
button.backgroundColor = UIColor.blueColor()
self.view.addSubview(button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func signInGoogle(sender: AnyObject) {
print("pressed")
}
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
withError error: NSError!) {
if (error == nil) {
// Perform any operations on signed in user here.
print(user.userID) // For client-side use only!
print(user.authentication.idToken) // Safe to send to the server
print(user.profile.name)
print(user.profile.givenName)
print(user.profile.familyName)
print(user.profile.email)
print(user.authentication.accessToken)
print(user.profile)
} else {
print("\(error.localizedDescription)")
}
}
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
withError error: NSError!) {
}
} // đây là đăng nhập bằng tài khoản gmail không dành cho googleplus. chỉ cần sao chép và quá khứ trong bộ điều khiển của bạn. và thêm func sau trong appdelegate Class
func application(application: UIApplication,
openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
var options: [String: AnyObject] = [UIApplicationOpenURLOptionsSourceApplicationKey: sourceApplication!,UIApplicationOpenURLOptionsAnnotationKey: annotation]
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
Nguồn
2016-07-28 12:44:15
Có may mắn không? Tôi không thể làm việc này .. – Chicken
Tôi cũng đang gặp vấn đề tương tự, nếu bạn có câu trả lời đó, vui lòng cho tôi biết cách giải quyết. –