Thursday, October 14, 2010

A Fix for a Seemingly Random iOS App Network Failure

Or How do I display the WiFi connection dialog?

If you're writing an iPhone, iPod touch or iPad app that uses the network, it's important that you set the UIRequiresPersistentWiFi key in your Info.plist file, otherwise your app may occasionally fail to connect to any network resources it requires. You will never see this failure in the simulator, or on an iPhone that has cellular service activated. Let me explain.

In order to conserve battery, iOS devices disconnect any active Wi-Fi connection after 30 minutes if the device is idle or asleep, unless the currently running app has the UIRequiresPersistentWiFi key set. If an app does not have this key set, the system doesn't automatically try to reestablish a Wi-Fi connection, even when your app tries to connect to the network.

The simulator uses your Mac's network connection—it never prompts you to connect via Wi-Fi. An iPhone with active cellular service will automatically use the cellular data network if the Wi-Fi transmitter is asleep.

You can test this on an iPod touch or Wi-Fi iPad, or on an iPhone in Airplane Mode with Wi-Fi turned on. In the Settings app, tell the device to forget any current Wi-Fi network; this will disconnect the Wi-Fi transmitter and turn it off. Then run your app. If you don't have UIRequiresPersistentWiFi set, the device won't prompt you to establish a Wi-Fi connection and any attempts your app makes to connect will fail.

When your app has UIRequiresPersistentWiFi set, the device will automatically connect to known Wi-Fi networks or pop up the standard Wi-Fi connection dialog if no known Wi-Fi network is available, and will maintain the connection while your app is active. This is especially critical for users of Wi-Fi only devices. If users complain that they sometimes have to open Safari or another app in order to get the network to connect, you forgot to set UIRequiresPersistentWiFi in your Info.plist.

One little annoyance: in Xcode (currently at version 3.2.4), the Info.plist editor doesn't include UIRequiresPersistentWiFi in the drop-down list of keys. In order to add it, create a new row and just type UIRequiresPersistentWiFi into the Key field, then right-click on the Value field and choose Value Type -> Boolean from the menu. Alternately, in the Groups & Files tree, right click on the Info.plist file and select Open As -> Source Code File from the menu. Then you can simply add
  <key>UIRequiresPersistentWiFi</key>
  <true/>
to the <dict> tag in the <plist> tag.

Wednesday, October 13, 2010

Apple to Announce OS X 10.7 Lion

Apple sent out invitations to the press today for a Mac related event on October 20th. With a lion peeking out of the invitation, there's no doubt that the next version of OS X will be announced and be code-named "Lion".

Let the speculation begin on what other cool new things will be announced.

Monday, October 11, 2010

12 Great Tips for iPhone Web Development

I just ran across this great article on SitePoint by Stoyan Stefanov, a Yahoo engineer and author of Object-Oriented JavaScript. It's called iPhone Development: 12 Tips To Get You Started. Since SitePoint is all about web development, they left the Web out of iPhone Development, but it's a great article nonetheless.

Stoyan covers just about everything you might want to do with HTML, CSS and JavaScript to customize your site for the small screen:
  1. use the iPhone Simulator and SDK documentation
  2. add the correct CSS @media declaration
  3. set the viewport meta tag
  4. handle orientation changes with JavaScript
  5. set orientation specific styles
  6. hide the Mobile Safari toolbar
  7. use CSS to create rounded corners
  8. handle touch events
  9. handle gestures
  10. add links to make voice calls and send text messages
  11. add an app icon
  12. debug using JavaScript and Xcode
Unfortunately he doesn't cover anything specific to the iPad—the article is actually from April 2009. Still, whether you want to optimize your site for iPhone and iPod touch users or need to show some custom web content in a UIWebView in your native app, just about everything you need to know is summarized right here. Check it out!

Friday, October 8, 2010

iPad Friday: October 8, 2010

Hello and happy Friday.  Today's collection of iPad wallpapers are from a experimental project and soon to be iPhone app.  These wallpapers make great backgrounds so you can easily see your icons and labels.  Cheers!

(click an image for the full-size wallpaper)












iPhone Friday: October 8, 2010

Hello and happy Friday.  Today's collection of iPhone wallpapers are from a experimental project and soon to be iPhone app.  These wallpapers make great backgrounds so you can easily see your icons and labels.  Cheers!

(click an image for the full-size wallpaper)












Thursday, October 7, 2010

Xcode Analyzer Error: "Analyzer skipped this file due to parse errors"

Apple integrated the Clang Static Analyzer into Xcode a while back. It's a part of the LLVM project and it uses the clang C/C++/Objective-C compiler front-end to the LLVM compiler to perform static analysis. The static analyzer (or "clang checker") is really useful for finding common retain/release errors in Objective-C code. You run the analyzer by building your project using the Build and Analyze item under the Build menu.

When I recently went to run the static analyzer on one of my projects using the recently released iOS 4.1 SDK, I got a series of the following types of errors:
Analyzer skipped this file due to parse errors

 '/var/folders/p9/p9X0YndXGHyxgsdRDDYre++++TI/-Caches-/com.apple.Xcode.501/SharedPrecompiledHeaders/PrecompiledHeaders-cbytywsinixhviaxvbooriusnxyo/PrecompiledHeaders.pch' file not found
I found the following workaround for the problem in the Apple developer forums (registration required).

On the Project menu, select Edit Active Target "MyProject" to bring up the Target Info dialog. Edit the Other C Flags setting and add -D__IPHONE_OS_VERSION_MIN_REQUIRED=030000, where 030000 corresponds to the iOS Deployment Target setting:
  • 3.0 -> 030000
  • 3.1 -> 030100
  • 3.2 -> 030200
  • 4.0 -> 040000
  • 4.1 -> 040100
Then the analyzer should be able to run successfully.

Wednesday, October 6, 2010

iOS SDK 4.1 Breaks Unit Tests

I ran into this problem a couple of weeks ago, but didn't get around to blogging about it until today. If you use the OCUnit test framework that's part of Xcode to write unit tests, upgrading to iOS SDK 4.1 will give you a rude surprise:

Run unit tests for architecture 'i386' (GC OFF) did not finish

An internal error occurred when handling command output: -[XCBuildLogCommandInvocationSection testName]: unrecognized selector sent to instance 0x2010cff80

An internal error occurred when handling command output: -[XCBuildLogCommandInvocationSection setTestsPassedString:]: unrecognized selector sent to instance 0x2010cff80

An internal error occurred when handling command output: -[XCBuildLogCommandInvocationSectionRecorder endMarker]: unrecognized selector sent to instance 0x2009b8580

It seems there's a mismatch between Xcode and OCUnit in the 4.1 SDK release. At first I wasn't able to find a workaround. I was going to install iOS 4.2 SDK beta 1 anyway, so I went ahead and did that and found that the problem had been fixed. The beta SDK has been working fine for me (though you should still use the 4.1 release SDK if you're building for submission to the app store).

Checking back on this problem, there are now a couple of workarounds. Apple posted some Objective-C code in their developer forums that monkey patches the problem. And Andy W posted a simple workaround on Stack Overflow.

This isn't the first time that Apple has broken their officially supported unit testing framework with an update to Xcode. It's kinda ironic, but I guess they don't have good test coverage of their test framework.