Tuesday, July 31, 2012

MFMailComposeViewController send pdf file

Direct pdf file send to MFmailcomposer:--

NSString *filePath = [[NSBundle mainBundle] pathForResource: @"file" ofType: @"pdf"];
    NSData *pdfData = [NSData dataWithContentsOfFile: filePath];
    [mailController addAttachmentData: pdfData mimeType:@"application/pdf" fileName:@"file.pdf"]


NSData for pdf file send to MFmailcomposer:--





MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
        mailController.mailComposeDelegate = self;

        [mailController setSubject:@"Subject"];

        [mailController addAttachmentData:pdfData
                                 mimeType:@"application/pdf"
                                 fileName:@"file.pdf"];

        [mailController setMessageBody:@"Body" isHTML:NO];
        [self presentModalViewController:mailController animated:YES];
        [mailController release];





Attach a CSV file to a MFMailComposeViewController:--



MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:@"CSV File"];        
    [mailer addAttachmentData:[NSData dataWithContentsOfFile:@"PathToFile.csv"]
                     mimeType:@"text/csv" 
                     fileName:@"FileName.csv"];
    [self presentModalViewController:mailer animated:YES];

MFMailComposer sending attached image size increased:==
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
if (picker !=nil) { 
   picker.mailComposeDelegate = self; 
   NSString *msgTitle;
   [picker setToRecipients:[NSArray arrayWithObjects:@"", nil]]; 
   [picker setSubject:msgTitle];    
   [picker addAttachmentData:UIImageJPEGRepresentation(imageView.image,0.5) mimeType:@"image/png" fileName:@"img"]; 
   [self presentModalViewController:picker animated:YES];
 
NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality)

//Replace this snippet to your code
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
if (picker !=nil) { 
   picker.mailComposeDelegate = self; 
   NSString *msgTitle;
   [picker setToRecipients:[NSArray arrayWithObjects:@"", nil]]; 
   [picker setSubject:msgTitle];    
   [picker addAttachmentData:UIImageJPEGRepresentation(imageView.image,0.5) mimeType:@"image/png" fileName:@"img"]; 
   [self presentModalViewController:picker animated:YES];

Adding a Retina-ready icon to your Mac app in three easy steps


I've been doing more OS X development recently, and coming from the iOS world, there's a lot that's familiar but I still stumble over many things. Creating an app icon is one of them. After some poking around, I've discovered what you need to do to create a custom icon for your modern Mac app.

Step 1: Create the icon images
Modern Mac icons pack five different resolutions in one.icns file, from 16x16 to 512x512:
  • icon_16x16.png
  • icon_32x32.png        
  • icon_128x128.png
  • icon_256x256.png
  • icon_512x512.png
The dimensions are actually "points" rather than "pixels". On standard resolution displays, 1 point == 1 pixel; iOS developers are already familiar with handling "retina" resolution displays where 1 point == 2 pixels. These double resolution "retina" resources get "@2x" added to the base filename, so in addition to the five standard resolution icon images, you now need five double resolution ones:
  • icon_16x16@2x.png
  • icon_32x32@2x.png
  • icon_128x128@2x.png
  • icon_256x256@2x.png
  • icon_512x512@2x.png
The names describe the icon size in points, so icon_16x16@2x.png is 32 by 32 pixels andicon_512x512@2x.png is 1024 by 1024 pixels. Some of these images have equivalent pixel sizes: icon_32x32.png and icon_16x16@2x.png are both 32 by 32 pixels. You may be able to get away with using the same bitmap in many cases, but you (or your icon designer) may want to tweak each version to look best on their respective display types.

Step 2: Add an .iconset to the Xcode project
In the olden days, you would use the Icon Composer app to build your .icns file, but it's no longer being updated by Apple and doesn't support double resolution images. Today, Xcode will build your .icns file automatically for you. (Alternately you can use theiconutil command line utility to build .icns with high res images or to extract images from .icns files; see Apple's High Resolution Guidelines for OS X for details.)

The trick to making Xcode build your .icns automatically is to put your set of icon images in a folder named .iconset, where  becomes the base name for your.icns file. Add this folder to your Xcode project and Xcode will then create.icns when you build, and automatically copy it to your app bundle.

Note that the images in the .iconset directory need to have the names given above: "icon_16x16.png" through "icon_512x512@2x.png" or you will see a warning in Xcode when building your project and the mis-named images won't be included.

Step 3: Add the icon to the Info.plist
To set your app's main icon, add the "Icon File" key to your app's Info.plist file and set the value to  (without the .icns extension). If you used the standard Xcode template to create your app, your Info.plist will be named "-Info.plist", where  is your app or project name. For those of you who like to edit your.plist files as XML, the key name is CFBundleIconFile.

You can also drag the .iconset directory from the finder or the Xcode project navigator to the "App Icon" pane of the Summary tab for your app's target (pictured above). If you use this approach, Xcode will insist on copying the .iconset into the root folder of your project, even if you've already added the .iconset somewhere else in your project's directory tree (another great example of Apple's attention to detail.) If you're not fussy about your project organization, you can simply let Xcode have its way; otherwise you can remove the copy Xcode makes and add the.iconset in a more appropriate place, like the Resources folder; just make sure to leave the "Icon File" key in your Info.plist and Xcode will show the icon in the "App Icon" pane.

Easy as 1-2-3
Not hard once you figure it out, and a nicer workflow than having to wrestle with a half-baked special purpose app like Icon Composer.

Sunday, July 22, 2012

Objective-C Programming - Blocks - Coding

Objective-C Programming - Blocks - Coding

Objective-C blocks are C-level constructs much like inline functions, anonymous functions, clojures or lambdas in other programming languages. They can also be viewed as function pointers but their design is cleaner and more elegant. Blocks were added to the Mac OSX 10.6 and iOS 4 SDKs to process a set of instructions, like a function, without requiring a separate function. Blocks makes code more readable and easier to maintain. Like functions, Blocks can have a return value and can have parameters or simply have a void return type.

General Syntax


The general syntax of a block starts with a caret (^) followed either by a set of curly braces containing a block of code or a set of parameters, enclosed in parentheses, with or without a return value. Here are some examples

^{ printf(âHello World!); }
or
^{ NSLog(@âHello world!â); }



In the above simple examples, you can see that the caret replaces the function name. You can embed this bit of code within an existing Objective-C method like the following snippet demonstrates:
?
-(void) someMethod{
   ^{
    NSLog(@âHello world!â);
    }
}
Naturally this is a bit mundane as it would be simpler just to add the NSLog code directly to the method, but this code helps illustrate the usage pattern of Blocks.

Another form of blocks involves using parameters or arguments as the following examples shows:
?
^(float a, float b, NSString *ops){
    float result = 0.0;
switch(ops)
{
    case â/â:
result = a / b;
    break;
    case â*â:
result = a * b;
    break;
    case â+â:
result = a + b;
    break;
    case â-â:
result = a - b;
    break;
}
     
return result ;
}
This “block” of code takes three arguments, one for two possible numbers and the third to allow for a arithmetic operation. To use this type of block, you could assign it to a float block signature or to a variable which is known as a Block Variable.

The following code demonstrates the former:
?
float(^ floaterDiv)(float, float, string) = ^(float a, float b, NSString *ops){
    float result = 0.0;
switch(ops)
{
    case â/â:
result = a / b;
    break;
    case â*â:
result = a * b;
    break;
    case â+â:
result = a + b;
    break;
    case â-â:
result = a - b;
    break;
}
     
return result ;
}

Declaring Block Variables


Blocks like functions can either have no return value, hence have a void return type or either a primitive data type or an Objective-C data type. The following code snippet demonstrates how to declare and define the block variable code.
?
float (^ floatOp)(float, float, int);
    
    
   floatOp  =  ^(float a, float b, int ops){
       float result=0.0f;
        
       switch (ops) {
           case 1:
               result = a/b;
               break;
                
           default:
               break;
       }
       return result;
   };
    
    
   float mathOp = floatOp(4.0f, 5.0f, 1);
    
   NSLog(@"%f", mathOp);
You would need to first declare the variable and any arguments types, if any as the following code snippets shows:

float (^ floatOp)(float, float, int);

This block variable declaration has a return value of float, next the variable name, floatOp, is defined by preceding the name with a caret, ^, and enclosing the name in parentheses. Then the variable’s arguments types, also enclosed in parentheses, are defined.

Once the Block Variable’s signature is declared, you can next add the logic to its body which encapsulated in curly braces as the following code demonstrates:

floatOp = ^(float a, float b, int ops){
float result=0.0f;

switch (ops) {
case 1:
result = a/b;
break;

default:
break;
}
return result;
};

Here the floatOp variable is assigned the signature of ^(float a, float b, int ops) which also include actual arguments. The body is then enclosed in curly braces and resembles the body of any standard C function or Objective-C method. Notice also that the block code is terminated by a semi-colon like any other expression.

Using Typedef


If you prefer, you can use the typedef keywork to declare the block variable in the header or outside the implementation body of class as the following code shows:
?
#import
typedef void(^ replaceString)(NSString*, NSString*, NSString* );
@interface klViewController : UIViewController
@end
A block variable, replaceString, with a void return type is declared with a signature of three NSString variables. Of course we could have placed the code in the implementation file as follows:
?
#import "klViewController.h"
typedef void(^ replaceString)(NSString*, NSString*, NSString* );
@interface klViewController ()
@end
@implementation klViewController
â¦
The result is the same unless of course you want the block variable to have a public scope; to make visible outside the class, then you would declare the block variable in the header.

Scope of a Block


All Blocks are created on the stack like other primitive types and will be destroyed when the enclosing function or method goes out of focus or from the stack frame. To persist the Block after the enclosing method is released, you can use the copy function to move the Bock from the stack to the heap as the following example demonstrates:

SomeObject objInstance = [floatOp copy];

In Summary


Blocks can be confusing at first but offer an elegant solution in event driven apps. They offer performance gains by providing a way to process a block of code on the stack within an existing method or function.