Skip to content

Commit 1dc7b46

Browse files
committed
Fix iOS steps
1 parent eea4b88 commit 1dc7b46

File tree

3 files changed

+40
-39
lines changed

3 files changed

+40
-39
lines changed

source/includes/steps-tutorial-ios-swift.yaml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,16 @@ content: |
120120
field for email and password, sign in and sign up buttons, and an activity
121121
indicator to show when the app is handling an authentication request.
122122
123-
First, let's implement the ``signUp()`` method to register a new user:
123+
First, let's implement the ``signUp()`` method to register a new user, which
124+
uses the email/password authentication provider of the Realm app to register a
125+
new user:
124126
125127
.. literalinclude:: /tutorial/generated/code/final/WelcomeViewController.codeblock.sign-up.swift
126128
:language: swift
127129
128-
Now, implement the ``signIn()`` method to log in with an existing user. Once
129-
logged in successfully, open the user realm and navigate to the
130-
ProjectsViewController:
130+
Now, implement the ``signIn()`` method to log in with an existing user using
131+
email/password credentials. Once logged in successfully, open the user realm
132+
and navigate to the ProjectsViewController:
131133
132134
.. literalinclude:: /tutorial/generated/code/final/WelcomeViewController.codeblock.sign-in.swift
133135
:language: swift
@@ -152,6 +154,15 @@ content: |
152154
.. literalinclude:: /tutorial/generated/code/final/ProjectsViewController.codeblock.user-in-realm-notification.swift
153155
:language: swift
154156
157+
Whenever we observe a realm, collection, or object, we receive a notification
158+
token that we must retain as long as we want to keep observing that object.
159+
Once we are done observing, for example because the user navigated to another
160+
screen, we should invalidate that token. One convenient place to do so is in
161+
the deinit method of the view controller:
162+
163+
.. literalinclude:: /tutorial/generated/code/final/ProjectsViewController.codeblock.invalidate-token.swift
164+
:language: swift
165+
155166
Let's provide a way for a user to log out and get back to the
156167
WelcomeViewController. The ``viewDidLoad()`` method hooks up the Log Out
157168
button at the top of the view to the ``logOutButtonDidClick()`` method. We can
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
22
// You always have at least one project (your own)
33
return userData?.memberOf.count ?? 1
4-
}
5-
6-
// :code-block-start: cell-for-row-at
7-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
8-
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
9-
cell.selectionStyle = .none
10-
11-
// User data may not have loaded yet. You always have your own project.
12-
let projectName = userData?.memberOf[indexPath.row].name ?? "My Project"
13-
cell.textLabel?.text = projectName
14-
15-
return cell
164
}

tutorial/swift-ios/Task Tracker/ProjectsViewController.swift

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ class ProjectsViewController: UIViewController, UITableViewDelegate, UITableView
6565
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Log Out", style: .plain, target: self, action: #selector(logOutButtonDidClick))
6666
}
6767

68+
// :code-block-start: log-out-button-did-click
69+
@objc func logOutButtonDidClick() {
70+
let alertController = UIAlertController(title: "Log Out", message: "", preferredStyle: .alert);
71+
alertController.addAction(UIAlertAction(title: "Yes, Log Out", style: .destructive, handler: {
72+
alert -> Void in
73+
print("Logging out...");
74+
// :hide-start:
75+
app.currentUser()?.logOut() { (error) in
76+
DispatchQueue.main.sync {
77+
print("Logged out!");
78+
self.navigationController?.popViewController(animated: true)
79+
}
80+
}
81+
// :replace-with:
82+
// // TODO: log out the app's currentUser, then, on the main thread, pop this
83+
// // view controller from the navigation controller to navigate back to
84+
// // the WelcomeViewController.
85+
// :hide-end:
86+
}))
87+
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
88+
self.present(alertController, animated: true, completion: nil)
89+
}
90+
// :code-block-end:
91+
6892
// :code-block-start: number-of-rows-in-section
6993
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
7094
// :hide-start:
@@ -78,6 +102,7 @@ class ProjectsViewController: UIViewController, UITableViewDelegate, UITableView
78102
// return 0
79103
// :hide-end:
80104
}
105+
// :code-block-end:
81106

82107
// :code-block-start: cell-for-row-at
83108
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
@@ -125,27 +150,4 @@ class ProjectsViewController: UIViewController, UITableViewDelegate, UITableView
125150
}
126151
// :code-block-end:
127152

128-
// :code-block-start: log-out-button-did-click
129-
@objc func logOutButtonDidClick() {
130-
let alertController = UIAlertController(title: "Log Out", message: "", preferredStyle: .alert);
131-
alertController.addAction(UIAlertAction(title: "Yes, Log Out", style: .destructive, handler: {
132-
alert -> Void in
133-
print("Logging out...");
134-
// :hide-start:
135-
app.currentUser()?.logOut() { (error) in
136-
DispatchQueue.main.sync {
137-
print("Logged out!");
138-
self.navigationController?.popViewController(animated: true)
139-
}
140-
}
141-
// :replace-with:
142-
// // TODO: log out the app's currentUser, then, on the main thread, pop this
143-
// // view controller from the navigation controller to navigate back to
144-
// // the WelcomeViewController.
145-
// :hide-end:
146-
}))
147-
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
148-
self.present(alertController, animated: true, completion: nil)
149-
}
150-
// :code-block-end:
151153
}

0 commit comments

Comments
 (0)