You Are Here Home > iPhone SDK

iPhone SDK

iPhone: libpng error: CgBI: unknown critical chunk

If you get this error when trying your app on the device, you have PNG compression on in Xcode, in Xcode 4 goto “Build Settings” search for PNG in the search box and turn off PNG compression…

iPhone: libpng error: CgBI: unknown critical chunk
Comments (1)   Filed under: C Programming,iPhone SDK,Mac Programming,Xcode   Posted by: Hamid

SQLite And The Problem Of Storing Long Long Integers

Recently, I was trying to store a very large integer value in a SQLite column, it didn’t matter wether I used INTEGER or UNSIGNED BIG INT, SQLite rounded it for me and I was left with an integer value that wasn’t even close to what I needed.

So one solution was to just use a VARCHAR or TEXT or event a BLOB and insert the number with quotes around it but guess what? SQLite did it again!!!

It turns out that SQLite tries to convert your values to ints to give you some benefits with sorting and comparing etc. – in case you didn’t know you really wanted an int rather than a BLOB – and that happens here in one of it’s functions:

/*
** Try to convert a value into a numeric representation if we can
** do so without loss of information.  In other words, if the string
** looks like a number, convert it into a number.  If it does not
** look like a number, leave it alone.
*/
static void applyNumericAffinity(Mem *pRec){
  if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
    double rValue;
    i64 iValue;
    u8 enc = pRec->enc;
    if( (pRec->flags&MEM_Str)==0 ) return;
    if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
    if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
      pRec->u.i = iValue;
      pRec->flags |= MEM_Int;
    }else{
      pRec->r = rValue;
      pRec->flags |= MEM_Real;
    }
  }
}

The solution to this issue is to prepend a 0 to your number and use a BLOB or TEXT type column and insert it like this: ’0THEVERYLARGENUMBER’

Don’t get me wrong, SQLite is a great library that I’ve been using, it’s very well tested and works great, it’s fast and thread safe, I specially like it’s licensee:

/*
** 2001 September 15
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************

I hope this helps…

SQLite And The Problem Of Storing Long Long Integers

Objective-C: A Function For Escaping Values Before Inserting Into SQLite

- (NSString *)escape:(NSObject *)value {
    if (value == nil)
        return nil;
    NSString *escapedValue = nil;
    if ([value isKindOfClass:[NSString class]] || [value isKindOfClass:[NSMutableString class]]) {
        NSString *valueString = (NSString *) value;
        char *theEscapedValue = sqlite3_mprintf("'%q'", [valueString UTF8String]);
        escapedValue = [NSString stringWithUTF8String:(const char *)theEscapedValue];
        sqlite3_free(theEscapedValue);
    } else
        escapedValue = [NSString stringWithFormat:@"%@", value];
    return escapedValue;
}
Objective-C: A Function For Escaping Values Before Inserting Into SQLite

Xcode: Embedding Lua In iPhone Apps

This is very simple, even though I can’t find so many resource regarding this online; follow these steps:

1- Download the latest version of Lua from: http://www.lua.org/ftp/

2- Obviously unpack it and find the “src” folder.

3- Delete the files: “lua.c”, “luac.c” and “print.c”

4- Grab the “src” file and drop it in your project’s left pane, under your projects’s name along with “Classes”, “Resources” etc.

5- Rename this newly added “src” to “lua”

Now you should be able to follow these types of tutorials:
http://www.ibm.com/developerworks/opensource/library/l-embed-lua/index.html

Hope this works for you!

Xcode: Embedding Lua In iPhone Apps
Comments (0)   Filed under: C Programming,iPhone SDK,Lua,Xcode   Posted by: Hamid

NSNotificationCenter Event Name List

I don’t know why I can’t find anything about this online but here are some of the events that the application sends out through NSNotificationCenter:

UIApplicationDidFinishLaunchingNotification
UIApplicationDidBecomeActiveNotification
UIApplicationWillResignActiveNotification
UIApplicationDidReceiveMemoryWarningNotification
UIApplicationWillTerminateNotification
UIApplicationSignificantTimeChangeNotification
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
UIApplicationStatusBarOrientationUserInfoKey
UIApplicationWillChangeStatusBarFrameNotification
UIApplicationDidChangeStatusBarFrameNotification
UIApplicationStatusBarFrameUserInfoKey

Use them like:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod) name:UIApplicationWillResignActiveNotification object:nil];

It’s obvious what they are but if you have a question post a comment…

NSNotificationCenter Event Name List
Comments (0)   Filed under: iPhone SDK,Objective-C   Posted by: Codehead

iPhone SDK: CGAffineTransformInvert and iPhone 4

So if you tried a piece of code like:

CGAffineTransform t0 = CGContextGetCTM(context);
t0 = CGAffineTransformInvert(t0);
CGContextConcatCTM(context, t0);

You know that in iPhone 4, your drawings will be smaller, and if you don’t use this bit of code, your origin is not in the lower left corner but in top left.

The reason for this is that t0 is a transformation matrix, Core Graphics will multiply every point by this matrix to find it’s location on the screen.

Read more about this matrix here:

http://iphonedevelopment.blogspot.com/2008/10/demystifying-cgaffinetransform.html

This matrix is a 3×3 matrix like this:

| a  b  0 |
| c  d  0 |
| tx ty 1 |

Now if print these before the call to CGAffineTransformInvert values to console, you will see this for iPhone 4:
a: 2.000000 – b: 0.000000 – c: 0.000000 – d: -2.000000 – tx: 0.000000 – ty: 960.000000

And after:
a: 0.500000 – b: 0.000000 – c: 0.000000 – d: -0.500000 – tx: -0.000000 – ty: 480.000000

You can see that a and d are used to scale the shapes you draw and CGAffineTransformInvert changes these values to a smaller value since iPhone 4 has much more pixels in it’s display and is capable of better resolutions.

Here are the values for iPhone 3:
a: 1.000000 – b: 0.000000 – c: 0.000000 – d: -1.000000 – tx: 0.000000 – ty: 480.000000

And after the call to CGAffineTransformInvert:
a: 1.000000 – b: 0.000000 – c: 0.000000 – d: -1.000000 – tx: -0.000000 – ty: 480.000000

The Solution

(Or at least I’m using this until I find a better solution) Add this to your code:

CGAffineTransform t0 = CGContextGetCTM(context);
 
    CGFloat xScaleFactor = t0.a > 0 ? t0.a : -t0.a;
    CGFloat yScaleFactor = t0.d > 0 ? t0.d : -t0.d;
    t0 = CGAffineTransformInvert(t0);
    if (xScaleFactor != 1.0 || yScaleFactor != 1.0)
        t0 = CGAffineTransformScale(t0, xScaleFactor, yScaleFactor);
    CGContextConcatCTM(context, t0);

This code will check the value of t0.a and t0.d in the matrix and if it’s not equal to one, it scales the matrix to match the original values. It also ensures that the xScaleFactor and yScaleFactor values are positive.

Again, this looks nasty but works, if there is a better way, please let me know…

iPhone SDK: CGAffineTransformInvert and iPhone 4
Comments (1)   Filed under: iPhone SDK   Posted by: Codehead

iPhone SDK: Base SDK Missing

Here is a solution that will probably fix your issue:

First go to: Project > Edit Project Settings > Build Tab > Edit the value for Base SDK
Then go to: Project > Edit Active Target > Build Tab > Edit the value for Base SDK here too

This should fix it…

iPhone SDK: Base SDK Missing
Comments (0)   Filed under: iPhone SDK   Posted by: Codehead

iPhone SDK: didFinishLaunchingWithOptions Is Not Being Called – Skipped

Double-Click on MainWindow.xib then select ‘File’s Owner’, then go to:

Tools > Connections Inspector

Under outlets you will see ‘Delegate’ drag from the circle next to it to your “APP_NAME App Delegate” icon, save and try again…

iPhone SDK: didFinishLaunchingWithOptions Is Not Being Called – Skipped
Comments (1)   Filed under: iPhone SDK   Posted by: Codehead