Passing user information to ExploreViewController

You can now sign in successfully and the user's information is in your app. To display the user's given name in ExploreViewController, you need to implement the prepare(for:) method in SignInViewController. This is so you can pass the user's given name to ExploreViewController and use it to set the value of the large label in headerView. Follow these steps to do so:

  1. Click on ExploreViewController.swift (inside the Explore folder) in the Project navigator.
  2. Add a property just below the class declaration:
var givenName:String?
  1. Modify collectionView(_:viewForSupplementaryElementOfKind:at:) so that it prints givenName to the Debug area, as follows:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
headerView = header as? ExploreHeaderView
if let givenName = givenName {
print("Hi, (givenName)")
}
return headerView
}
  1. Click on SignInViewController.swift (inside the Misc folder) in the Project navigator.
  2. Implement prepare(for:) like so (the boilerplate code should already be in the file; if it isn't, just type the following code before the MARK: - SignInWithApple methods):
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let tabBarController = segue.destination as? UITabBarController, let navViewController = tabBarController.viewControllers?[0] as? UINavigationController, let exploreViewController = navViewController.viewControllers[0] as? ExploreViewController {
exploreViewController.givenName = appleIDGivenName
}
}

This will set the value of givenName in ExploreViewController to appleIDGivenName.

  1. Build and run the app and tap the Sign in with Apple button. 
  2. Repeat the same steps you performed earlier to sign in (step 11 - 14 in the previous section, Implementing delegate methods and button actions). The Explore screen will appear.
  1. You will see the following in the Debug area, indicating that the user's given name has been successfully passed to ExploreViewController:

Now, all you have to do is assign the given name to the EXPLORE label's text property. Follow these steps to do so:

  1. Click on ExploreHeaderView.swift (inside the View folder in the Explore folder) in the Project navigator.
  2. Add a property just below the lblLocation property:
@IBOutlet weak var mainlbl:UILabel!
  1. Click on Main.storyboard and select Explore Header View in the document outline. Click the Connections inspector. Drag from the mainlbl outlet to the EXPLORE label:

  1. Confirm that the mainlbl outlet has been connected:

  1. Click on ExploreViewController.swift in the Project navigator.
  2. Modify collectionView(_:viewForSupplementaryElementOfKind:at:) in order to assign the given name to mainlbl:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
headerView = header as? ExploreHeaderView
if let givenName = givenName {
headerView.mainlbl.text = "Hi, (givenName)"
}
return headerView
}
  1. Build and run the app and tap the Sign in with Apple button. 
  2. Repeat the same steps you performed earlier (step 11 - 14 in the previous section, Implementing delegate methods and button actions). The Explore screen appears with the user's given name displayed in the header, as shown in the following screenshot:

You have successfully implemented Sign in with Apple for your app. Excellent!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.211.66