Writing my first iPhone app – Day 3 – Validating forms is no fun in any language

Yesterday I was quite busy boxing up my apartment for next week’s move (fuck i hate moving) so relatively small progress on the app, but the login / registration screen is getting there

Created two new views, one for login and one for registration obviously and linked them to LoginViewController and RegisterViewController. Also renamed my home screen’s controller to be HomeViewController (very imaginative naming, I know)

The bottom buttons are there to switch views between register and login + I still have my “let me in” button for entering directly to the Play view for testing purposes (really need to add a TODO to remove that from the final product at some point). The code for switching the views looks like this

As for the login and registration code: Why is that that in any language the most boring and tedious thing to do is form validation? Wish someone would come up with a sure way of doing it. Anyway, the code for my register form looks like this. Fokin’ boring

Swift

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

funcisValidEmail(testStr:String)->Bool{

// println("validate calendar: \(testStr)")

letemailRegEx="[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,8}"

letemailTest=NSPredicate(format:"SELF MATCHES %@",emailRegEx)

returnemailTest.evaluateWithObject(testStr)

}

funcisValidUsername(testStr:String)->Bool{

// println("validate calendar: \(testStr)")

letuserRegEx="^[a-zA-Z_0-9]{5,}$"

letuserTest=NSPredicate(format:"SELF MATCHES %@",userRegEx)

returnuserTest.evaluateWithObject(testStr)

}

@IBAction funcdoRegister(sender:AnyObject){

varemail=self.emailInput.text

varusername=self.usernameInput.text

varpasswd=self.passwordInput.text

varhasError=false

if(email==""){

emailErrorLabel.text="Please enter your email address"

emailErrorLabel.hidden=false

hasError=true

}elseif(!self.isValidEmail(email)){

emailErrorLabel.text="Invalid email address"

emailErrorLabel.hidden=false

hasError=true

}else{

emailErrorLabel.hidden=true

}

if(username==""){

usernameErrorLabel.text="Please pick a username"

usernameErrorLabel.hidden=false

hasError=true

}elseif(count(username)30){

usernameErrorLabel.text="Username must be between 5 and 30 characters"

usernameErrorLabel.hidden=false

hasError=true

}elseif(!self.isValidUsername(username)){

usernameErrorLabel.text="Username can only contain alphanumeric characters"

Couldn’t find any built in methods to validate emails so had to use regex (and not a very good one) to validate it on the front end. Obviously will need to do the same on the server side as well. The code I think is pretty self-explanatory. Checking for certain field values and displaying error messages depending on what rules they break. If no error detected do an async request to the server and check it’s response. Since the API is not yet ready I’m currently only checking for valid status.

I also implemented (partially) Facebook connect which was surprisingly easy. Since I couldn’t find a straight forward step by step guide to implement it in Swift here it is for you:

The guide tells you to set up a new Facebook App. The setup requires a valid App Store ID which you might not have. To trick it I just used a random app’s ID. To get the id you just copy any app’s url and get the relatively long number from the end of the link.

Add the SDK to your building targets. Click on the top element in your file manager in Xcode and open the Build Phases tab. Then in the “Link Binary with Libraries” section add the following three frameworks: FBSDKLoginKit, Bolts, FBSDKCoreKit

The SDK is written in Objective-C so you need to create a “bridging header” to use it in Swift. To do so create a “Bridging-Header.h” file in your project’s main folder with the following content:

C

1

2

#import <FBSDKCoreKit/FBSDKCoreKit.h>

#import <FBSDKLoginKit/FBSDKLoginKit.h>

Click on the top element in your file browser and select “Build settings”. Make sure “All” is selected in the top menu. Scroll down to “Swift compiler – Code generation” and add “YourProjectname/Bridging-Header.h” as the “Objective-C bridging header”

Try building the project to make sure everything is linked correctly.

Using the Facebook SDK to implement FB Login is straight forward. First of all you need to modify your view class definition a bit.

This code basically adds a new subview to the current view, which contains the login button. At this point I have literally no idea how to style the button itself. It just shows up right in the middle of the screen, which is currently fine with my crappy interface but I can see it being a pain in the future. This is how it looks atm:

So thats it for today. Tomorrow I’m gonna try and create the server side API and hook up those forms / FB login button. Happy bank holiday Monday everyone