BuySellAds.com

My book Barcodes with iOS is nearing completion. Buy it now to get early access!
Our DNA is written in Objective-C
Jump

Category Archive for ‘Recipes’ rss

Determining the Hardware Model

Not all Apple Hardware is created equal. For some apps you need to distinguish between different models of iPhones and iPod Touch. But when you query the model property of UIDevice you don’t get enough information to really know what hardware your app is running on.

With a little bit of googling I found an article on Ars Technica explaining how the pros are getting the kind of information that we regular dev guys dream of having at our fingertips. It basically uses barely documented system calls to get the infamous model string from which you can infer the real device model.

We are talking about sysctlbyname which Apple even documents as a standard C library function call to “get or set system information”. iPhone Dev crack Erica Sadun made this UIDevice category extension. We are inspecting it with awe as we see the magic unfold.

Read more

__MyCompanyName__ Treats Warnings as Errors

I finally figured out three XCode settings that I’ve been dreading to change for quite a while now. See if you didn’t think of changing them yourself as well, but could not be bothered to Google them.

Are you still working for __MyCompanyName__?

If not you might want to change what XCode puts into the standard header which is being generated for all new files.

//
//  Report.m
//  ASiST
//
//  Created by Oliver Drobnik on 24.12.08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

There is no UI method of changing this string which is kept in the XCode preferences file. Instead you have to use a command like this.

defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{"ORGANIZATIONNAME" = "Drobnik.com";}'

The result being that from now on all code belongs to your company, even if it’s just a company of one.

Read more

Detecting Internet Connectivity

For the latest version of GeoCorder Apple required me to show an alert if there was not internet connection. This is meant to prevent “user confusion” which would occur if the user is expecting something to happen staring at his screen, but nothing happens because of lack of data connectivity.

There is a good example from which you can steal the technique, but it took me some experimenting to find the right mix. For some reason that escapes me the regular connectivity check would only work within the sample, but not in my own code.

Read more

Advanced Sorting of NSArray with Functions

I’ve already discussed previously how to sort NSArray with descriptors and selectors. Even how to unsort it i.e. shuffle it. You can use descriptors to sort by KVC-compliant properties. Selectors you would use if the objects contained in the array have a comparison method themselves.

For those special cases where you neither have properties nor a suitable sort method built into the objects to be sorted you have to resort to the third and last method of sorting an NSArray: with a specialized sort function.

For two projects I need to sort an array of NSDictionaries. Here I’ll show you how I did it.

Read more

Target-specific Headers

Often you will add multiple targets to your project file to build variants of the same app. For example if you have a Lite version or Ad-sponsored variant using the same source code you would definitely save yourself a lot of work having a single project and then multiple targets.

While I was helping a colleague merging three projects into one we found it impossible to assign headers to single targets. You can assign source code, images and lots of other file types, but why oh why no headers? How would we make sure that from 3 Constants.h files always the correct one would be used?

There are of course methods out there which propose to either have a script replace the headers or use an ugly contruct like this:

#if defined(TARGET_1)
#import
#elif defined(TARGET_2)
#import
#else
#error Must define a TARGET macro!
#end

I you you too are cringing from seeing such code. Surely there must be a more elegant solution. I searched, and I found not one but TWO.
Read more

Custom Back Button on Navigation View

When you push a view controller onto a navigation stack, the back button will always show the title of the previous view controller. This can lead to ugly effects if the title is too long. But then again, you want long titles on your view controllers because you don’t want to let the title bar space go to waste.

The method of how you can customize the back button is a little bit counterintuitive, but it was shown on episode 13 of the Standford lectures. [Here my I came to a screetching halt]

When I went to search my Blog for the link to the Standford stuff, I also found that I had already posted exactly the same recipe already one month ago. And  It’s still valid, even though I did not remember posting it. This serves to show that you might not be as productive at 5 am in the morning as you would assume. Memory is lacking then.

Well, just to put in at least something new, here is a before and after visual illustration:

Before:

Before

After:

After

Go to the previous article “Shorter Back Buttons

The Death of Global Variables

c0der asks:

I currently implement global variables by defining them within the AppDelegate.h file (outside of class definition) and then include this file in each ViewController.m that needs access to these variables.

This works just fine but I’ve noticed that after saving/restoring these variables as persistent data (NSUserDefaults), their values seem to change over time.

My question: is this approach the right way to handle globals without encapsulation? I use a lot of ‘C’ data types — int, char, etc. for my global variables (rather than NSInterger, etc.) so I’m not sure if there’s a better approach.

My short answer: DON’T. Globals are EEEEVILLLLLL. Why not create an engine class for your app that you use as shared instance. Define all your “globals” there.

Read more

Build for OS 2.x and 3.0 at the Same Time

Let’s imagine I have this great MyBookshelf App and now with OS 3.0 coming I am planning to include Storekit so that my users can also purchase new content from within my app. So far, that’s a great idea. Additional features for my customers, additional income through in-app sales.

But maybe I have hundreds of thousands of users who are not immediately willing to upgrade to the new software. The reasons for this are outside the scope of this article but as an active developer you are risking loss of customers if you only concentrate on the new. 

Your choices are:

  1. make a final build for 2.x putting in all the bugfixes you can and then move on. Existing 2.x customers will only receive the latest 2.x version, new customers will have to have a 3.0 iPhone to purchase the new app.
  2. create a new app from the same codebase adding only the 3.0 features there and then continuing to push updates to both the 2.x version and the 3.0 version.
  3. create magic code to dynamically load 3.0 frameworks if they are present. This is highly advanced and only for true pros.

For this article I am focussing on option 2. I want one project to hold my source code and have two targets, one for 2.x and one for 3.0. Only the 3.0 version can include the appropriate 3.0-frameworks and I don’t want to see any compiler problems. Also I don’t want to resort to some fancy art of dynamic loading.

Read more

Handling Deprecated Methods for iPhone SDK 3.0

In the past week I received an email by Apple, like my dear developer colleagues, stating that I have to consider whether the code of my apps is compatible to iPhone SDK 3.0. Gosh, the guys from Cupertino got me scared!

And that they can’t take a joke occurred this night: The update of my app Super Trumps, submitted about 8 days ago, has been rejected this night. The friendly associate of Apple stated that my app crashes when using it with SDK 3.0. That means: They’re testing apps already now on compatibility!

Without delay I checked the compatibility of all my existing apps and – lo and behold – in two applications problems occurred.

Read more

How to Fix Code Signing Errors

Andreas asks:

With Device | Debug and Code Sign “iPhone Developer” I have no problems getting my app onto the device, but with all other configurations I get this strange error message in XCode Organizer.
Unexpected Error

That’s a weird error that most of us have encountered one time or the other. Here are my hints how to get it fixed.

Read more