Search This Blog

Sunday, 27 November 2011

Read/Write text to File in iPhone/iPad app

Here we are sharing how to Create a file and write in it.
 Just copy and paste into .m file in your project

//This is our Writing part:

NSArray *filepath =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   
    NSString *documentsdirectory=[filepath objectAtIndex:0];
    NSLog(@"Document directory>>>%@",documentsdirectory);
    NSString *filename=[NSString stringWithFormat:@"%@/myfile.txt",documentsdirectory];
    NSString *content=@"One\nTwo\nThree\nFour\nFive";
    [content writeToFile:filename atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil];

//Writting part end.

//This our Reading part
NSArray *path= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentdir=[path objectAtIndex:0];
    NSString *filename=[NSString stringWithFormat:@"%@/myfile.txt",documentdir];
    NSString *cont=[[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:nil];
    NSLog(@"content>>%@",cont);

Reading part end.

Thursday, 3 November 2011

Generate Custom Keyboard (Buttons)



Hello Friend!
Here we are going to generate custom keyboard
//Copy and paste buttongenerate void function in your .m file of your project

-(void)buttongenerate
{
int xposition=0;
int yposition=0;
for (int i = 0; i < 26; i++) 
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
  [btn setFrame:CGRectMake(xposition, yposition, 30, 30)];
[btn setTitle:@"@" forState:UIControlStateNormal];
[btn setTag:i];
  [btn addTarget:selfaction:@selector(buttonclick:)
                      forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btn];
xposition=xposition+30;
if(i == 9 || i==19)
{
yposition=yposition+30;
xposition=0;
}
}

}



//Also copy and paste buttonclick function in your project below or above the buttongenerate function
//This function is called on the action of button clicked which is generated




-(void)buttonclick:(UIButton *)sender
{
NSLog(@"Button TAG>>%i",sender.tag);
}
Thats finish.

Wednesday, 2 November 2011

Facebook Integration in your application



Download FacebookSDK and copy in your project.


Now import facebook headers file in your .h file


#import "Facebook.h"
#import "FBConnect.h"
#import "FBRequest.h"

//Add these four delegate in .h file
<FBSessionDelegate,FBDialogDelegate,FBLoginDialogDelegate,FBRequestDelegate>

like:
@interface FacebookViewController : UIViewController <FBSessionDelegate,FBDialogDelegate,FBLoginDialogDelegate,FBRequestDelegate>{

//Now declare variables in .h file
Facebook *facebook;
NSArray *permissionsArray;

Now come to .m file and add this on the top after @implementaton
static NSString* kAppId = @"227448770601908";

//Now copy and paste from facebook start to facebook end

///facebook start
#pragma mark -
#pragma mark Facebook delegate methods

-(void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response{
NSLog(@"Response : %@", [[response URL] absoluteString]);
}

-(void)request:(FBRequest *)request didLoad:(id) result {
NSLog(@"result:%@",result);
NSLog(@"result:%@",[result objectForKey:@"name"]);
}

- (void)request:(FBRequest *)request didFailWithError:(NSError *) error {
NSLog(@"result:%@",[error description]);
}

-(void)fbDidLogin {
NSString *publishString = [NSString stringWithFormat:@"Blogs"];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Photo Rating",@"text",@"http://iphone-app-dev-beginners.blogspot.com/",@"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];

NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys:
@"image", @"type",
@"http://iphone-app-dev-beginners.blogspot.com/", @"src",
@"http://iphone-app-dev-beginners.blogspot.com/", @"href",
nil];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
@"PottyLegs for iPhone!", @"name",
@"http://iphone-app-dev-beginners.blogspot.com/", @"href",
@"string",@"description",
[NSArray arrayWithObjects:imageShare, nil ], @"media",
nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
//    NSLog(attachmentStr);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  kAppId, @"api_key",
  @"Share on Facebook"@"user_message_prompt",
  actionLinksStr, @"action_links",
  publishString, @"message",
  attachmentStr, @"attachment",
  nil];
[facebook dialog: @"stream.publish"
  andParams: params
andDelegate:self];
}

//// data receive from server end

-(void)fbDidNotLogin:(BOOL)cancelled {
NSLog(@"did not login");
}

- (void)fbDidLogout {
}

//facebook end

//call facebook function in your Button action
-(IBAction)FacebookButtonPressed
{
facebook = [[Facebook alloc] initWithAppId:kAppId];
[facebook authorize:permissionsArray delegate:self];
}



SMS Sending Integration



In this post i am going to guide for integration of SMS in your applicaiton.
so here we go!

//Import in your .h file

#import <MessageUI/MessageUI.h>

//Add framework
MessageUI.framework

//copy and paste these two functions in your .m file

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"MyApp" message:@"Unknown Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];    
[alert release];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
-(void)SMSComposer
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = @"I am busy at the moment!";
controller.recipients = [NSArray arrayWithObject:@"Cell no"];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
[controller setSelectedScopeButtonIndex:1];
}
}

//Now you just have to call that function in your Button Action
//Just like:

-(IBACTION)ButtonSMSPressed
{
   [self SMSComposer];
}
finish.

My Practice Projects

Download Free Hand Painting Project
Painting


Download  Open twenty one website in single click
WebsitesBank


Download "In this project i used google map,Read string from file and string comparison, appending string"
Mix project


Download XML Parsing project
XmlParsing


Download A to Z word Puzzle Game
Word Puzzle Game

Twitter Integration In Your App



Hello Friends!
In this post i am going to guide about Twitter integration in iphone application with some easy steps you just have to copy paste. so here we go!

Download Twitter SDK folder and add in your project where your classes show on lefts side.

import headers files in your .h file
#import "SA_OAuthTwitterEngine.h"
#import "SA_OAuthTwitterController.h"

add these two delegate in .h file
<SA_OAuthTwitterEngineDelegate,SA_OAuthTwitterControllerDelegate>

example:

@interface TwitterViewController : UIViewController <SA_OAuthTwitterEngineDelegate,SA_OAuthTwitterControllerDelegate>{

then declare twitter engine variable in .h file
SA_OAuthTwitterEngine *_engine;


Right click on Frameworks on lefts side of the window then select Add
then select Existing Frameworks. Select 
libxml2.dylib

Now Goto Project in pulldown menu and select Edit Active Target, and search Header Search Path
add this line 
$(SDKROOT)/usr/include/libxml2

Now we have to add delegate function  in .m file
just copy and paste from twitter start to twitter end

//Twitter start
- (void)Twitter:(BOOL)animated {
if(_engine) {
return;
}
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = @"9wHWtHz6C52dkBAsqRO8Q";
_engine.consumerSecret = @"qO8E2ags3WrGLO6ddRJVePabfxUnax8GYznHzNA";
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller){
[self presentModalViewController: controller animated: YES];
}
else {
[self updateStream:nil];
}
}

-(IBAction)updateStream:(id)sender {
[_engine getFollowedTimelineSinceID:1 startingAtPage:1 count:100];
}

#pragma mark SA_OAuthTwitterEngineDelegate

- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: data forKey: @"authData"];
[defaults synchronize];
}

- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username {
return [[NSUserDefaults standardUserDefaults] objectForKey: @"authData"];
}

#pragma mark SA_OAuthTwitterController Delegate

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(@"Authenticated with user %@", username);
[self updateStream:nil];
}

- (void) OAuthTwitterControllerFailed: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Failure");
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Fail !" message:@"Authentication Failure !" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
_engine =nil;
}

- (void) OAuthTwitterControllerCanceled: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Canceled");
_engine =nil;
}

#pragma mark MGTwitterEngineDelegate Methods

- (void)requestSucceeded:(NSString *)connectionIdentifier {
NSLog(@"Request Suceeded: %@", connectionIdentifier);
[_engine sendUpdate:[NSString stringWithFormat:@"http://iphone-app-dev-beginners.blogspot.com/"]];
[_engine clearAccessToken];
_engine = nil;
}

- (void)receivedObject:(NSDictionary *)dictionary forRequest:(NSString *)connectionIdentifier {
NSLog(@"Recieved Object: %@", dictionary);
}

- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier {
NSLog(@"Direct Messages Received: %@", messages);
}

- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier {
NSLog(@"User Info Received: %@", userInfo);
}

- (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)connectionIdentifier {
NSLog(@"Misc Info Received: %@", miscInfo);
}

///twitter end

Last step you have to call twitter function in your Button action
like this:
-(IBAction)ButtonTwitterPressed
{
    [self Twitter:YES];
}


Download Complete Project: FacebookTwitter Example app