Tuesday, 31 July 2012

Could Not Instantiate Class Named 'NSLayoutConstraint'

Back into iPhone development again, I found myself briefly scratching my head at an app crashing when testing on one of my real devices, but not another. The console shows a stack trace with the pertinent complaint being


*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named NSLayoutConstraint'*** 

Well that's no good now is it ? I found that it runs just fine on my iPhone running iOS 6 beta 3, but crashes out with the above error when running on my iPod 5.1. It turns out that the new Xcode has introduced some new functionality into the Interface Builder, some of which is enabled by default when creating new XIBs.

The offending item is this case, is the use of "Autolayout" of interfaces. It seems that when using Xcode to create new interfaces, Autolayout is checked by default, even though it is a feature available for iOS 6 onwards. My project targets iOS 4, so I was a bit surprised to see it assume such a breaking setting, but maybe that is just because the Xcode version I am using is also still in beta (Version 4.5 4g125j).

The fix is quite simple, Select the XIB which is failing to load, and from the panes down the right, find the one entitled "Interface Builder Document". Under here, are settings for deployment versions (which represents the minimum version you want to support), which can be changed to match your expectations.





Selecting the correct version will result in the build failing, quoting a build error such that:

Auto Layout on iOS Versions prior to 6.0

Underneath this setting, is a checkbox entitled "Use Autolayout" which should be unchecked, as per the build error. Rinse and repeat for any other XIBs you have created using Xcode 4.5.

Tuesday, 17 July 2012

BBC News App for Asus Nexus 7 Tablet

** Update #1 - 19th July, 2012 **
I notice that the BBC news app is now showing up in the Play store, however, it is not optimised for the tablet. In fact, it is the mobile version of the application which is perfectly functional but not as nice of a user experience. I've opted to stick with tablet optimised version for now, which can be downloaded below. Feel free to try both out, one will simply install over the other. The official version is available here.
**

You will find the BBC News app link below. Nexus 7 can run the BBC News app, however a search of the UK Google Play store will reveal that the app is incompatible with the Nexus 7. I'm sure the BBC will update the app's compatibility shortly, rendering this post pointless when it does, but in the meantime this post will provide details on how to get the BBC News app on your shiny new Nexus 7. Note, this is the tablet version of the app, so is optimised for tablet layouts.

You will need to allow downloads from 3rd party locations. That is enter Settings, Security, then check the option to allow installs from Unknown Sources.

Download the APK from here. Open the APK and accept the install.

As you can see, it looks pretty good. Videos can be played using a 3rd party app, such as MX Player.

screenshot showing the app in landscape mode
in landscape  mode

Thursday, 14 June 2012

Create JSON manifest file for directory

The following bash shell script was created for a very specific purpose; however due to licensing agreements the exact purpose cannot be disclosed as of yet. However, the techniques used here might prove useful to others striving to solve similar problems to that which I have just solved.

This script will inspect a directory of your choosing, and for each file in the directory, calculate the SHA-1 hash. A JSON file will then be created called "manifest.json" which lists each file along with its corresponding hash.

Usage: (Created and tested on a Mac)
From a Terminal prompt, enter:
bash manifestMaker.sh dir
where dir is the directory for which you want to create a manifest.

Script
Download the script from ManifestMaker, or see below for the source code. Remember to make the script executable.

#!/bin/bash

#script which generates a JSON file, which has as keys the filenames of all files in the given directory, and as values the SHA1 digest of the corresponding files

#check number of arguments is as expected
if [ $# -ne 1 ]
then
    echo "Need to provide directory on which to operate"
    exit 1
fi

src="$1"

#check directory exists
if [ ! -d $src ]
then
    echo "Directory $src does not exist"
    exit 2
fi

#change to specified directory
cd $src

#declare target output file
out="manifest.json"

#clean up first
rm $out
touch $out

#find the number of files - needed to know whether to put a "," at the end of each line or not
numFiles=$(ls | wc -l)

#start printing to the file
echo "{" >> $out

index=1
for file in *
do
#check if comma is needed at the end of the line
if [ "$index" -lt "$numFiles" ]
then
endLine=","
else
endLine=""
fi

#gives some unfriendly output formatting
digestFull=$(openssl sha1 $file)

#tidy up formatting to get actual value
digest=${digestFull#*= }

#print line to file
echo "    \"$file\" : \"$digest\"$endLine" >> $out

#increment our index
index=$[$index+1]

done

echo "}" >> $out

Thursday, 12 April 2012

iOS - Integrating KIF Tests with Jenkins CI


Below is the script I use in Jenkins to kick off my automated GUI tests, which are performed using KIF which was developed by Square. KIF serves as an excellent start for running and automating GUI testing. I had to make a small change to KIF to support entering text into text fields which weren't empty; KIF didn't seem to handle this so I made a fix in a fork, located at my KIF.


One of the problems I had initially was that whilst the build would be marked as successful or a failure correctly, there was no feedback on which of the GUI tests failed. My script below solves this problem by outputting the feedback from KIF if and only if the GUI tests fail; if they are successful there is no need to see them in my opinion.


Your script would need to be adapted to contain the correct details for your project/workspace/scheme/target of course, but hopefully this will serve as a good guide.


Waxsim is the required bridging application needed to support integration between KIF and Jenkins. Waxsim has been installed on the server, running under /Applications/waxsim - if you have it installed elsewhere then update the reference accordingly.


#!/bin/sh
#kill simulator if running
killall -s "iPhone Simulator" &> /dev/null
if [ $? -eq 0 ]; then
    killall -KILL -m "iPhone Simulator"
fi


echo "About to build GUI tests scheme"


xcodebuild -scheme "GUITests" -workspace "xxx.xcworkspace" -configuration Debug -sdk "/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/" build CONFIGURATION_BUILD_DIR="/var/hudson/workspace/$JOB_NAME/build" 


echo "About to run waxsim - a long delay here might suggest something is obstructing the view of the simulator on build slave"


/Applications/waxsim "/var/hudson/workspace/$JOB_NAME/build/GUITests.app" > /tmp/KIF-$$.out 2>&1


# WaxSim hides the return value from the app, so to determine success we search for a "no failures" line


echo "Testing finished"


# count the number of times "TESTING FINISHED: 0 failures" is found - 0 means that there was a failure
success=`exec grep -c "TESTING FINISHED: 0 failures" /tmp/KIF-$$.out`


# if there was a failure, show what waxsim was hiding and crucially return with a non-zero exit code
if [ "$success" = '0' ]
then 
    cat /tmp/KIF-$$.out
    echo "==========================================="
    echo "GUI Tests failed"
    echo "==========================================="
    exit 1
else
    echo "==========================================="
    echo "GUI Tests passed"
    echo "==========================================="
fi

Tuesday, 10 April 2012

Script to Kill iPhone Simulator if Running

Instructions to end the iPhone Simulator process if it is running.

As part of a CI setup, it is typical to have multiple iOS build jobs running on an individual build slave. As you can only have a single instance of the iPhone Simulator, you might find some of your jobs fail because another job has started the simulator, and you can't get access to it.

What seems like a sensible approach here, is to kill the simulator if it is already running, meaning your build job is guaranteed to start from a known state. If you attempt to close the simulator when it isn't running, however, you will end up with an error, along the lines of "No matching processes belonging to you were found". That's why you need to check if the process is running first, before attempting to close it.

The following script checks if the simulator is running, and if it is, ends the process. You can do this at the start of each build job.


#!/bin/sh
#kill simulator if running
killall -s "iPhone Simulator" &> /dev/null
if [ $? -eq 0 ]; then
    killall -m -KILL "iPhone Simulator"
fi




And in case you were wondering; this works for the iPad simulator too. The process is the same so the script above works unedited.


If you have any problems getting this going, drop me a comment below.

Thursday, 9 February 2012

Rotating BlackBerry Playbook Simulator

I'll keep this one short and snappy.

If you want to change from the default 'landscape' mode, click and hold the mouse from the bottom right hand edge of the simulator (just anywhere in the black section will do) and drag towards the centre of the screen, and release.

Saturday, 7 January 2012

Could Not Reconnect All Network Drives in Windows 7

I often see the notification balloon popping up from system tray to tell me that my network drives could not reconnect. I typically see this after I've first turned on my computer or after I've booted from Hibernation.



When I open Computer, I can see my network drives, but they are shown with a red cross indicating that there is indeed a problem with the connection to them. Applications which rely on data from these network drives cannot obtain their data, and fail in their own ways. The particularly annoying aspect of this is that there absolutely no problem with the network connection to these drives. Simply double clicking on the drives will open them up and restore the connection. All applications will function correctly thereafter... until the next time I boot up the machine.

Below is a workaround you can use to ensure your remote drives are available for use when you turn on your PC. For my environment, I'm running Windows 7 64-bit and have a Synology DS411J which is a NAS drive. But really, this should work for connecting any remote network drive.