Saturday, June 11, 2011

Google Analytics in iPhone SDK

Hello Friends,

Today i would like to introduce one important topic about tracking your iPhone or iPad application with one of the best fee google analytical tool. This facility provides you complete tracking with many more functionalities. This analytical tool let you track any event in your application happens. We can analyze which screens of our application are visited most. And all such information we can access from google's website. We can filter out information for better understanding our application usage world wide.

Google provides different graphs and charts to analyze data. Google analytic SDK provides easy integration with application. We have to just add one framework and one header file which is available here.

Drag framework and header file given in sdk in your project and add the following code.
Import "GNATracker.h" file in App delegate class and write following code in didFinishedLaunching method of App delegate class.

// **************************************************************************
  // PLEASE REPLACE WITH YOUR ACCOUNT DETAILS.
  // **************************************************************************
  [[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-00000000-1"
                                         dispatchPeriod:kGANDispatchPeriodSec
                                               delegate:nil];
  NSError *error;

  if (![[GANTracker sharedTracker] setCustomVariableAtIndex:1
                                                       name:@"iPhone1"
                                                      value:@"iv1"
                                                  withError:&error]) {
    NSLog(@"error in setCustomVariableAtIndex");
  }


Above code initiate your account and connection with google analytics. Now following code will be written at place where event or action should be tracked.

if (![[GANTracker sharedTracker] trackEvent:@"Application iPhone"
                                       action:@"Launch iPhone"
                                        label:@"Example iPhone"
                                        value:99
                                    withError:&error]) {
    NSLog(@"error in trackEvent");
  }

Tuesday, May 31, 2011

Download video from URL and Save it in iPhone SDK

Hello Friends,

Today we will discuss for downloading video from particular URL and save it to application sandbox. Generally video files are vary large, so we need efficient way to perform such task. I used to follow the following method.

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setHTTPMethod:@"GET"];
NSError *error;
NSURLResponse *response;

NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *videosFolderPath = [documentFolderPath stringByAppendingPathComponent:@"videos"]; 

Now we need to check that video folder is exist or not. If not, then we will create it.

BOOL isDir;
if (([fileManager fileExistsAtPath:videosFolderPath isDirectory:&isDir] && isDir) == FALSE) {
    [[NSFileManager defaultManager] createDirectoryAtPath:videosFolderPath attributes:nil];
}


NSData *urlData;
NSString *downloadPath = @"http://foo.com/videos/bar.mpeg";
[request setURL:[NSURL URLWithString:downloadPath]];
urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *filePath = [videosFolderPath stringByAppendingPathComponent:@"bar.mpeg"];
BOOL written = [urlData writeToFile:filePath atomically:NO];
if (written)
    NSLog(@"Saved to file: %@", filePath);

Have a nice day !!

Saturday, May 28, 2011

Mail sending fuctionality in iPhone SDK (MFMailComposer)

Hello Friends,

Today we are going to discuss very use full topic in iPhone SDK. Many times we need to send an email from iPhone application. For this functionality apple has provided very good feature of MFMailComposer which is given by apple in iOS.  To integrate mail sending functionality from your application follow the following steps.

  • Add MessageUI.framework in your project.
  • Import following files:
  • #import <MessageUI/MessageUI.h>
    #import <MessageUI/MFMailComposeViewController.h> 
  • Add delegate in your header file like <MFMailComposeViewControllerDelegate>
  • Now write following methods in the implementation file and set send mail action on any button you want.

-(IBAction)btnEmailClick
{
 Class mailclass =(NSClassFromString(@"MFMailComposeViewController"));
 if(mailclass != nil)
 {
  if ([mailclass canSendMail])
  {
   [self displayComposerSheet];
  }
  else
  {
   [self launchMailAppOnDevice];
  }
 }
 else
 {
  [self launchMailAppOnDevice];
 }
}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{ 
 switch (result)
 {
  case MFMailComposeResultCancelled:
  {
   break;
  }
  case MFMailComposeResultSaved:
  {
   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Mail successfully saved!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
   [alert release];
   break;
  }
  case MFMailComposeResultSent:
  {
   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Mail sent successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
   [alert release];
   break;
  }
  case MFMailComposeResultFailed:
  {
   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Mail sending failed!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   [alert show];
   [alert release];
   break;
  }
  default:
  {
   break;
  }
 }
 [self dismissModalViewControllerAnimated:YES];
}


-(void)displayComposerSheet
{
 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self;
 [picker setSubject:@""]; 
 NSString *emailBody = @"Hello World";
 [picker setMessageBody:emailBody isHTML:NO];
 
 [self presentModalViewController:picker animated:YES];
    [picker release]; 
}
-(void)launchMailAppOnDevice
{
 NSString *recipients = @"mailto:first@example.com?cc=second@example.com,third@example.com&subject=Hello from California!";
 NSString *body =@"Hello World";
 
 NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
 email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
 
}

Everything in above code is understandable and easily customizable. So change the code according to your requirements. Yon can set default recipients,subject and mail according to your application requirements. Still if you have any doubts, please feel free to ask me.

Thanks. Enjoy.. :-)