Understanding Key-Value Observing and Coding

In programming, one of the most commonly accepted facts is that the flow of a program depends on the value of the various variables and properties you use. You just have to think how many times during an application development you have to check for your properties’ values, and based on them to drive the execution of the code to one or another way. Even more, think of how many times you need to check if an object has been added to an array, or if it has been removed from it. So, being aware of the changes that happen to the properties of your classes is a vital part of the programming process.

There are various ways that let us be notified when a property gets changed. For example, if you have a method to set a property’s value, or you just override a setter method, you can send a notification (NSNotification) to notify an observer about that change, and then perform the proper actions based on the value that was set. If you are familiarized with notifications, then using them would not be a problem at all if you would want to use them for being aware about changes in properties, but the truth is that doing so for a great number of properties would require to send and observe for a great number of notifications as well, and that could lead to a lot of extra code writing.

kvo-featured

Nevertheless, there is a much better way to observe for changes in properties, and Apple uses it a lot in its own software. That way is not used a lot by programmers because it seems hard to be learnt, however this is not true. Once you get to know it, you’ll probably love it and you’ll see that it’s much effortless and easier to track down changes on single properties or collections, such as arrays. This method is called Key-Value Observing, but is mostly known as KVO.

Key-Value Observing (KVO) is related directly to another powerful and important mechanism, named Key-Value Coding, or KVC. Actually, any property you want to observe for changes must be Key-Value Coding compliant, but we will talk more about that later. The important thing for now is to make clear that both of them provide a really powerful and efficient way to write code, and knowing how to handle them can definitely turn you into a more skilled and advanced programmer.
[Read more...]

Understanding XML and JSON Parsing in iOS Programming

One of the most important tasks that a developer has to deal with when creating applications is the data handing and manipulation. Data can be expressed in many different formats, and mastering at least the most known of them consists of a key ability for every single programmer. Speaking for mobile applications specifically now, it’s quite common nowadays for them to exchange data with web applications. In such cases, the way that data is expressed may vary, but usually is preferred either the JSON or the XML format.

iOS SDK provides classes for handling both of them. For managing JSON data, there is the NSJSONSerialization class. This one allows to easily convert a JSON data into a Foundation object (NSArray, NSDictionary), and the other way round. For parsing XML data, iOS offers the NSXMLParser class, which takes charge of doing all the hard work, and through some useful delegate methods gives us the tools we need for handling each step of the parsing.

json-xml-demo-featured

[Read more...]

Introduction to Auto Layout

Editor’s note: If you’ve downloaded the Xcode 6 beta and played around with it, one thing you may notice is the change of Interface Builder. The default view controller is now wider and doesn’t look like an iPhone 5. When you position a button in the center of the view and run the app, it doesn’t look good. The button is not centered properly.

What’s wrong? How can you make it right? The answer is Auto Layout. Auto Layout is a constraint-based layout system. It allows developers to create an adaptive interface that responds appropriately to changes in screen size and device orientation. We seldom talk about Auto Layout in our tutorials. Some beginners find it hard to learn and avoid using it. Starting from Xcode 6, you should learn to love Auto Layout. Apple is expected to release 4.7-inch and 5.5-inch iPhone 6 this fall. Without using Auto Layout, it would be very hard for you to build an app that supports all screen sizes.

auto-layout-featured

So starting from this week, we’ll publish a series of articles about Auto Layout. We’ll start with the basics.

Enter the introduction of Auto Layout by Ziad.

I know that there are many developers who hates Auto Layout, maybe because it’s relatively new or it’s hard to use for the very first time. But believe me, once you get comfortable with it, it becomes one of your greatest tools that you can’t live without when developing your app. In this tutorial, I will give you a very brief introduction of Auto Layout.
[Read more...]

Introduction to UIActionSheet and UIPopoverController

All mobile applications, no matter what they are about, they have one common and obvious characteristic: They offer interaction, which means that they are not static, but require input or actions needed to be taken by users from time to time in order to function properly. One quite usual behavior, is the ability to provide ways that allow users to make choices or take decisions where it’s needed, and ultimately act that way on the applications’ data or functionalities. iOS SDK provides a pre-made action-taking view controller, the UIActionSheet.

Action sheets consist of a really convenient and fast way to display a bunch of options that the user should select from, and it is widely used in great number of applications. Its disadvantage though is that it adopts the iOS’s look and feel without being able to be graphically modified, therefore it might not fit to applications with customized GUI.

Beyond action sheets, another very important and cool tool designed especially for the iPad, is the UIPopoverController. A popover controller is actually a container which is used to display content on top of another content. What makes it unique is the fact that it can be displayed almost everywhere around the screen of the iPad, but usually it’s shown near to the button that caused its appearing with a nice arrow pointing to the button’s direction. iOS by default encloses action sheets in popover controllers on iPad, but they can also be manually created for custom content.

uiactionsheet-featured

Before we begin working with those two view controllers, let’s do some more talking about them in order to get to know their basics, and then we’ll proceed on how to use them in projects. So, let me start by telling that both the UIActionSheet and UIPopoverController exist to serve the same purpose, and that is to provide a quick way for presenting content to the user. This content can be either a button-based menu (action sheet), or anything else originating from a view controller (popover controller). Undoubtably, they are both pretty useful tools, and everyone developing for iOS should master both of them. Their great difference lies to where each one is used, and that’s the essential fact you should be aware of: An action sheet can be used on any iOS device (iPhone, iPad), but a popover controller can be used only on iPad, as I have already said.
[Read more...]

iOS Programming 101: How To Create Swipeable Table View Cell to Display More Options

When iOS 7 was first released, one of the many visual changes that particularly interested me was the swipe-to-delete gestures in the Mail app. By now you should be very familiar with the feature. After you swipe a table cell, you’ll see the Trash button, plus a new button named More. The More button will bring up an action sheet that shows a list of options such as Reply, Flag, etc.

I thought it’s a great feature to provide additional options for manipulating a table record. However, as you know, Apple didn’t make this feature available to developers in iOS 7. You can only create the swipe-to-delete option in table cell. The More feature is only limited to the stock Mail app. I have no idea why Apple keeps such great feature to its own app. Fortunately, some developers have created free solutions (such as UITableView-Swipe-for-Options, MCSwipeTableViewCell) and made them available freely.

In this tutorial, I’ll use SWTableViewCell and see how to implement swipe-to-show-options feature in your app. SWTableViewCell is pretty to easy to use. If you understand how to implement UITableView, you shouldn’t have any problem with SWTableViewCell. On top of that, it supports utility buttons on both swipe directions. You’ll understand what it means in a minute.

Swipeable UITableViewCell

Let’s get started and build our demo app.
[Read more...]

Understanding Git Source Control in Xcode

During an application development process, a quite significant part is the way developers manage to keep track of the changes been made over time. It really consists of a necessary need to be able to store and handle copies of working code versions in various stable stages, and revert back to them when bugs or problems arise. Even more, when a number of programmers work at the same project, keeping track of all changes is a one-way path. Fortunately, developers don’t have to discover their own way to do all that, as there are special software solutions, called Version Control Systems.

A version control system, or in other words a revision control system, is actually a (software) mechanism that is capable of monitoring changes performed to code files over time and storing them for future reference. Besides that, it can also save extra essential data, such as the developer who made the changes, when did they happen, what was actually modified, and other kind of historical and not only data. Moreover, such a system provides the ability to compare various versions of the code, revert to a previous version of either specific files or a whole project if needed, and eventually implement a bug-free product by tracking down any malicious code.

git-source-code

Using a version control system, developers can work on different paths of a project, commonly named branches, and when their piece of project is ready, everything is put together so the final release of the application can be built. This process is called merging, and it consists of a special characteristic of version control systems. Actually, that fashion of work is mandatory in teams of developers and software companies, where each one if responsible for a project’s part, and at the end everything must be gathered up and put in one place.

For single developers it is not required to use a version control system, however is highly recommended, as with it’s much more easier to track down bugs or going back to stable and working versions of code, once a dead end appears or for some reason everything is messed up. The truth is that a great number of lone-rider programmers, especially the new ones, do not use such a system at all, and is common to manually keep copies of their projects when they are about to add new features or generally modify them. That’s a really bad habit, as a source control system does all that much better and efficiently, providing at the same time all the extra capabilities previously described.
[Read more...]

First Time App Developer Success Stories Part 2: From Zero iOS Programming Experience to Launching Their First Apps

You may have read some app millionaire stories that earn tons of money overnight. But you rarely find stories about app developers who are less successful. Recently I reached out to a number of first time app developers and asked them to share their app development experience. To me their stories are equally amazing and I have learnt so much from them. These developers are all started from zero iOS programming experience and end up with their apps launched on App Store. A couple weeks ago I shared the first part of the success stories. Here comes to the part 2.

Some people think it’s hard to create their first apps because they get a big idea. Most of them only see the “million-dollar ideas” and want to hit big when the app launches. That’s unreal and you’ll easily get frustrated if your expectation is too high. The truth is you don’t need to have a grand idea and set high expectation when first started. The developers featured in this post just started small and built their first apps around a simple idea they cared about, be it a recipe app or a game book.

Once again, I’m really proud to showcase their works. Enjoy their stories and app development experience. If you’re still struggled about how to learn iOS programming and make your first app, you’ll probably inspired by their stories.

app-developer-part-2
[Read more...]

iOS Programming 101: How To Create Circular Profile Picture and Rounded Corner Image

One of the changes in iOS 7 is that it favors the use of circular image over square image. You can find circular icons or images in stock apps such as Contacts and Phone. In this short post, we’ll explore the CALayer class and see how you can apply it to create circular image or image with rounded corner.

You may not heard of the CALayer class. But you should have used it in some ways if you’ve built an app. Every view in the UIKit (e.g. UIView, UIImageView) is backed by an instance of the CALayer class (i.e. layer object). The layer object is designed to manage the backing store for the view and handles view-related animations.
The layer object provides various attributes that can be set to control the visual content of the view such as:

  • Background color
  • Border and border width
  • Shadow color, width, etc
  • Opacity
  • Corner radius

The corner radius is the attribute that we’ll use to draw rounded corner and circular image.

Circular Image using calayer

As always, the best way to understand how CALayer works is to use it. We’ll create a simple profile view with a circular profile photo.

[Read more...]

Background Transfer Service in iOS 7 SDK: How To Download File in Background

In a previous tutorial I presented a specific new multitasking feature in iOS 7, the Background Fetch, showing how easy it is to make an app to schedule downloads in the background. In this tutorial, I am going to work with another great multitasking feature, named Background Transfer Service.

Prior to iOS 7, only a few kinds of application were allowed to download resources or content on the background while they would not run, and just for a limited time. Big downloads should actually occur while the app was in the foreground, and that was a hard fact for all developers. However, things changed in iOS 7 with the Background Transfer Service coming, as it totally eliminates all the limitations presented above. Not only every app can download content while it’s not running, but it can also have as much time as it’s required at its disposal until all downloads are over.

Great flexibility and more power comes when the Background Transfer Service is combined with other multitasking features, such as the Background Fetch. For example, using the Background Fetch an app can schedule and initiate a download in the limited time that has at its disposal, and then using the Background Transfer Service to perform the actual data downloading.

Background Transfer Service Tutorial

When the Background Transfer Service gets in action, what is actually happening is that the operating system takes charge of all the download process, performing everything in background threads (daemons). While a download is in progress, delegates are used to inform the app for the progress, and wakes it up in the background to get more data if needed, such as credentials for logging in to a service. However, even though everything is controlled by the system, users can cancel all downloads at any time through the application.

Many times, the Background Transfer Service is synonymous with a new API introduced in iOS 7, the NSURLSession. This class actually replaces the NSURLConnection which was used until iOS 6, providing more features, flexibility and power when dealing with online content. With NSURLSession, three types of actions are allowed: File downloading and uploading, and data fetching (for instance, HTML or JSON). To communicate with online servers, it uses the HTTP (and HTTPs) protocol.
[Read more...]