Showing posts with label Usefull Links. Show all posts
Showing posts with label Usefull Links. Show all posts

Thursday, December 16, 2010

Tips & Tricks for conditional iOS3, iOS3.2 and iOS4 code

Tips & Tricks for conditional iOS3, iOS3.2 and iOS4 code

In this post, I'll show you ways to determine which version of iOS you are running on and show you how to write a macro that can both conditionally compile and runtime switch between the code for different versions of iOS.

A project or target that supports multiple versions of iOS

To make an application target that runs on multiple versions of iOS is relatively simple:
  • Set the "Base SDK" in your projects settings to the newest version number of iOS whose features you may want.
  • Set the "iPhone OS Deployment Target" to the oldest version number of iOS that you will support
However, getting the target settings correct is the easy part of the problem. The hard part is using new features on newer iOS versions without breaking the app on older versions.

Running in the 3.1.3 simulator

Before getting to the actual code, it might be worthwhile to discuss how to run your projects in older versions of the simulator.
The simulator is an important part of iOS development (since it is much faster and simpler than running your code on the device). But Apple have removed SDK versions earlier than 3.2 from the current Xcode builds. This makes it hard to verify your apps on earlier devices unless you install on a physical device.
Support for 3.1.3 is relatively important since it is the last version of iOS supported by the original iPhone and iPod Touch and it will be a few months before iOS 4 exceeds 80% of the remaining iPhone and iPod Touch market.
To allow simulation in 3.1.3, you must install an old version of Xcode. If you are a registered iPhone developer, you can download Xcode 3.1.4 for Leopard with iPhone SDK 3.1.3 or Xcode 3.1.4 for Snow Leopard with iPhone SDK 3.1.3. Be careful to install these in a different location to your Xcode 3.2.3 with iOS3.2/iOS4 (either select a different hard disk or rename your existing /Developer directory before you install).
Once you've got an old version of Xcode, you'll want to duplicate your main target and set the Base SDK to 3.1.3 in this duplicate (because it won't exist in this version of Xcode). You should use a second target for this because you shouldn't risk messing with your main target just to run code in the simulator.

Using features from newer iOS versions while supporting older iOS versions

For example, if you want to start an iOS4 background task in an application that you want to run on earlier versions of iOS, then you'll need to use code like this:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    if ([[UIApplication sharedApplication]
        respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)])
    {
        UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
            beginBackgroundTaskWithExpirationHandler:^{}];

        // Perform work that should be allowed to continue in background

        [[UIApplication sharedApplication] endBackgroundTask:bgTask];
    }
#endif
There are three important components:
  1. The #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 4000 compile-time conditional. This ensures that if we choose to build this project with a Base SDK lower than 4.0, then it won't cause compile problems. This is essential for running in older versions of the simulator.
  2. The runtime check that UIApplication supports the beginBackgroundTaskWithExpirationHandler method. Since the final release build will be built against the 4.0 SDK (even if users install on SDK 3.0) this runtime check ensures that the method we need is available.
  3. Everything else between the #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 4000 and the #endif is the iPhone OS 4 code.

Making the conditional work less ugly

The problem with the previous code is the compile-time conditional and the runtime check for the presence of methods is cumbersome since you must remember to to both.
If you want to integrate both a compile-time check and a runtime check, a better approach would look like this:
IF_IOS4_OR_GREATER
(
    UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]
        beginBackgroundTaskWithExpirationHandler:^{}];

    // Perform work that should be allowed to continue in background

    [[UIApplication sharedApplication] endBackgroundTask:bgTask];
);
We can implement this macro as follows:
#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_4_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_4_0 550.32
#endif

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
#define IF_IOS4_OR_GREATER(...) \
    if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_4_0) \
    { \
        __VA_ARGS__ \
    }
#else
#define IF_IOS4_OR_GREATER(...)
#endif
If we want to include something only in OS versions prior to a a specific version, then we don't need the conditional compilation (since we still want the code to appear when compiled in a later version. In this case, only a runtime check is required. You can either do this directly, or for symmetry with other macros, you could use:
#define IF_PRE_IOS4(...) \
    if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_4_0) \
    { \
        __VA_ARGS__ \
    }
Three interesting points to note about these macros:
  1. I use the kCFCoreFoundationVersionNumber to determine the iPhone OS at runtime. There are many examples on the web using [[UIDevice currentDevice] systemVersion] but that method requires a string comparison and potentially handling of major and minor numbers within the string components. A single double comparison is far more straightforward.
  2. I have not used the typical do { x } while (0) wrapper around the macro, so you can simply tack an else onto the end if you choose (and it doesn't need conditional compilation of its own).
  3. I use a variable argument list for the macro. This is so that any number of commas may appear in the contents without causing problems.
A final point... the kCFCoreFoundationVersionNumber definitions may not be in every version of the SDK (each SDK normally contains definitions for versions up to but not including itself), so you should conditionally define them yourself in case they're missing. Here's a handy list:
#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_2_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_2_0 478.23
#endif

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_2_1
#define kCFCoreFoundationVersionNumber_iPhoneOS_2_1 478.26
#endif

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_2_2
#define kCFCoreFoundationVersionNumber_iPhoneOS_2_2 478.29
#endif

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_3_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_3_0 478.47
#endif

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_3_1
#define kCFCoreFoundationVersionNumber_iPhoneOS_3_1 478.52
#endif

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_3_2
#define kCFCoreFoundationVersionNumber_iPhoneOS_3_2 478.61
#endif

#ifndef kCFCoreFoundationVersionNumber_iPhoneOS_4_0
#define kCFCoreFoundationVersionNumber_iPhoneOS_4_0 550.32
#endif

Better still: solutions that don't require macros

Better than a simple macro is a simple function. This is a valid solution where the contents of your conditional code does not itself contain OS specific code (only the condition itself requires OS specific logic).
A common example is handing separate layout for iPad and iPhone versions. Ordinarily, if you're compiling for iPad 3.2 and iPhone 3.1.3, you need the following code:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
    if ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] &&
        [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        // iPad specific layout changes
    }
    else
#endif
    {
        // iPhone layout
    }
You can handle this with a conditional macro like the IF_IOS4_OR_GREATER but a far better solution is:
if (isIPad())
{
    // iPad specific layout changes
}
else
{
    // iPhone layout
}
Where all the conditional pollution is tidily kept in your isIPad() function:
BOOL isIPad()
{
    IF_3_2_OR_GREATER
    (
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
        {
            return YES;
        }
    );
    
    return NO;
}

Conclusion

Apple doesn't exactly make it easy to support old versions of the iPhone SDK. I'm sure they want everyone to keep up to date or buy new devices if their current device can't be updated.
That's not always a realistic attitude for App Store developers. You can't expect all your customers to upgrade as soon as possible.
The important point when writing for multiple versions of the SDK is to keep as few conditionals as possible. You don't want to have thousands of conditionals in your code for supporting different versions. Every conditional is extra testing work since different behaviors must be fully exercised on all different platforms.
While the conditionals and functions I've talked about here will help, if you find yourself needing a lot of conditionals you may also want to consider design changes like instantiating different subclasses for different OS versions.

100 Free Courses & Tutorials for Aspiring iPhone App Developers

100 Free Courses & Tutorials for Aspiring iPhone App Developers

Unless you’ve been living under a rock, you know that the iPhone is a big deal and it’s one of the most popular subjects of development these days. Lots of developers are creating their own iPhone apps, and with the right know-how, you can too. Check out our list of courses and tutorials to learn everything that’s important about developing for the iPhone.
University
Here you’ll find iPhone development courses offered by top universities.
  1. iPhone Application Programming: Learn about programming for the iPhone from Stanford on iTunes. [Stanford]
  2. Introduction to iPhone Application Development: Use this course’s posted slides to get a crash course in iPhone application development. [MIT]
Apple Resources
You can learn about iPhone development straight from the source with these Apple documents.
  1. Getting Started with iPhone: Here you’ll find a general introduction to iPhone development. [Apple]
  2. Object-Oriented Programming with Objective-C: This document offers an excellent guide for object oriented programming. [Apple]
  3. Networking & Internet Coding How-Tos: In this resource, you will find lots of great advice for networking and Internet development on the iPhone. [Apple]
  4. Getting Started with Audio & Video: Use this document to get started with audio and video features in iPhone applications. [Apple]
  5. Your First iPhone Application: This introductory tutorial offers a step by step description of getting started with an iPhone application. [Apple]
  6. Getting Started with Performance: This guide offers an introduction to improving the performance on iPhone apps. [Apple]
  7. iPhone Application Programming Guide: Get an introduction to the iPhone OS and development process. [Apple]
  8. iPhone OS Technology Overview: Learn about the iPhone OS and its technologies from this guide. [Apple]
  9. Getting Started with Data Management: Here you’ll find a reference that will help you with data management. [Apple]
  10. Security Overview: Get an understanding of the security concepts on the iPhone from this resource. [Apple]
  11. Performance Overview: Get a look at the factors that determine performance through this guide. [Apple]
  12. Resource Programming Guide: Check out this resource to learn how to work with nib and bundle resources.
  13. Getting Started with User Experience: This document offers an introduction to constructing iPhone application user interfaces. [Apple]
  14. iPhone Human Interface Guidelines: Follow these guidelines to make sure your iPhone app has a good human interface. [Apple]
  15. iPhone Development Guide: Use this development guide to get an introduction to creating web apps on the iPhone. [Apple]
  16. Data Formatting Programming Guide for Cocoa: This guide will teach you how to use Cocoa formatters for data. [Apple]
  17. Getting Started with Tools: You will find a guided introduction to the Xcode toolset from this document. [Apple]
  18. Data Management Coding How-tos: Get answers to common data management coding questions. [Apple]
  19. Introduction to Cocoa Application Tutorial: You’ll need at least a base level understanding of Cocoa for iPhone development, which you can check out in this tutorial. [Apple]
  20. Core Animation Programming Guide: Follow this guide to get the main components and services of Core Animation. [Apple]
  21. Coding Guidelines for Cocoa: In this guide, you’ll learn about naming guidelines for the Cocoa API as well as design advice. [Apple]
  22. Getting Started with Graphics and Animation: Follow this guide for an introduction to 2D and 3D graphics and animation. [Apple]
  23. Learning Objective-C: A Primer: Check out this document once you’ve worked through object oriented programming and Cocoa. [Apple]
  24. Cocoa Fundamentals Guide: You’ll learn about the basic concepts, terminology, and more in Cocoa from this guide. [Apple]
  25. Graphics and Animation Coding How-Tos: In this resource, you’ll find lots of great tips and advice for graphics and animation on the iPhone. [Apple]
Getting Started
Get an introduction to iPhone development through these tutorials.
  1. iPhone App Development-Where to Start: This tutorial will teach you how to get started in iPhone app development. [The Daleisphere]
  2. Bootstrap: Learn a few pointers for iPhone development from this resource. [furbo]
  3. Learn How to Develop for the iPhone: This tutorial will show you how to build an alternate page and style sheet for the iPhone. [NETTUTS]
  4. iPhone Application Development, Step By Step: In this tutorial, you will find a step by step guide to creating a simple iPhone game. [Open Laszlo]
  5. First iPhone Application: Get a brief introduction to creating your first iPhone application. [iPhone SDK Articles]
  6. iPhone Dev: Check out this PDF to get a tutorial for iPhone development. [Lucas Newman]
  7. iPhone App Development for Web Hackers: Use this tutorial to learn about geo-location features and beginner development tips. [How to Iphone Application]
  8. How to Write an iPhone App: This tutorial gives you a basic look at what it takes to write an iPhone application. [Webmonkey]
  9. iPhone App Development for Web Hackers: In this article, you’ll learn about web hacking development for the iPhone. [Dominiek]
  10. Writing Your First iPhone Application: Bill Dudney will walk you through all of the tools and pieces of knowledge you’ll need to write your first iPhone application. [The Pragmatic Bookshelf]
  11. Cocoa Touch Tutorial: iPhone Application Example: This tutorial will show you how to make a very basic Cocoa Touch application with Interface Builder. [Cocoa Is My Girlfriend]
  12. Building an iPhone app in a day: Check out this tutorial to see how you can build a useful app quickly. [The Bakery]
  13. Seven Things All iPhone Apps Need: Check out this list to see what’s essential when creating an iPhone app. [APCmag]
  14. Put Your Content in My Pocket: Learn how to use the iPhone web browser to your advantage from this article. [A List Apart]
  15. iPhone Training Course: Become a master at writing iPhone applications through this course. [Rose India]
  16. So you’re going to write an iPhone app…: Learn about code reuse, memory, and more from this tutorial. [furbo]
  17. Learn How to Develop for the iPhone: Check out this tutorial to see how to build an alternative page and style sheet for the iPhone. [Net Tuts]
  18. Developing for the iPhone: This resource will show you how to develop ASP.NET applications for the iPhone. [Dot Net Slackers]
  19. Getting Started with iPhone Development: Ed Burnette offers a basic introduction to iPhone development. [ZDnet]
Tools
These tutorials will teach you how to use specific tools in order to create iPhone apps.
  1. Make an iPhone App Using the Envato API: Make your own iPhone app with the Envato API with the help of this tutorial. [Net Tuts]
  2. Developing iPhone Applications using Ruby on Rails and Eclipse: Learn how to detect mobile Safari from a Ruby on Rails application through this tutorial. [IBM]
  3. 14 Essential Xcode Tips, Tricks and Resources for iPhone Devs: Learn how to make sense of xcode with this helpful resource. [Mobile Orchard]
  4. Develop iPhone Web Applications with Eclipse: This tutorial will help you learn how to create iPhone applications with Aptana’s iPhone development plug-in. [IMB]
  5. Build an iPhone Webapp in Minutes with Ruby, Sinatra, and iUI: You can learn how to quickly put together an iPhone app with these tools. [Mobile Orchard]
  6. iPhone Development with PHP and XML: In this tutorial, you’ll get a look at developing custom applications for the iPhone. [IBM]
Details
These tutorials cover all of the important details in iPhone app development.
  1. Avoiding iPhone App Rejection from Apple: This tutorial holds the secrets to making sure your iPhone app makes the cut. [Mobile Orchard]
  2. Landscape Tab Bar Application for the iPhone: Follow this tutorial to learn about making the tab bar application support landscape orientation. [Cocoa Is My Girlfriend]
  3. iPhone Programming Tutorial-Using openURL to Send Email from Your App: This tutorial explains how you can send email through applications, and even pre-fill fields. [iCode]
  4. Multi Touch Tutorial: This tutorial will show you how you can respond to a tap event. [iPhone SDK Articles]
  5. Create a Navigation-Based Application: This tutorial will teach you how to create and run a navigation-based application from XCode.
  6. Advanced iPhone Development: Go beyond the basics with this iPhone development tutorial. [Dot Net Slackers]
  7. Here’s a Quick Way to Deal with Dates in Objective C: Get information on dealing with date fetching through this tutorial. [Howtomakeiphoneapps]
  8. Navigation Controller + UIToolbar: Through this tutorial, you can learn how to add a UIToolbar to an app. [iPhone SDK Articles]
  9. iPhone Asynchonous Table Image: Follow this thorough article to learn about loading multiple images in your iPhone app in an asynchonous manner. [Markj]
  10. Localizing iPhone Apps-Internationalization: You can use resource files to display text in a user’s language-learn how in this tutorial. [iPhone SDK Articles]
  11. Tutorial: JSON Over HTTP on the iPhone: With this tutorial, you’ll get a step by step how-to for JSON web services through an iPhone app. [Mobile Orchard]
  12. Parsing xml on the iPhone: This tutorial will show you how to parse XML using the iPhone SDK. [Craig Giles]
  13. Reading data from a SQLite Database: Here you’ll find a quick tutorial for reading data from a SQLite database. [dBlog]
  14. How to Make an Orientation-Aware Clock: Through this tutorial, you’ll learn about building a simple, orientation-aware clock. [The Apple Blog]
  15. Finding iPhone Memory Leaks: Carefully find iPhone memory leaks by using this tutorial. [Mobile Orchard]
  16. Localizing iPhone Apps: MAke sure that your iPhone app is properly formatted according to a user’s native country or region with the help of this tutorial. [iPhone SDK Articles]
  17. OpenAL Audio Programming on iPhone: Here you’ll get code snippets, learning, and more. [Gehaktes]
  18. 9 iPhone Memory Management Links and Resources: Here you’ll find a variety of iPhone memory management resources that can help you get things under control. [Mobile Orchard]
  19. Parsing XML Files: Get an understanding of how you can parse XML files with this tutorial. [iPhone SDK Articles]
User Interface
These tutorials are all about the user interface and interaction.
  1. UITableView-Drill down table view tutorial: Check out this tutorial to learn how to make a drill down table view. [iPhone SDK Articles]
  2. iPhone Coding-Learning About UIWebViews by Creating a Web Browser: In this tutorial, you’ll learn about UIWebViews through the creation of a browser. [iCode]
  3. Design Patterns on the iPhone: Check out David Choi’s guest lecture on user interface design for the iPhone. [New Jersey Institute of Technology]
  4. UITableView-Adding subviews to a cell’s content view: This tutorial will show you how to customize the UITableViewCell. [iPhone SDK Articles]
  5. Drill down table view with a detail view: Learn how to load a different detail view on the UITabBarController. [iPhone SDK Articles]
  6. Extending the iPhone’s SDK’s UIColor Class: Learn how to extend the iPhone SDK UIColor class, and get code samples from this article. [Ars Technica]
  7. UITableView: Learn how to make a simple index for the table view with this tutorial. [iPhone SDK Articles]
Building Tutorials
Check out these tutorials where you’ll build a specific app, and learn more about iPhone development along the way.
  1. Build a Simple RSS Reader for the iPhone: Get walked through the creation of an RSS reader for a simple feed on the iPhone. [The Apple Blog]
  2. iPhone Gaming Framework: This article offers a look at writing code for iPhone game developers. [Craig Giles]
  3. Build a Simple RSS Reader for the iPhone: Follow this tutorial, and you’ll learn about building a simple iPhone RSS reader.
  4. iPhone Game Programming Tutorial: This multipart tutorial offers a way to learn OpenGL and Quartz for iPhone development. [iCode]
  5. Build your very own Web browser!: Follow this tutorial to learn about the process of building your own iPhone web browser. [dBlog]
  6. iPhone application development, step by step: Find out how to build the iPhone application NEWSMATCH using OpenLaszlo. [OpenLaszlo]
  7. Building an Advanced RSS Reader using TouchXML: Get step by step information for creating an advanced iPhone RSS reader from this tutorial. [DBlog]
  8. iPhone SDK Tutorial: Building an Advanced RSS Reader Using TouchXML: This tutorial will help you learn more about iPhone development by building an advanced RSS reader with TouchXML. [dBlog]
Videos
Watch these videos for a visual guide to iPhone app development.
  1. Basic iPhone Programming: Check out this video to get started with iPhone programming. [iPhone Dev Central]
  2. First Step Towards the App Store: Work towards getting your app in the app store with the help of this tutorial. [You Tube]
  3. Hello World: This tutorial will help you learn the basics of iPhone programming. [iPhone Dev Central]
  4. UITableView iPhone Programming Tutorial: Watch this video to learn how to populate a UITableView. [YouTube]
  5. iPhone App Tutorial 1: Check out this video to quickly learn about Interface Builder. [YouTube]
  6. iPhone IB-Your First App: Watch this tutorial to learn how to use the Interface Builder. [iPhone Dev Central]
  7. Understanding Source Code: Learn how to get started with development on the iPhone through this video tutorial. [YouTube]
  8. How to Make an iPhone App: Create an iPhone app using Jiggy and this tutorial. [YouTube]
  9. iPhone Development with Dashcode: Find out how to develop iPhone applications with Dashcode through this tutorial. [YouTube]
Development Resources
These resources are not courses or tutorials, but they are incredibly valuable resources for beginner iPhone app developers.
  1. iPhone Open Application Development: This book will teach you how to create software for the iPhone environment. [Safari Books Online]
  2. iPhone GUI PSD File: Use this set to get a comprehensive, editable library of iPhone UI assets. [Teehanlax]
  3. 31 iPhone Applications with Source Code: Teach yourself how to create iPhone apps by taking a look at the code in these. [Mobile Orchard]
  4. iPhoney: Using iPhoney, you’ll be able to see how your creation will look on the iPhone. [Market Circle]
  5. 35 Free iPhone Icon Sets: Check out this resource to find a great variety of iPhone icons

Usefull Links

Downloads - cookbooksamples - Project Hosting on Google Code
iPhone Programming Tutorial – Local Notifications | Blancer.com Tutorials and projects
AppsAmuck iPhone Development Tutorials and Examples
The Daleisphere — iPhone App Development – Where to Start
Cocoa with Love: Tips & Tricks for conditional iOS3, iOS3.2 and iOS4 code
AVAudioPlayer volume problems - iPhone Dev SDK Forum
UNIX Tutorial One
How to target a specific iPhone version? - Stack Overflow
***OBJECTIVE-C WITH XCODE**: STANFORD ONLINE BOOKS
iPhone native apps icons: how to remove the glossy look. | Surgeworks Mobile
iPhone SDK – Get Current Touch Location | de
A-1 Technology Newsletter
C Tutorial
User Experience Coding How-To's
iOS4 Map Kit - Draggable Annotation Views | Trent Kocurek
PayPal X Developer Network: Community: PayPal Mobile XSpace
TheElements
UIKeyboardTypeNumberPad and the missing "return" key
PayPal X Developer Network: Community: PayPal Mobile XSpace
Cocoa Is My Girlfriend » Accessing The Cloud From Cocoa Touch
Using iPhone SDK MapKit Framework – A tutorial « The Spoken Word
100 Free Courses & Tutorials for Aspiring iPhone App Developers | Best Universities
[A-1 Technology Client Extranet] Sign in
In App Purchase Programming Guide: Introduction
erica's iphone-3.0-cookbook- at master - GitHub
Creating a Tabbar
Event-Driven XML Programming Guide: Parser Capabilities and Architecture
iPhone : warning: no ‘-renderInContext:’ method found – solution « Cocoa, Makin’ it Simple.
iphone - blend two uiimages - Stack Overflow
Java Operators Tutorial | Java Beginner