Sep 13

Apple’s sense of humor

I had a good laugh today when I got a nice error from XCode.

I made a mistake and passed the wrong variable to NSMutableArray::initWithCapacity.The value of the variable was -1 which, obviously, got cast to an unsigned and caused a crash. But I found the error message funny.

Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** -[__NSPlaceholderArray initWithCapacity:]: capacity (4294967295) is ridiculous’

Thanks XCode team, you made my day brighter!

Jun 13

When Google strikes… Google API client library for Objective C linking errors

Yesterday I decided to play around with Google Analytics and get familiar with their API. I wanted to create an iPhone app that loads data from Google Analytics and draws graphs for some of the metrics I considered important. But mainly this was to be just a learning project.

I looked around to see if there’s anything out there to handle all the server communication since it’s based on OAuth2 and I didn’t want to implement everything from the ground up and I found this: Google APIs Client Library for Objective-C

Aside from the documentation being sparse, at best, there was no example for the Analytics part either. I’ve copied the Analytics files and the required dependencies into my project everything seemed to go smooth… except that it didn’t.

When trying to compile I’d get a nice linker error saying that there are 97  duplicate symbols (see below). Now, debugging linker errors is about as much fun as trying to count grains of sand while wearing boxing gloves. I checked that the .m files are not included twice under Build Phases -> Compile Sources. Nope, all seemed ok. I even removed the files and added them manually…. No luck. I cleaned and rebuilt… Still nothing. Talk about finding the needle in the hay stack… I ended up looking through the source code and I rested my eyes on this little gem called GTLAnalytics_Sources.m

#import "GTLAnalyticsConstants.m"

#import "GTLAnalyticsAccount.m"
#import "GTLAnalyticsAccounts.m"
#import "GTLAnalyticsCustomDataSource.m"
#import "GTLAnalyticsCustomDataSources.m"
#import "GTLAnalyticsDailyUpload.m"
#import "GTLAnalyticsDailyUploadAppend.m"
#import "GTLAnalyticsDailyUploads.m"
#import "GTLAnalyticsExperiment.m"
#import "GTLAnalyticsExperiments.m"
#import "GTLAnalyticsGaData.m"
#import "GTLAnalyticsGoal.m"
#import "GTLAnalyticsGoals.m"
#import "GTLAnalyticsMcfData.m"
#import "GTLAnalyticsProfile.m"
#import "GTLAnalyticsProfiles.m"
#import "GTLAnalyticsSegment.m"
#import "GTLAnalyticsSegments.m"
#import "GTLAnalyticsWebproperties.m"
#import "GTLAnalyticsWebproperty.m"

#import "GTLQueryAnalytics.m"
#import "GTLServiceAnalytics.m"


Arghhhhhhhhhh! Importing .m files? Whyyyyyy? I simply left out the file and all works without a hitch. But I did waste one hour to track this down.

Continue reading →

Nov 11

Upgrading to iOS5 causes ‘No Provisioned iOS devices are available’ error

I finally decided to upgrade my iPhone to iOS 5 today. The upgrade process was pretty smooth, but once it was done, I was no longer able to run the apps from XCode. I kept getting the no provisioned ios devices are available with a compatible ios version error.

Turned out that all I had to do was to go to Window->Organizer and, from the Devices pane, select my iPhone and click on Use this phone for development.

Easy solution, but was not immediately obvious to me

Sep 11

Apple pains – Invalid signing certificate

I’ve been trying for the past two days to submit a new version of CashBase but I kept getting the dreaded “application failed codesign verification. the signature was invalid, or it was not signed with an apple submission certificate. (-19011) ” error. (Note: after migrating to XCode 4, all sorts of weird things happen, talk about a buggy piece of software, right there)

I tried all the tricks in the book: revoke the distribution certificate and re-create it, clean, made sure that the Code Signing for distribution is the correct one, etc. Nothing!

After many failed trials, I thought of taking a look at the entitlements used to sign the binary:

codesign -dvvv CashBase.app

I noticed that the Authority key was using my iPhone Development certificate. Turns out that the Archive Scheme in XCode 4 decided to switch all by itself to the Release Build configuration instead of the Distribution. Changing it back to Distribution solved my problem

With hindsight, I should have noticed that the binary was being compiled to the Release-iphoneos instead of the Distribution-iphoneos folder.

Note: A collection of helpful hints for this error can be found here

Edit: To edit the scheme, go to Product -> Edit Scheme. Select the Archive Scheme and make sure that the Build Configuration is set to Distribution (this is for XCode 4.x)

Feb 11

Windows 7 – keyboard hooks stop working, a workaround

It seems that sometimes applications that install a keyboard hook (to monitor media keys, for instance) can randomly stop receiving the keyboard commands. I’ve seen this problem happen with the Groovie app. I’ve also heard of it happening with various music and video players.

It turns out to be a bug in Windows7; under full-load, the Operating System will not allow sufficient time for the hooks to process the messages and will remove them without notifying the application.

One workaround that I found was to increase the Hooks Timeout setting in the Windows registry:

Go to HKEY_CURRENT_USER\Control Panel\Desktop and set the LowLevelHooksTimeout to a larger value, I used 2710 (which is 10 seconds)

Hope this helps someone.

Feb 11

Grooveshark keyboard hook

The guys at Grooveshark did an awesome job switching from the Flash based player to Javascript (as a side note, I just wish they’d lose the Flash ads, they make my Macbook Pro’s CPU hit 70% load just listening to music).

This allowed me to write a new application that captures the play/pause/forward/backward media keys and send the necessary messages to the player. Using the app is so much simpler as well, just launch the app and you’re ready to go! No need to go through the tedious process of selecting the window and keys, everything should be straightforward.



Since Grooveshark never replied to my email, I’m publishing the code

Groovie Source

Feb 11

iPhone autocomplete class

While working on the Cashbase iPhone project ( CashbaseHq ), I found myself longing for an autocomplete feature, but could not find anything on the web. So I decided to build my own. The functionality is identical to the way autocomplete is used on the web. Upon typing a text in the text field, a list with suggestions appears bellow it. Selecting an item from the suggestions list will hide the list and add the word in the textfield.

This is how it looks in our app (for space reasons we have designed the suggestion list to be displayed above the field that requires autocomplete, but you can easily display it below – just see the demo app)

How to use:

Import the Autocomplete.h file

#import "Autocomplete.h"

Prepare the target UITextField for autocomplete by disabling the iPhone’s default autocomplete feature. You usually do this in the viewDidLoad method.

//disable the iPhone's default autocomplete feature
textField.autocorrectionType = UITextAutocorrectionTypeNo;
//create the Autocomplete class and initialize it with some data
autocomplete = [[Autocomplete alloc] initWithArray:[[NSArray alloc] initWithObjects:@"apples", @"oranges", @"bananas", @"peaches", @"grapes", @"blackberries", @"strawberies", @"watermelons", @"mangos", @"pears", @"lemons", nil]];

Now, when the text in the text field has changed (ie: in the EditingChanged handler), just call the GetSuggestions method, which will return an array of strings that match the parameter passed

NSArray *suggestions = [autocomplete GetSuggestions:textField.text];

You can do whatever you want with these suggestions, I displayed them in  a table view and when the user taps a cell, the text is added to the text field (see attached demo project)

Autocomplete Demo Application

Autocomplete Source

Nov 10

Simple Grooveshark play / previous / next media keys hook

Notice: This software is obsolete, please read this post instead

A few days ago, a good friend of mine told me he’d like to be able to play/go back/skip the songs on Grooveshark, and asked whether I could write a small application to implement the functionality he needed.

I thought this to be a great occasion to stick my nose in C# and see what’s what. A few hours later I had a simple application that does just what was required. There’s nothing fancy about it, and I don’t plan on adding any new features, but feel free to grab the source code and modify it after your own heart’s desire.

How to use:

  1. Open the Skipper app
  2. Open Grooveshark in your browser – I hope you’re using Firefox or Chrome! If you’re using Internet Explorer, help make the world a better place and download one of the browsers mentioned above!
  3. Drag the Window Finder tool on top of the Grooveshark window
  4. Add the X,Y coordinates for the play / previous / next buttons in the app (you can find these by taking a capture of your screen and using any image editing software to find the coordinates). Please note that the coordinates have to be in Window space, meaning that you should compute X and Y starting with from the top-left corner of your window, not from the top-left of your screen)
  5. Make sure you don’t change the tab in the browser. The Grooveshark tab must ALWAYS be active. If you need multiple tabs, I suggest you open Grooveshark in a separate browser.
  6. Use the media keys (play / previous / next) on your keyboard to navigate through the songs.
  7. NOTE: the browser window does not have to be active, it can be hidden by other windows and this will still work

I’ve only tested this on Windows 7 but it should work in Vista / XP as well.

If you need additional functionality, feel free to modify the source code, I will not have time to upgrade it.

Edit: A big thank you to Ruurd Moelker for taking the time to improve the application by adding the following:

  • Media button target selection with mouse cursor.
  • Always visible target pointers
  • An icon.
  • Trayicon support.

Download Skipper

Download Skipper Source code

Nov 07

Yahoo! Messenger archive search

For quite a while now I’ve been frustrated by the fact the Yahoo! Messenger does not give you the option to search through the archive. I’ve found myself wishing for that search function more and more often lately. Having to browse through each conversation, to find what I needed, was inhuman – especially since I didn’t remember when the conversation took place!

So I decided to build a small application, that reads the Yahoo! Messenger Archive and implements basic keyword search. The first version of the software is already complete and can be downloaded here. The current version is completely functional, though many improvements are still in store.

So far I’ve only been interested in functionality, so the interface is kind of awkward, but will be one of the last things I will take care of. There are still some issues with the multithreaded code that have to be sorted out, but I’ve found this utility helpful a few times already.

Check it out and let me know if you find any bugs or have any suggestions!

Nov 07

C++ vs C#

For a while now, I’ve been thinking whether , for me, as a C++ programmer, learning C# is worth investing time in. I admit I was kind of annoyed seeing people with little experience in programming saying that C# is the way to go, it is the latest release from Microsoft so it must be the greatest thing since sliced bread.

There are a lot of good posts on C++ vs. C# out there, so I won’t repeat any of the traditional pros and cons, these are just some of my thoughts on the matter.

I believe some hotshots at Microsoft took a good look at Java and thought “Hmmmm….this write once, run anywhere thingie seems intriguing…and why are so many people using it?… and we don’t like that Sun’s getting all this attention. Yeah, we definitely want a share of this! But…we didn’t really have that much of a success with J++, how can we rebrand it? Wait! Why don’t we create a new language that runs anywhere (as long as that anywhere supports a Win-based OS, of course) and market it as the language that incorporates the best features from all other major languages?”

Now, believe it or not, Microsoft has nailed a lot of good things with C#. It is easy to use, the learning curve is nowhere near as steep as C++’s and, there’s plenty of documentation for it. Unfortunately, I believe these are also its pitfalls. IMHO, while C#’s relative simplicity will – maybe – allow more people with great ideas, that might not have had the time to learn C++, to start programming, it will also encourage sloppy programming. Most (new) programmers will not bother about resource management (where and when the resources are allocated and when or where they are released) since the garbage collector does that transparently, but truth is this will have a great impact on performance.

So…back to my original problem, should a C++ programmer bother learning C# or not? Well, with the CLR included neatly into VS 2005 there’s no need to, but in the end it all comes down to the personal preference. I say, take a look at C#, you might end up liking it. Personally, I did program with it a little bit, but didn’t like it… I’m sticking with C++ for now 🙂