Skip to content

Commit

Permalink
Merge "Prepare Swift app for inclusion in integration guide"
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthecheung authored and Gerrit Code Review committed Jun 4, 2015
2 parents 8aeb66e + 746595b commit 2e4763a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 25 deletions.
47 changes: 44 additions & 3 deletions ios/signin/SignInExampleSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// [START appdelegate_interfaces]
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
// [END appdelegate_interfaces]
var window: UIWindow?

// [START didfinishlaunching]
Expand All @@ -28,15 +30,54 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
if configureErr != nil {
println("Error configuring the Google context: \(configureErr)")
}

GIDSignIn.sharedInstance().delegate = self

return true
}
// [END didfinishlaunching]

// [START openurl]
func application(application: UIApplication,
openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url, sourceApplication: sourceApplication, annotation: annotation)
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
// [END openurl]
}

// [START signin_handler]
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
withError error: NSError!) {
if (error == nil) {
// Perform any operations on signed in user here.
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(
"ToggleAuthUINotification",
object: nil,
userInfo: ["statusText": "Signed in user:\n\(user.profile.name)"])
// [END_EXCLUDE]
} else {
println("\(error.localizedDescription)")
// [START_EXCLUDE silent]
NSNotificationCenter.defaultCenter().postNotificationName(
"ToggleAuthUINotification", object: nil, userInfo: nil)
// [END_EXCLUDE]
}
}
// [END signin_handler]

// [START disconnect_handler]
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
withError error: NSError!) {
// Perform any operations when the user disconnects from app here.
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(
"ToggleAuthUINotification",
object: nil,
userInfo: ["statusText": "User has disconnected."])
// [END_EXCLUDE]
}
// [END disconnect_handler]

}
59 changes: 37 additions & 22 deletions ios/signin/SignInExampleSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import UIKit
// Match the ObjC symbol name inside Main.storyboard.
@objc(ViewController)
// [START viewcontroller_interfaces]
class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
class ViewController: UIViewController, GIDSignInUIDelegate {
// [END viewcontroller_interfaces]

// [START viewcontroller_vars]
@IBOutlet weak var signInButton: GIDSignInButton!
@IBOutlet weak var signOutButton: UIButton!
Expand All @@ -30,45 +31,41 @@ class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
// [START viewdidload]
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().delegate = self

GIDSignIn.sharedInstance().uiDelegate = self
statusText.text = "Initialized Swift app..."
toggleAuthUI()
}
// [END viewdidload]

// [START signin_handler]
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
withError error: NSError!) {
if (error == nil) {
// User Successfully signed in.
toggleAuthUI()
statusText.text = "Signed in user:\n\(user.profile.name)"
} else {
println("\(error.localizedDescription)")
toggleAuthUI()
}
}
// [END signin_handler]
// Uncomment to automatically sign in the user.
//GIDSignIn.sharedInstance().signInSilently()

func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
withError error: NSError!) {
statusText.text = "User disconnected."
// TODO(developer) Configure the sign-in button look/feel
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "receiveToggleAuthUINotification:",
name: "ToggleAuthUINotification",
object: nil)

statusText.text = "Initialized Swift app..."
toggleAuthUI()
// [END_EXCLUDE]
}
// [END viewdidload]

// [START signout_tapped]
@IBAction func didTapSignOut(sender: AnyObject) {
GIDSignIn.sharedInstance().signOut()
// [START_EXCLUDE silent]
statusText.text = "Signed out."
toggleAuthUI()
// [END_EXCLUDE]
}
// [END signout_tapped]

// [START disconnect_tapped]
@IBAction func didTapDisconnect(sender: AnyObject) {
GIDSignIn.sharedInstance().disconnect()
// [START_EXCLUDE silent]
statusText.text = "Disconnecting."
// [END_EXCLUDE]
}
// [END disconnect_tapped]

Expand All @@ -91,4 +88,22 @@ class ViewController: UIViewController, GIDSignInDelegate, GIDSignInUIDelegate {
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self,
name: "ToggleAuthUINotification",
object: nil)
}

@objc func receiveToggleAuthUINotification(notification: NSNotification) {
if (notification.name == "ToggleAuthUINotification") {
self.toggleAuthUI()
if notification.userInfo != nil {
let userInfo:Dictionary<String,String!> =
notification.userInfo as! Dictionary<String,String!>
self.statusText.text = userInfo["statusText"]
}
}
}

}

0 comments on commit 2e4763a

Please sign in to comment.