»
S
I
D
E
B
A
R
«
Detect iPhone Shakes
Apr 17th, 2010 by admin

iPhone has motion detection feature, to detect if someone shakes an iPhone is easy.

// Ensures the shake is strong enough on at least two axes before declaring it a shake.
// "Strong enough" means "greater than a client-supplied threshold" in G's.
static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
        double
                deltaX = fabs(last.x - current.x),
                deltaY = fabs(last.y - current.y),
                deltaZ = fabs(last.z - current.z);

        return
                (deltaX > threshold && deltaY > threshold) ||
                (deltaX > threshold && deltaZ > threshold) ||
                (deltaY > threshold && deltaZ > threshold);
}

@interface L0AppDelegate : NSObject <UIApplicationDelegate> {
        BOOL histeresisExcited;
        UIAcceleration* lastAcceleration;
}

@property(retain) UIAcceleration* lastAcceleration;

@end

@implementation L0AppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {
        [UIAccelerometer sharedAccelerometer].delegate = self;
}

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

        if (self.lastAcceleration) {
                if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) {
                        histeresisExcited = YES;

                        /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */

                } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) {
                        histeresisExcited = NO;
                }
        }

        self.lastAcceleration = acceleration;
}

// and proper @synthesize and -dealloc boilerplate code

@end
iPhone SDK Proximity Sensor
Apr 17th, 2010 by admin

Although you can’t take advantage of the iPhone’s proximity sensors by using iPhone SDK, You can now access the sensor’s state via UIDevice’s proximityState property. However, it may not be that useful for games, because it is only an on/off thing, not a near/far measure. Another issue is that it’s only available on iPhone and not iPod touch.

Launch Google Maps from another app on iPhone
Apr 16th, 2010 by admin

To launch Google Maps application with a specific address from within my own native iPhone application, use UIApplication’s openURL method, that will perform the normal iPhone magical URL reinterpretation.

For example, the following code will invoke the Google maps app:

[someUIApplication openURL:[[NSURL alloc] initWithString: @"http://maps.google.com/maps?g=London"]]

Sort an NSMutableArray with custom objects
Dec 6th, 2009 by admin

To sort an NSMutableArray with custom objects, you can either implement a compare-method for your object:

- (NSComparisonResult)compare:(id)otherObject {
    return [self.birthDate compare:otherObject];
}

NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];

or even better: (The default sorting selector of NSSortDescriptor is compare:)

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                              ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
int myInt = (int) myFloat;
Programmatically send SMS on the iPhone
Nov 28th, 2009 by admin

To send SMS on the iPhone programmatically, You can use a sms:[target phone number] URL to open the SMS application.

instanceof in Objective-C
Nov 28th, 2009 by admin

There is no instanceof in Objective-C, but there are ways to create the same result:

1. [myObject class] for returning the class of an object.

You can make exact comparisons with:

if ([myObject class] == [MyClass class])

but not by using directly MyClass identifier.

2. Similarily, you can find if the object is of a subclass of your class with:

if ([myObject isKindOfClass:[AnObject class]])

Global Constants in Objective-C
Nov 28th, 2009 by admin

A good way to make constants defined once for the whole app is to create a header file like:

// Constants.h
extern NSString * const MyFirstConstant;
extern NSString * const MySecondConstant;
//etc.

You can include this file in each file that uses the constants or in the pre-compiled header for the project.

You define these constants in a .m file like

// Constants.m
NSString * const MyFirstConstant = @"FirstConstant";
NSString * const MySecondConstant = @"SecondConstant";

Constants.m should be added to your application/framework’s target so that it is linked in to the final product.

The advantage of using string constants instead of #define’d constants is that you can test for equality using pointer comparison (stringInstance == MyFirstConstant) which is much faster than string comparison ([stringInstance isEqualToString:MyFirstConstant]) (and easier to read, IMO).

Base64 library on iPhone SDK
Nov 28th, 2009 by admin

If you’d like to do base64 encoding and decoding, but could not find any support from iPhone SDK, don’t worry. Here’s a nice code sample at the bottom of this post. Very self-contained…

http://www.cocoadev.com/index.pl?BaseSixtyFour

Defining a preprocessor symbol in Xcode
Nov 28th, 2009 by admin

To define a preprocessor symbol in Xcode, go to your Target or Project settings, click the Gear icon at the bottom left, and select “Add User-Defined Setting”. The new setting name should be GCC_PREPROCESSOR_DEFINITIONS, and you can type your definitions in the right-hand field.

the full syntax is:

constant_1=VALUE constant_2=VALUE

Note that you don’t need the ‘=’s if you just want to #define a symbol, rather than giving it a value (for #ifdef statements)

Converting a float to an int in Objective-C
Nov 28th, 2009 by admin

To convert a float to an int in Objective-C is not that much different from the classic C-style, C-style casting syntax works in Objective C, so try this:

int myInt = (int) myFloat;