Thursday 28 February 2013

How to Read And Write Plist File

//Read TopScore.plist  File



-(void)readTopScorePlist
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TopScore.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"TopScore" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
self.MyTopScore = [[data valueForKey:@"TopScore"] intValue];
[data release];
}

//Write To TopScore.plist


-(void)writeToTopScorePlist
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TopScore.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"TopScore" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:path ];
    [data setObject:[NSString stringWithFormat:@"%i",self.MyTopScore] forKey:@"TopScore"];
    [data writeToFile:path atomically:YES];
    [data release] ;
}

Integrating OpenFeint and Admob With Cocos2D iPhone

Two excellent tutorials available on integrating OpenFeint and Admob with Cocos2D iPhone games. For those unfamiliar with it, OpenFeint is a social networking platform for iPhone games that adds some great features such as leaderboards, chat, and challenges. Admob is a mobile advertising solution which was recently purchased by Google that helps in monetizing your app.

The Tutorials:
OpenFeint and Cocos2D iPhone Tutorial
Admob and Cocos2D iPhone Tutorial

OpenFeint Integration With iOs Apps

OpenFeint is a powerful social framework for iOS games. It makes it easy to handle achievements, leaderboards, and sharing. You can even synchronize it with Game Center. This tutorial only covers a basic OpenFeint integration and not any advanced features.

This tutorial starts with the Sparrow scaffold. If you are not using the scaffold, your integration might be a little different.
1. Go to https://api.openfeint.com/dd/signup and sign up for an account.
 
If you already have an account, just create a new game from the developer dashboard.
2. Download the latest SDK from https://api.openfeint.com/dd/downloads.
I am using the OpenFeint iOS 2.12.5 package.
3. Find OpenFeint.framework in the SDK package and add it to your Sparrow project.
Also, add the correct configuration bundles for your project: 

If your game is iPhone landscape only, use OFResources_iPhone_Landscape.bundle. 
If your game is iPhone portrait only, use OFResources_iPhone_Portrait.bundle. 
If your game is iPad only, use OFResources_iPad.bundle. 
If your game is iPhone landscape and portrait, use OFResources_iPhone_Universal.bundle. 
All others use OFResources_Universal.bundle. 

4. Add the following frameworks to your project.
AddressBook 
AddressBookUI 
CFNetwork 
CoreLocation 
CoreText 
GameKit 
libsqlite3.0.dylib 
MapKit 
MobileCoreServices 
Security 
SystemConfiguration  
At this point, you should be able to build and run your app with no OpenFeint related warnings or errors.
5. Add the following code in ApplicationDelegate.h and .m.
ApplicationDelegate.h
#import "OpenFeint/OpenFeint.h"
 
// Add OpenFeintDelegate after UIApplicationDelegate
@interface ApplicationDelegate : NSObject <UIApplicationDelegate, OpenFeintDelegate>
ApplicationDelegate.m
// At the end of applicationDidFinishLaunching
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:UIInterfaceOrientationPortrait], OpenFeintSettingDashboardOrientation,
@"OFSparrow", OpenFeintSettingShortDisplayName,
#ifdef DEBUG
[NSNumber numberWithInt:OFDevelopmentMode_DEVELOPMENT], OpenFeintSettingDevelopmentMode,
#else
[NSNumber numberWithInt:OFDevelopmentMode_RELEASE], OpenFeintSettingDevelopmentMode,
#endif
nil];
[OpenFeint initializeWithProductKey:@"qXprwYNXTJYdg1iT4lK9Eg" andSecret:@"xgVwRmTkhmOn5StvYiOruIDmL8dEEUyCniZgmLTn0o" andDisplayName:@"OpenFeint Sparrow" andSettings:settings andDelegates:[OFDelegatesContainer containerWithOpenFeintDelegate:self]];
 
// Add these methods also
- (void)dashboardWillAppear {
    [mSparrowView stop];
}
 
- (void)dashboardDidDisappear {
    [mSparrowView start];
}
 
- (void)userLoggedIn:(NSString *)userId {
    NSLog(@"User %@ logged into OpenFeint", userId);
}
 
// In the dealloc method
[OpenFeint shutdown];
6. Use these methods to open the OpenFeint dashboard.
// You may need to #import "OpenFeint/OpenFeint+Dashboard.h"
[OpenFeint launchDashboard];
[OpenFeint launchDashboardWithListLeaderboardsPage];
[OpenFeint launchDashboardWithHighscorePage:@"leaderboardID"];
[OpenFeint launchDashboardWithAchievementsPage];
[OpenFeint launchDashboardWithFindFriendsPage];
[OpenFeint launchDashboardWithWhosPlayingPage];
7. Use these methods to submit scores and achievements.
// You may need to #import "OpenFeint/OFHighScoreService.h" and "OpenFeint/OFAchievement.h"
[OFHighScoreService setHighScore:100 forLeaderboard:@"leaderboardID" onSuccessInvocation:nil onFailureInvocation:nil];
[[OFAchievement achievement:@"achievementID"] updateProgressionComplete:100.0f andShowNotification:YES];
8. Yay! You're done.
Here is the sample project from this tutorial. 
Sample Project