Monthly Archive for August, 2008

NSURLConnection + timeoutConnection = Amdev Woes

So for the past two days I’ve had my iPhone app crashing and vomiting “EXC_BAD_ACCESS” in the console. Generally speaking, this error means you’ve released a reference to some object you still need and some other object tried to send a message to said object afterward. It turns out that it was entirely my fault and (GASP) paying a little more attention to Apple’s reference would saved hours and hours of debugging for me.

NSURLConnection is actually pretty simple to use but here are some tips:

  1. Use the asynchronous mode for any reasonably large file (bigger than a few KB) as people might be on a slow Edge network connection and locking up the view is never a good thing.
  2. Do not send the message “start” to your connection if you use connectionWithRequest or initWithRequest to initialize your connection as you’ll get the nasty “EXC_BAD_ACCESS” error mentioned above. The reason for this is simple:  You’ve scheduled your connection to time out in n seconds, the method that initializes the connection starts fetching data immediately. You then send the message “start,” which spins off the connection trying to fetch data again. This works fine except that the original attempt eventually times out and tries to call back, hosing everything over and crashing your application hard core. Oops. You should only send the message “start” to your connection if you initialize it with initWithRequest:delegate:startImmediately.
  3. When setting up NSURLRequest to pass to NSURLConnection, I highly recommend using NSMutableURLRequest so you can set the User-Agent header.  Some http servers may not return data, even for a simple GET without the User-Agent.  It’s my understanding that User-Agents are optional, but I do not know if said server behavior is nonstandard.

    NSMutableURLRequest *request  = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: @"http://myurl.com"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];

    //Borrow FF2’s User-Agent

[request setValue: @"Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16" forHTTPHeaderField: @"User-Agent"];

I hope this saves someone some time.

return7

so drew and i founded a new startup, return7.  we’re each working on an iphone app, and we’re hoping to submit the apps to apple within the next week or two — stay tuned.

Appcelerator Dynamic Code + IE = Be careful

So you want to insert widget code using element.innerHTML in IE? That’s fine, but wrap the string you’re innerHTMLing in a call to “Appcelerator.Compiler.addIENameSpace(string).”  Why?  When inserting XML into IE’s DOM, you need to prefix it w/ “<?xml:namespace prefix = app ns = “http://www.appcelerator.org” />” so IE knows what to do.  It sort of makes sense, actually, but we’re all spoiled by Firefox and the like.

Can’t Select Distribution Provisioning Profile in Xcode

If you can’t seem to select the Distribution Provisioning Profile when you’re trying to sign your iPhone app for distribution on the AppStore, here’s a tip:  Check your Keychain Access application, under “Keys” on the left hand side to make sure you have a public and private key for the certificate you request in step 1 of the process.

If you don’t have these keys, remove your certificate from Keychain and remove your provisioning file (from ~/Library/MobileDevice/Provisioning Profile). Installing the same old certificates over and over will NOT help — you have to remove them all from your system and invalidate the certificate and distribution profile on the Apple Developer Connection page, then start the process over again.  When you generate the initial request and upload the certificate request to Apple’s site, check Keychain Access for the the public/private keys — if they’re there, you’re on the right track.

From there, clean your build and follow the instructions on Apple’s site and you’ll be good to go.  Make sure you close xcode prior to installing the provisioning profile again (I just copied mine to the directory instead of dragging it onto xcode or itunes).