Writing my first iPhone app – Day 2

I have to say so far I’m liking Swift a lot. It is a nice, straight forward language with some awesome feature like conditionals.

Ok so what I did today? (well yesterday, but whatever)

First of all I added some new views to my interface. I realized that i will need to record the user’s activity on the server so I will need some sort of identifier to them. Of course I could just use the device’s ID but its better to make them create an account and use their login name / user id as the identifier. Anyway, I needed to add a login view as the first responder of my app. For now I will just add a simple button to redirect the flow to the “play” view, will deal with the actual registration later. Also added a new view to my tab controller for editing the user profile / view activity. Here is how my storyboard looks like at the moment:

Screen Shot 2015-05-23 at 21.12.25

The code for my LoginViewController is really just a placeholder now

loginOverride just redirects the user to my tabController where the game code kicks off.

Made some good progress with the game itself as well. Actually pretty much the whole game logic is done. I did set up a fake API for getting the questions. Here is the not so interesting PHP code

Turns out implementing async requests in Swift is fairly simple. Just have to call NSURLConnection.sendAsynchronousRequest and parse the response. 

One thing I’ve learned about this and it is very important: never ever (EVER) try to update UI elements directly from the async request’s callback because it won’t take effect. It took me like half an hour to recognize that my code in fact works, but the UI elements are not updated. Clever people on stackoverflow told me that I should use dispatch_async to with dispatch_get_main_queue() to tell the main execution thread to update the UI.

As for rendering the question, it is pretty straight forward:

I want to allow the user 30 seconds to answer each question. I really hoped that the SDK has some sort of built in timer object, otherwise I should have implemented my own recursive async thing which I really didn’t want to do. Fortunately the SDK in fact has an NSTimer class so implementing the countdown was easy:

The only thing left to do is to hook up the answer buttons and check if the answer is correct. I simply added a tag to each button 0,1,2,3 and linked the touch up event to a new custom action:

Simples. If the clicked button’s tag equals to the one got from the (fake) API display a big green message, otherwise a red one. In the future the app will report the result to the server for logging purposes and of course to move the player on the global board but that is for the future.

Tomorrow I’m gonna start building the server side a little bit and try to make the registration / login view and hopefully add Facebook login too

Be Sociable, Share!

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">