Archive for the ‘xCode’ Category

AppCode File Template for CoreGraphics and PaintCode

Wednesday, November 21st, 2012

In my last post I setup a CALayer subclass for CoreGraphics and PaintCode.

I created an AppCode template that I am sharing here.

The header file template:

Implamentation file template:

 

Vector Graphics in iOS with CoreGraphics and PaintCode from an SVG file

Monday, November 19th, 2012

Since I started iOS development there has always been one thing missing for me. That has been a vector graphic file format. There is no format currently natively supported by apple.

I did some digging and found SVGKit. This is a very nice library that loads in .svg files. It even supports SVG animations. Not to mention its free. So I had to share this. However it can be tricky to set up.

In the end I settled on using PaintCode. PaintCode is a vector based graphics application that generates CoreGraphics Objective-C. PaintCode will also load in SVG files.

The idea is you load an SVG file into PaintCode. Edit it if needed then copy the Objective-C code into your project.

There are different ways you can include it in your projects. I would like to use it in layer of the CALayer flavour.

What I have done is create a CALayer subclass. That all you have to do is past in the PaintCode code.

For convenience I create all my vector assets at 100×100 pixels. I then use a scale to get it to the right size.

This assumes you have PaintCode and have already got the code ready. Also assumes the image is 100×100.

Step 1:
Add the “QuartzCore.framework” to your project.

Step 2:
Create a new file using the “Objective-C Class” template for iOS. USE CALayer Subclass

Step 3:
Add the bellow line to to your Header file(.h).

 

Step 4:

Add the bellow code to the implementation file(.m). Note: see line 17 if your image is not 100×100 pixels

Step 5:

Add the CoreGraphics code to the drawImage method.

Step 6:

Add this layer to a view that is currently on the view stack.

 

Thats all you need.

 

**Edit

I created an AppCode template for this.

 

Using LevelHelper without Box2d for Cocos2d-X

Thursday, November 8th, 2012

I have been trying out LevelHelper recently and it has been very useful when making games that have a fair bit of physics.

I was looking at making a really simple game that did not require physics. Thats where LevelHelper seemed to fall over. Since the editor generates code for you and that code has reference to Box2D.

Fear not for all is not lost, you can still use LevelHelper without implementing the physics. You still need use the Cocos2D-X with Box2D template in xCode. You just don’t attach the physics properties.

So follow the tutorials on the LevelHelper site. When you get to the bellow line of code in the second video:

The first parameter “world” is used to attach the physics properties. The second parameter “this” is what all the image assets in LevelHelper level are attached to. All you need to do is change the above line to:

Now just ignore any code to do with physics and Box2D.

Miller Cylindrical Projection with source for (Long, Lat) to (x, y)

Wednesday, May 2nd, 2012

Recently I have been trying to get a custom map working with Longitude and Latitude that plotted in the x and y in iOS. It also needed to work with a zoom and offset. It was a bit of an argus journey but I got there in the end.

What you will need is a projections type(map type) and the formula for the conversion. Ideally a projection type that compensates for the curvature of the earth.

I settled for a Miller Cylindrical projection. The problem with the maps is finding an accurate map. You can use maps with offsets but getting started it helps if you have an accurate map. I ended up going through a lot of maps before I found an accurate one. It had a white border that I had to crop off but otherwise it was perfect.

DONT FORGET TO CROP THE WHITE BOARDER IF YOU SCALE IT UP

http://en.wikipedia.org/wiki/File:Miller_projection_SW.jpg

I scaled it up for testing purposes up to a height of 6563 and a width 8933 cropped.

Here is the Objective-C code and little C to get he job done:

As you can see the the hight and width is hard coded. What if your map needs an offset and you also need to have a dynamic size to compensate for zoom levels.

The description method

Tuesday, October 11th, 2011

Any object can implement the description method that is used to return a description of the contents of a receiver. The problem i have always found with this method. Is keeping the method up to date during the development process. Also getting other developers working on the project also keeping it up to date.

With that problem in mind I put together this method that will automatically generate the string and list all available properties in the the receiver. It will print out when the instance is used in NSLog or or printed using the GDB debugger commands in my previous post.

You can also find it on my custom xCode 4 templates here on github. Right at the bottom the class.

xCode GDB Debugger Commands

Thursday, September 22nd, 2011

One of the things that I struggled with going from eclipse and Visual Studio to xCode was the the debugger.  Most of the objects have memory references rather than values. For a long time while developing iOS apps all i would get, or though I had was one cryptic error message. Till I discovered po which stands for print object. This allowed me to print out the contents of an object which can be very useful.

Here is  list of gdb commands: (Warning properties are not identified by the debugger so dont use anything with dot notation)

po – print object – can be used to print out the likes of NSStrings and NSDictionaries
use: po someObject

print – prints base types – used to print out to the screen ints floats and the like
use: print myFloat

set – set a variable – allows you to set variables
use:  set someString=@”test”

call – calls a method – can be used to call methods or the properties getters and setters
use: – call [someObjectInstance withString:@”newString”];

n – Next – always you to travers your code during breck
use: n

There are 2 main places you can use the commands.

The first is the consol output during a break or crash just after the gdb:

The second is in a break point first set a break point then click the “Click to add action text” ass seen bellow.

Now you can add you command in the newly available text field that will always be triggered at that point in the code.

Carful with the break point triggers not to confuse your self if you put sets or calls in there.

Setting up xCode 4 Projects

Friday, June 24th, 2011

Every time I start a new xCode project i do it a little differently. I include files in some and settings in others. I have decided to put them all here to make sure i don’t miss any. If you have any other project settings, scripts, snippets or useful utils please drop a comment.

1. Enabling and dealing with Zombies:

This allows you to find errors that come from delloced objects being called upon. That normally just gives you the useless error bad access. with this enabled it should take to a class or function when you see the bad access error. This can be dealt with in 2 ways either enabling zombies in the editor thus allowing you to debug it in xCode or using instruments. If the first method does not work for you try the second.

Method 1 xCode Debug:

  1. get to the run settings by press: cmd+alt+r
  2. from the left nave select “RUN  yourApp.app”
  3. select “Arguments” from the right tab system
  4. expand if not already expanded the environment variables
  5. add NSZombieEnabled the name parameter and YES to the value parameter

Method 2 instruments Debug:

Rather than take you through it Mark Johnson has created a good video that talks you through it.

Check out his blog post for more details: http://www.markj.net/iphone-memory-debug-nszombie/

2. Replacing NSLog with PSLog

PSLog is a NSLog replacment. I have seen it arround for a while now. Basicly it gives you a bit more info like where log is called from. The only problem with it, is the dependancy it creates on having to keep importing PSLog in every one of your projects.

I modified it so you can use NSLog in your code and at compile time in debug mode it replaces your NSLogs with PSLogs. During deploy it replaces NSLog with comments. This way it removes any depandacies on the PSLog function.

How ever this only applies to the PSLog function it self none of its expanded counter parts like PSLogDebug. Using PSLogDebug will maintain a dependancy.

Check it out:

https://github.com/abeazam/PSLog.git

If you #import “PSLog.h” in the .pch file in the support group in your project you wont have import it in every file you want to use it. which Helps the whole dependancy issue.

TODO, FIXME converted to warnings

I duno if you noticed but if you add a //TODO: or //FIXME: comments it appears  in the function menu. The problem with that though is you need to be in the class to see the list. Ideally what I wanted was a list of todos that I can see them all in one place. You can do that by adding a bash script that adds them as warnings.

  1. Select your project in the left hand menu
  2. Select your project in the target right hand menu
  3. Click “Add Build Phase” in the bottom right.
  4. Select “Add Run Script”
  5. Past in the script below and your done 🙂

i think this guy might have come up with it: http://deallocatedobjects.com/2011/05/11/show-todos-and-fixmes-as-warnings-in-xcode-4/

Snippets
Snippets are one of the things that stood out to me in xCode. Here are a few of mine:

Completion shortcut – log
Completion scope – function and method

Completion shortcut – imp
Completion scope – processor directive

If you want to make a variable just stick them in <#here#>.

Crash Log Server

http://quincykit.net/
https://github.com/therealkerni/QuincyKit

Sends the crash report to a server with a single line of code. I have not tried the push notifacsion but every thing else seems to work resonably.

http://quincykit.net/

Please let me know if you find anything else that helps keep a project in shape.

Code Based Localisation

Friday, August 14th, 2009

Code Based Localisation in xCode 3.0+ SDK

Code based localisation focuses on any strings you may have in your code. Finding these strings and making them dynamic. This is a contrast to how localisation is done in say Flash where the localisation methods are normally set up by the developer. Kind of like Flex.

If you are after NIB Localisation checkout this post here

Abstract:
A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the language like “English”, “French” or “Japanese”. The second is the local project abbreviation “.lproj” putting them together you get folders by the name “English.lproj”, “French.lproj” or “Japanese.lproj”. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.

Basically any text that is set via code needs to be set using NSLocalizedString this takes 2 strings. The first is the the copy that needs localisation. The second string it takes is the comment that appears above the name value pair.  see example:

Once you have written your code using NSLocalizedString with all the necessary default copy you will run a terminal script that will pull out the default strings strings to create a .strings file which you would then localise. “genstrings” is the command line tool used to interrogate your .m files to find what needs to be localised.

Once you have your localised .strings file you simply copy it to the different local folders updated the copy to the respective local and bobs  your uncle, its done.

Implementation:

  1. create a new project in xcode it should not matter what type but I will use an iPhone View-Based Application.
  2. add the bellow code to the viewControler of the newly created project to quickly add some uilabels
  3. Open the terminal window and navigate too the root of you project folder. This is the folder that contains your Classes folder.
  4. Run “genstrings ./Classes/*.m” in the terminal which will scan every .m file in your Classes folder for any references to NSLocalizedString and added them to a Localizable.strings file which should appear in the root of your project folder.
  5. In the finder navigate to the root of the project folder and then drag and drop Localizable.strings from the Finder to the resource folder in XCode remembering to insure that the “Copy items into…” check box is unchecked. Also make sure that Text Encoding is set to UTF-16 to insure all languages are covered.
  6. Finally press add.
  7. Right-Click the newly added Localizable.strings then select “Get Info”.
  8. Select the General tab from the “Get Info” panel
  9. Click “Make file Localizable”
  10. Click back on the general tab
  11. Click “Add Localization”
  12. Enter your new Locals “French”(from the English name section of the link) and or see here for full list: http://www.loc.gov/standards/iso639-2/php/English_list.php
  13. Once you have added the Locals for Localizable.strings close the “Get Info” window
  14. Run your app go to the settings panel>>Genral>>International change your local and it should display your localised text.

NIB Localisation

Friday, August 7th, 2009

NIB Localisation xCode 3.0+ SDK

There are 2 ways to localizes iPhone application and regular OSX applications. The first is localizing the NIB file. This automatically hard codes the localized copy into a NIB file. This works as of iPhone SDK 3.0 should work on previouse versions and future versions but that was my version at the time.

You can also localise with code but that is not the focus of this point check back for Code based localisation maybe next week.

Abstract:
A localised project needs a local specific folder that follows a name convention made up of 2 parts. First is the Language, like “English”, “French” or “Japanese”. The second is the local project abbreviation lproj. Putting them together you get “English.lproj”, “French.lproj” or “Japanese.lproj”. All the local based copy and assets will end up in the respective local folder. You can create these folders manually but I like to get xCode to create them using the localisation functionality built into the IDE.

NIB localisation works by taking a NIB that was built for one local with all the copy hard-coded into the NIB then scanning the NIB with the command line tool ibtool. Extracting the copy to a strings file. The strings file is then copied and localised for each local. Then another script hard codes the localised strings into the respective NIBs. This process only uses the generated strings files once to get the NIBs localised. Once this is done the Strings files are no longer necessary.

Implementation:

  1. create a new project in xCode it should not matter what type but I will use an iPhone View-Based Application.
  2. open up the view controller NIB in interface builder add some labels and buttons add some copy to them
  3. save and close interface builder
  4. in xCode right click the view controller NIB and click “Get Info”
  5. make sure you are in the genral tab then click “Make file Localizable”
  6. select the “General” tab again as may change after clicking “Make file Localizable”
  7. click “Add Localization”
  8. Enter your new Locals like “French” from the English name section of the link see here for full list: http://www.loc.gov/standards/iso639-2/php/English_list.php
  9. close “Get Info” Panel
  10. open up terminal
  11. navigate to your xCode project folder in terminal
    1. now we are going to run the command line script above to scan your nib file and generate a strings file below I have broken down the command so you can replace what u need
    2. I will use    ibtool –generate-strings-file NIBCopy.strings English.lpoj/LocalizationNIBViewController.xib
    3. ibtool is the command line tool called Interface builder tool that is installed by default when u install xCode
    4. –generate-strings-file   is the ibtool command that creates a strings file from a nib
    5. NIBCopy.strings is the name I  have chosen for my strings file u can  rename it if you want but maintain the .strings at the end
    6. English.lpoj/LocalizationNIBViewController.xib    is the path from the root of my xCode project folder to the NIB file i want to get scanned you must rename this to match the path and name of your NIB
    7. in your case you should only have to replace the NIB path from sub step 6
    8. so you should end up with something similar to ibtool –generate-strings-file NIBCopy.strings English.lpoj/LocalizationNIBViewController.xib
    9. it is likely that you will get the error “Unable to find either a loadable database or a Nodes.xml configuration file” i get it too don’t worry about it assuming your strings file was generated it will contain the name value pairs from the nib
    10. check to make sure your NIBCopy.strings is created if so move on else go back to sub step one and try again maybe type out the whole request manually in-case copy pasting messed it up a bit
  12. now copy the NIBCopy.strings to the French folder and any other locals folder you have created
  13. update and localise the NIBCopy.strings in the respective folders
  14. once again load up the terminal window
  15. navigate to the root of your project folder in the terminal
    1. now we are going to run the command line script above to take your NIBCopy.strings that contains your localised copy and hard code it into your localised NIBs
    2. ibtool is the command line tool called Interface builder tool that is installed by default when you install xCode
    3. –strings-file the ibtool command that refers to a strings file
    4. French.lproj/NIBCopy.strings my french localised strings file
    5. –write the command that writes a NIB file based on a Strings file and a NIB file
    6. French.lproj/LocalizationNIBViewController.xib the NIB file that will be writen/updated with the localised copy
    7. English.lproj/LocalizationNIBViewController.xibthe NIB file that will be the template for the localised french NIB file above
    8. it is likely that you will get the error “Unable to find either a loadable database or a Nodes.xml configuration file” i get it too don’t worry about it in fact that is how i know it worked 😉
    9. repeat sub step 1 to 7 for each localised NIB file local
  16. to test this either open up the localised NIB in interface builder or run the app and change the language settings in the simulator or on your developer iPhone
  17. the strings files are no longer needed as the copy is now hard-coded into the NIB

Extending(Subclassing) UIButton in Objective-C

Friday, July 10th, 2009

After playing around with UIButton class for a while I found Apple have been very sneaky with some  of their UI classes.

What it appears they have done is use the UIButton class as a sort of wrapper for a number of private button classes. “UIRoundedRectButton” is one of those classes.

“UIRoundedRectButton” is a private class so it can not be extended(subclassed). The initilizers normaly return an instance of self and if that self is not the class type trying to hold it there will be trouble. The problem is if you initialise UIButton with buttonWithType the class it returns is a private class. If you try and extended the UIButton once your custom class is initialized it returns a “UIRoundedRectButton” object which is private so you can not contain it.

If you try the code below which uses the description you can see the object description is not UIButton.

In short you can not extend UIButton and initialize it with buttonWithType  in Objective C. On the other hand you can extened UIButton for a button in interface builder. If  you use interface builder you need to use initWithCoder as the initialiser. While interface builder does the private stuff.