Reputation: 101
Am developing a app using swift in my app a user can login using Google integration method now I want to use API to get user id.
Code in my appdelegate:
import UIKit
import CoreData
import IQKeyboardManagerSwift
import Alamofire
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate {
var window: UIWindow?
var centerContainer: MMDrawerController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
IQKeyboardManager.sharedManager().enable = true
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
if (configureError != nil){
print("We have an error:\(configureError)")
}
GIDSignIn.sharedInstance().clientID = "12345678.apps.content.com"
GIDSignIn.sharedInstance().delegate = self
return true
}
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!) {
if (error == nil){
print("yes user signed in:\(user)")
print(user.profile.name)
print(user.profile.email)
print(user.userID)
let profilePicURL = user.profile.imageURLWithDimension(200).absoluteString
print(profilePicURL)
let mainstoryboard = UIStoryboard(name: "Main", bundle:nil)
let centerViewController = mainstoryboard.instantiateViewControllerWithIdentifier("FourthViewController") as! FourthViewController
let leftViewController = mainstoryboard.instantiateViewControllerWithIdentifier("LeftSideViewController") as! LeftSideViewController
leftViewController.ProName = user.profile.name
leftViewController.proImage = profilePicURL as String
let leftSideNav = UINavigationController(rootViewController: leftViewController)
let centerNav = UINavigationController(rootViewController: centerViewController)
self.centerContainer = MMDrawerController(centerViewController: centerNav, leftDrawerViewController: leftSideNav)
self.centerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.PanningCenterView;
self.centerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView;
self.window!.rootViewController = self.centerContainer
self.window!.makeKeyAndVisible()
}
else{
print("looks we got signin error:\(error)")
}
}
func application(application: UIApplication,
openURL url: NSURL, options: [String: AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as? String,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
}
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
withError error: NSError!) {
// Perform any operations when the user disconnects from app here.
// ...
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}
Now in the method func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, withError error: NSError!)
I want to use a API key; if I use it says fatal error.
What I tried:
In that
func signin(signIn: GIDSignIn)method{
Alamofire.request(.GET,"http://www.someethig.com.php?emailid="+user.profile.email+"", parameters: nil, encoding: .URL, headers: nil).response { (req, res, data, error) -> Void in
let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
print(dataString)
let json = JSON(data: data!)
let userid = (json["success id"])
not even printing any data.
Upvotes: 0
Views: 906