Author Archive

How to integrate WhatsApp into an iOS app

One of the method for your iPhone app to interact with WhatsApp is through a custom URL scheme

The Objective-C call to open one of these URLs is as follows:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];

How to add a label in SpriteKit

We can use the following code to add a label in spritekit

SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
myLabel.text = @"+1";
myLabel.fontColor = [UIColor redColor];
myLabel.position = CGPointMake(200, 100);
myLabel.fontSize = 40;
[myLabel setScale:0.01];
[self addChild:myLabel];

Reading and Opening a File in PHP

PHP readfile() Function

The readfile() function reads a file and writes it to the output buffer.

PHP Open File – fopen()

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

PHP Create File – fopen()

The fopen() function is also used to create a file.

$myfile = fopen("example.txt", "w")

Set background color with rgb format in css

The following code uses the rgb function with .css() method.

         $("td:empty").text("Was empty").css('background', 'rgb(255,220,200)');


If you are using Cocos2d v3, use CCTextField for text input. For example:

CCTextField *enterName = [CCTextField textFieldWithSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"textfield_background.png"]];
enterName.fontSize = 16.0f;
enterName.contentSize = CGSizeMake(100.0f, 50.0f);
enterName.preferredSize =CGSizeMake(100.0f, 50.0f);
enterName.positionType = CCPositionTypeNormalized;
enterName.position = ccp(0.5f, 0.5f);
[self addChild:enterName z:5];

If you are using Cocos2d v2, use UIKit components. For example:

CGSize size = [[CCDirector sharedDirector] winSize];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(size.width * 0.5,
                                                                       size.height* 0.1,
                                                                       100, 100)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[[[CCDirector sharedDirector] view]addSubview:textField];

As a note, Cocos2d v3 actually uses a UITextField in iOS and a NSTextField in Mac.

/** iOS: UITextField used by the CCTextField. */
@property (nonatomic,readonly) UITextField* textField;
#elif defined(__CC_PLATFORM_MAC)

/** Mac: NSTextField used by the CCTextField. */
@property (nonatomic,readonly) NSTextField* textField;

Changes in cocos2d 3.x

CCNodes, CCScenes and CCLayers

CCLayer does not exist in cocos2d v3.x anymore. CCLayer used to be the class you should use, when you wanted to handle touch input by a user. In cocos2d v3.x any CCNode can handle touch input, thus there is no use for CCLayer anymore. CCNode is the new CCLayer. For scenes use CCScene for everything in your scene use CCNode.

Touch handling

For now it is most important to know how to enable touch handling on your CCNode:
self.userInteractionEnabled = TRUE;

How to catch a touch and its touch position:

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
CGPoint touchLocation = [touch locationInNode:self];

//you can put your touch handling code here

And to know the three other methods that can be implemented to determine moving, ending, or canceled touches:

- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event

- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event

- (void)touchCancelled:(UITouch *)touch withEvent:(UIEvent *)event

How to change the font size in UIWebview

Uee the following codein your webViewDidFinishLoad delegate method

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    int fontSize = 20;
    NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
    [webView stringByEvaluatingJavaScriptFromString:jsString];
    [jsString release];

NSString to NSDate

NSString *dateString = @"01-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDate convert to NSString:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", strDate);
[dateFormatter release];

Rename files in Documents directory

To get NSDocuments directory use :

NSString *originalfileName = @"MyFile.txt";
NSString *newfileName = @"MyFileNameChanged.txt";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathOriginal = [documentsDirectory stringByAppendingPathComponent:originalfileName];
NSString *filePathNew = [documentsDirectory stringByAppendingPathComponent:newfileName];

You can use moveItemAtPath to rename.

NSError * err = NULL;
NSFileManager * fileManager = [[NSFileManager alloc] init];
BOOL result = [fileManager moveItemAtPath:filePathOriginal toPath:filePathNew error:&err];
NSLog(@"Error: %@", err);
[fileManager release];

If you do not know the names of the files in documents directory you can use :

NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];

This array contains all filenames you need. So you can rename them all.

Comparing UIButton backgroundImage

We can use the following code to compare a UIButton’s background image

if ([[myButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed@"myImage.png"]]) {
// Button has a background image named 'myImage.png'
// Button has not a background image named 'myImage.png'
Page 1 of 1012345...10...Last »