Posting to Rails with AJAX
Don’t forget your csrf token!
The token itself can be retrieved in Rails with: session[:_csrf_token]
When symbolicatecrash Fails (and it often does)
This problem has plagued me for almost three years now. It seems sometimes symbolicatecrash works, sometimes it doesn’t.
I have all my dSYM files (I even use a build script that archives them all automatically), and spotlight can find them from the UDID, but to no avail.
The solution is atos.
I copied the dSYM file (actually a folder) to the desktop, and run this command & you will get the file & line of code. Repeat for each symbol in the crash that you need (generally you only need one or two key ones…)
$ atos -arch armv7 -o ~/Path/to/Your.app.dSYM/Contents/Resources/DWARF/AppName 0x000cefd8 -[SomeClass someMethod] (in GeoLog) (AppName.m:242)
Thanks to NaveenShan for this tip.
Incidentally, If someone writes a Mac OS X app that can reliably symbolicate crashes, each time, every time, and doesn’t break on every new version of the SDK/OS – I will pay upwards of $50.
I wish things would just work, and I wouldn’t have to waste my precious dev time stuffing around with this crap.
Oh My God They Killed parentViewController
Pre iOS 5, ‘parentViewController’ would return the view-stack’s parent view controller, or the controller which presented that controller when called on a modal view controller. This was the intended, documented functionality of the method, not some mistake.
iOS 5 changes this by splitting out the latter functionality to ‘presentingViewController’. Unfortunately, rather than creating 2 new methods with new functionality, they simply changed the old one and created the new one. So for anyone like me who missed this particular change (it’s not even in the API diffs as it’s not changed, or removed), [self parentViewController] starts returning nil, your code continus to run, but will probably hang if when you expect to have dismissed the view controller.
What terrible design. I think it would have been better to deprecated the old function & split it into two new ones. Or at least provide some warnings to the change. If Objective-C actually crashed on nil values like java, again this would be easy to spot & change – but hang bugs are particularly nasty to try and solve.
Anyway, here are my testing results of [self parentViewController] where self is a controller that was presented as modal:
XCode 4.1 & SDK 4.3: iOS 5: presentingViewController, iOS 4.x: presentingViewController
XCode 4.2 & SDK 5.0: iOS 5: nil, iOS 4.x: presentingViewController
As you can see, when iOS5 runs the older SDK it doesn’t break (because that would break everyone’s code), and iOS4 running with code from the new SDK doesn’t break either. Just SDK & iOS 5 together.
Fixing this if you only support iOS 5 is simple. Replace every instance of [self parentViewController] where you want the model view controllers parent to [self presentingViewController].
Most people need to support iOS 4. This is a bit of a pain because you can’t just call ‘presentingViewController’ without it crashing (due to this method not existing on iOS4). Here’s my #define to get the old behaviour back:
#define self_parentViewController (([self parentViewController] != nil || ![self respondsToSelector:@selector(presentingViewController)]) ? [self parentViewController] : [self presentingViewController])
In english, this means: if the parentViewController is not nil, or the presentingViewController method doesn’t exist yet (in the case of < iOS 5.0), return the result of presentingViewController. Else return the result of presentingViewController.
EDIT: if you need to call the old parentViewController on something other than self, try my revised #define.
NB. just like before – this *may* not return the presenting view controller, if self has it’s own parentViewController that isn’t the one which presented it. But this is equal to the old SDK behaviour, so I don’t think it will introduce bugs.
Everywhere in your code, replace [self parentViewController] with self_parentViewController (no square brackets).
#defines are normally pretty evil but in this case, until you drop 4.0 support I actually feel it’s cleaner. Now you could do [self parentViewController] || [self presentingViewController], and this will work in many cases, that is until parentViewController returns nil on iOS 4, when your app will crash due to the unrecognised ‘presentingViewController’ selector. Safter to use my #define for full backwards compatibility.
json-framework (now SBJSON) is EVIL
I just had an unforgivable bug due to the json-framework JSON library. It was a regression, as it previously worked well a year ago.
When I updated the library back in June, there was a massive flaw: all NSNumbers in NSDictionaries converted to JSON (a pretty common occurance) are limited to 6 significant digits.
so the long: 1234567890 becomes 1234560000
and a double: 123.4567890 becomes 123.456, 12.34567890 becomes 12.3456
Well that is totally shit, I lost the precision of a bunch of data because of this and I am not happy. Now I should have had my own tests to catch this, I accept that. But I guess I put some faith into widely-used data serialisation libraries, that they don’t lose any bloody data.
No idea if this bug is already fixed in SBJSON and I don’t really care.
I just converted my entire codebase to JSONKit. The precision is now preserved. The entire library is just two source files, and the code is actually readable. json-framework is so unnecessarily complicated I can’t even work out where my data is getting lost, so it’s no surprise the bug was introduced.
Git
Here are some things that I find useful in git. Basically this is my scratch pad of commands I need.
Replacing one branch with another.
To set master to be equal to ‘tweaks’
git checkout tweaks git merge -s ours master git checkout master git merge tweaks
Stop your local file and accept theirs (or vice versa).
git checkout --ours filename.c git checkout --theirs filename.c
Undo the last commit (before you push)
git reset --soft HEAD^
git clean -d -x -n # removes all untracked and ignored files
Now something XCode Clear out all the cruft.
~/Library/Developer/Xcode/DerivedData
Reverting a commit (actually reverting it, i.e. a new commit with the reverse merge)
git revert
Splitting off a branch at a given commit, and rolling the origin branch back to that same commit
git checkout core git branch core-experimental git reset --hard <SHA of last commit you want to keep> git push -f <remote> core
Getting the SHA of the submodule of a tag
$ git ls-tree released-1.2.3 foo
Add all the files you don’t want to stash, then:
$ git stash --keep-index
Then unstage your changes if you didn’t really want to stage them with:
$ git reset --soft
Deleting a file from the index without deleting the local copy
git rm --cached filename
Getting diffing your changes before you push them:
git diff origin/master
Diffing the index before commit
git diff --cached
Rescuing HTTPClient Exceptions
I have a need in a rails app to rescue just exceptions raised due to fairly common situations in downloading data from URLs. Catching all exceptions is bad, as exceptions in my code are treated the same as connection errors, and not passed to my standard exception handling (HTTP 500, plus email to me).
From looking at the docs, there are two base-error classes HTTPClient raises due to http connection errors, BaseResponseError and TimeoutError (these are the two classes that extend RuntimeError). A bunch of others derive from these. I decided not to catch the ConfigurationError.
Adding in a catch for invalid domains / IP addresses, and my rescue statement looks like this:
rescue Errno::EADDRNOTAVAIL, Errno::ETIMEDOUT, HTTPClient::BadResponseError, HTTPClient::TimeoutError
Some Useful EXIF Tools
Here are some useful EXIF (image metadata) resources:
Mac exif inspector client (works on Lion) EXIF Viewer
An online tool that seems pretty comprehensive (with an example).
XCode4: Running in Release
One decent feature about XCode4 is a more streamlined “scheme” approach, were rather than having to pick the environment, build type etc every time, there are targeted schemes like “Debug” and “Archive.
One thing I felt was missing however is the ability to install Release builds on my device. I would normally use the ‘Run’ tool to do this in XCode3, but that is now Debug-only in XCode4 (well, you can alter the scheme, but then you have to change it back, and it kinda defies the point). I use my own app a lot, and this can provide some valuable testing, however it’s important to also archive the DSYM files for symbolication of crashes.
One way is to archive, and sync to iTunes. I hate having to use iTunes more than needed, and this is a slow process. It does however archive the DSYM which is useful.
However, I am already archiving the dSYM automatically on all release builds (see this post).
The solution in XCode4 is to use the “Profile” option. It will build and install a normal Release build on your iPhone. With the auto-archive script (above), you still get the dSYM for crash symbolicating. One side-effect is it will launch a profiler, but this is no hassle, you can simply press ‘stop’. Hell of a lot easier and faster than exporting & syncing with iTunes anyway.
Beautiful Ruby
When coding, I often have the need for a statement along the lines of:
print (string_a != nil) ? string_a : string_b
i.e. print string_a unless it’s nil, then print string_b.
In ruby, I figured there must be a neater way, so I tried:
print string_a or string_b
and it worked. Wow. So neat. So awesome…! =D
XCode4′s build output directory
The default output directory (which you can nuke to do a full clean) is now here:
~/Library/Developer/Xcode/DerivedData
Credit:Â http://stackoverflow.com/questions/5331270/why-doesnt-xcode-4-create-any-products/5331427#5331427


