Advanced iOS Features
The course is part of this learning path
This course explores some advanced iOS features. We'll look at how to store data permanently. You'll learn how to work with more than just one screen, how to use Gesture Recognizers, Timers, Alert Messages and so much more. You're going to have essential skills for iOS development after you complete this course.
This course is intended for beginners who want to learn how to build apps using Swift.
To the most out of this course, you should have some basic understanding of programming and computer science in general.
Hi. In this lecture, we're going to complete our Sign Up Registration Form and congratulations on your first Sign Up Registration Form if you manage to do it yourself. If not, that's okay. We're going to do it right now together. So of course, I'm going to use if statements here. I'm going to check if usernameText is blank. So, I'm going to do it like this: if usernameText.text is actually blank.
So, that's all you need to do to check if a string is empty string. So if this is the case, I'm going to show this message as "Username not found!". And in fact, I'm going to comment this out. If you do / and * and if you do //, it will just comment out one line. If you do / and *, it will create another * and / for you. If you do it like this, it will comment out everything inside of that block. So, these are all commented out. This will not be counted as a code. It will be counted as a note. So, I'm going to rewrite everything so that we can practice it. So, the alert is going to be a UIAlertController. I'm going to use the title "Error" and message will be "Username not found". And the style that I want is the Alert. So, I'm going to create okButton, and this will be an Action indeed and the title will be "OK", style will be default and the handler, I'm not going to need any handler so I'm going to say nil. So, alert.addAction(okButton) and self.present(alert. So, this will present an alert and
animated true completion nil. This will present an alert, if only if, usernameText.text isn't given to us. So, that's cool. That's all we have to do, of course, if we're going to continue checking for passwords as well but right now, we've finished it. So, we can actually test this in our simulator and see if this actually works. By the way, I'm using this close as you can see. So, you can close it and open it anytime you want to have more space in your coding section. So, here you go. Without username, if I click on Sign Up, this will be given. If I write any username over here and if I click Sign Up, this won't display me any other messages. So, that's cool. It's working. But I also want to control the passwords.
So, username obviously works. So, if this is the case and we're going display this message, else if passwordText is missing. So, passwordText.text == "". Then, I'm going to display all these messages one more time. So, I'm going to copy and paste it and rather than saying "Username not found", I'm just going to say "Password not found". So, this will be okay. And if that is the case, I'm going to display an alert message, else if. We have to check if passwordText and password2Text match or not. So, I'm going to say else if passwordText.text != password2Text.text. If these two do not match, then I'm going to display another alert message and this will be Password1 and Password2 are different or something, "Passwords do not match". So, that's cool. And else, if everything works out fine, what should we do? If these conditions are not valid, then I'm going to display an alert message saying "Success". And in this message, I'm going to say "User created" or "User OK".
So, this will be our registration form. So, that's cool. Let me run this and try it. So, Username, Username not found. So, let me write another one. So, I'm going to rewrite this as Atil. And I'm going to leave the password blank for this time. So, Passwords not found. Good. So, let me give me a password over here. So, it says that Passwords do not match because I have not written anything in the password2Text. So, let me do everything right. And as you can see it says that User OK, Success. So, this is good. This is working. So, let me try one more time. Let me try without username but with passwords and this work as well. Whenever I do something like this, I'd like to try all the possibilities to see if I'm missing something. And as it seems, it works pretty good to me. Actually that means that we have created our Sign Up Registration App but I'd like to make an improvement in here because maybe, you have realized that we have copied and pasted these codes too many times. So, we are using almost the same codes in every function in every if control. Then, actually we can make this into a more efficient structure.
We can create a function creating user messages, alert messages and we can only change the messages or titles. And I believe we only change messages and for the last alert. We only change title and message. So maybe, we can create a function in which we can ask for the title and the message from the developer, and this function can generate this alert message and show it to the user. It will be much more efficient than writing this or copying and pasting this all the way. So, let me create this function and you will see it better because in real apps also, you will see something like this a lot because we will use alert messages a lot. So, I'm going to say this is going to be a makeAlert function. Of course, name do not matter.
You can choose whatever you want. So, I'm going to copy paste any alert from here. You can take this and you can call this makeAlert and it will display exactly the same message to the user. But we want to change this message and we want to change this title. So, let's get them from the developer. So, I'm going to ask this as a input. So, how do I do it? I'm going to say titleInput and this will be a string and also, I'm going to say messageInput and this will be a string as well. So, rather than saying "Success", I'm going to say titleInput in here and rather than say message, I'm going to say messageInput. So, this will create this UIAlert for me with the title and the message that is given in here. So, when you write makeAlert right now, it will ask for a titleInput and it will ask for a messageInput. So, that will be much better than what we used to have in here. So, we're just going to use these messages. So, let me copy this and let me delete everything in here and say makeAlert rather than saying "User OK". I will say "Passwords do not match". And let me delete everything in here.
So, rather than saying "User OK", I'm going to go for "Password is missing" or "Password not found". And this will be "Error" rather than "Success". RSo, this is "Password not found". And finally, we're going to delete everything in here and use the same function and going to say "Error" and "User not found". So, this is cool. And as you can see, this is much clear. This is much more structured and much more efficient than copying and pasting everything. So, you can use functions efficiently like this. So, when I say Sign Up, it will say Username not found. Let me try the other possibilities as well to see if we broke something. And everything seems to be working right now. So, if I say 1, 2, 3, 4 or 1, 2, 3, 1 over here and say Sign Up. It says Success, User OK. So, that's it. That concludes our alert message lectures. We're going to stop here and we're going to continue learning iOS Advanced Operations in the next lecture.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.