Tag Archives: iPhone development

Decode HTML characters in iPhone SDK with Objective-C

Just create an interface by following this code.

@interface MREntitiesConverter : NSObject {
NSMutableString* resultString;
}
@property (nonatomic, retain) NSMutableString* resultString;
- (NSString*)convertEntiesInString:(NSString*)s;
@end

@implementation MREntitiesConverter
@synthesize resultString;
- (id)init
{
if([super init]) {
//resultString = [[NSMutableString alloc] init];
}
return self;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s {
[self.resultString appendString:s];
}
- (NSString*)convertEntiesInString:(NSString*)s {
if(s == nil) {
NSLog(@”ERROR : Parameter string is nil”);
}
resultString = [[NSMutableString alloc] init];
NSString* xmlStr = [NSString stringWithFormat:@"%@", s];
NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data];
[xmlParse setDelegate:self];
[xmlParse parse];
NSString* returnStr = [[NSString alloc] initWithFormat:@”%@”,resultString];
[resultString release];
return returnStr;
}
- (void)dealloc {
//[resultString release];
[super dealloc];
}
@end

When you want to decode HTML characters within a string, just call the app like this.

MREntitiesConverter *converter = [[MREntitiesConverter alloc] init];
someStr = [converter convertEntiesInString:@"abcdefghiijXXXYZ"];
[converter release];

Easy enough? ;)


Showing UIAlertView on cocos2d Scene/Layer

This can be done easily just like when you are working with the standard ViewController.

Here is the sample code…

UIAlertView *alert = [[UIAlertView alloc] init];
[alert setTitle:@"My Title"];
[alert setMessage:@"My message"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
[alert show];
[alert release];

With this, you can detect which button was pressed by handling the alertView message like this.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
// Yes, do something
}
else if (buttonIndex == 1)
{
// No
}
}

And when I do this in my game, I’ll pause the game first and resume when player pushed on the button. This can be done easily via sharedDirector as well.

[[Director sharedDirector] pause]; //Pause your game
[[Director sharedDirector] resume]; //Resume your game

Hope you enjoy!


The most easiest method to add a splash screen for your iPhone application

Well done, I don’t know if there is any other framework that let you add a splash screen easier than the iPhone SDK or not. I never see it.

What you want to do is just creating your splash screen image in PNG format which have 320×480 resolution for portrait or 480×320 if your app is running on landscape orientation.

Then name it “Default.png”, please note that you have to name it “Default.png” not “default.png”. It’s case sensitive.

Then just put the file into your project and it’s DONE!

You will see your splash screen until your first view was loaded successfully.

Short & Sweet?
;)


How to capture a screenshot for your app by the app itself

It just likes what you did by running the app and hold the stand-by button then push on the home button.

When you run this code, it will capture the current screenshot and add into the Camera Roll.

It’s not yet included in the official SDK but you can get it run easily.

@interface UIApplication (Extended)
-(void) _writeApplicationSnapshot;
@end
[[UIApplication sharedApplication] _writeApplicationSnapshot];

Yo!