Thursday 24 March 2011

Pop Over Control on iPad

Real helpful tutorials !


http://mobiforge.com/designing/story/using-popoverview-ipad-app-development

http://www.matthewcasey.co.uk/2010/04/07/tutorial-introduction-to-pop-over-control-on-ipad-part-2/

http://www.jannisnikoy.nl/index.php/2010/04/ipad-tutorial-creating-a-popoverviewcontroller

Tuesday 22 March 2011

Converting iPhone xib to iPad xib?

When I first tried to upgrade from an existing Iphone App to an Ipad App I used: "Universal App"! The Main thing here is, that you have to create all your xib files manually.

I did then tried it with the other option "Using a Single Xcode Project to Build Two Applications" - where all the necessary XIB Files are translated to Ipad...


Using a Single Xcode Project to Build Two Applications

Maintaining a single Xcode project for both iPhone and iPad development simplifies the development process tremendously by allowing you to share code between two separate applications. The Project menu in Xcode includes a new Upgrade Current Target for iPad command that makes it easy to add a target for iPad devices to your existing iPhone project. To use this command, do the following:

Open the Xcode project for your existing iPhone application.
Select the target for your iPhone application.
Select Project > Upgrade Current Target for iPad and follow the prompts to create two device-specific applications.

Launching Other Apps within an iPhone Application

Yes it is possible to lunch other apps within an iPhone application, it can be done via URL technique .

Examples of some of the key applications that you can launch via URL are:

Launch Apple Mail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://info@iphonedevelopertips.com"]];

Dial a Phone Number (iPhone Only)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://8004664411"]];

Launch the SMS Application
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:55555"]];

Launching the AppStore
NSURL *appStoreUrl = [NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/
wa/viewSoftware?id=291586600&mt=8"];
[[UIApplication sharedApplication] openURL:appStoreUrl];

How to set navigation controller color

To set navigation controller color there’s a simple solution. Write below code in your view ‘View did load delegate’

Predefine color

self.navigationController.navigationBar.tintColor = [UIColor redColor];

RGB color

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:255/255 green:60/255 blue:0/255 alpha:100/255];

How to change the name of an iPhone app xcode

Just follow below three steps and you will be able to do it surely.

1. Select the target icon in your xCode
2. “Get Info” on your project’s target (your current old development name)
3. Search for “Product Name” under “Packaging”. Change the value of that what you want the new project name is going to be.

How to add ASIHTTPRequest in Iphone project

I faced some difficulties for how to use this great framework but then I find the official documentation link which is very helpful and solve my all problems.


http://allseeing-i.com/ASIHTTPRequest/Setup-instructions

How to convert Image into NSData

Here’s the simple solution.

NSData* topImageData = UIImageJPEGRepresentation(topImage, 1.0);

How to convert NSData into image

Here’s the simple code

UIImage *img = [[UIImage alloc] initWithData:data];

How to add button in UINavigationController

You can write following code in ViewDidLoad method

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(newDate:)];

and also define the selector to get the user interaction response

This is the Selector of above right bar button

- (void) newDate: (id) sender{
}

Float in objective c

Most people facing problem in how to control the numbers after point in objective c you can use the following trick in order to achieve this

NSString *floatMin = [NSString stringWithFormat:@"%.01f",anyFloatVariable];

In this way you can only get two decimals after point

CSV parser for iPhone

You can download the CSV parser from the following link

http://zeeshanullah.com/blog/downloads/CSV.zip

I’ve used this CSV parser in 3 of my applications and all are on iTunes so do not worry while using this.

Use code:

CSVParser *parser = [CSVParser new];
[parser openFile: filePath];
NSMutableArray *csvContent = [parser parseFile];

Simple is that create the new object of CSVParser then define the path and at last call parseFile method that will return NSMutableArray of csv file.

Text to Pdf for iPhone

I’ve made a function that creates pdf file from text.

void CreatePDFFile (CGRect pageRect, const char *filename) {

// This code block sets up our PDF Context so that we can draw to it
CGContextRef pdfContext;
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
// Create a CFString from the filename we provide to this method when we call it

//********************************************************************************************************
//This is the path where pdf saved you can save it any where like inside iPhone in NSDATA and then mail it.
//********************************************************************************************************
path = CFStringCreateWithCString (NULL, filename,
kCFStringEncodingUTF8);

// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, 0);
CFRelease (path);
// This dictionary contains extra options mostly for ‘signing’ the PDF
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR(“My PDF File”));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR(“My Name”));
// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
// Cleanup our mess
CFRelease(myDictionary);
CFRelease(url);
// Done creating our PDF Context, now it’s time to draw to it

// Starts our first page
CGContextBeginPage (pdfContext, &pageRect);

// Draws a black rectangle around the page inset by 50 on all sides
CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width – 100, pageRect.size.height – 100));

// This code block will create an image that we then draw to the page
const char *picture = “Picture”;
CGImageRef image;
CGDataProviderRef provider;
CFStringRef picturePath;
CFURLRef pictureURL;

picturePath = CFStringCreateWithCString (NULL, picture,
kCFStringEncodingUTF8);
pictureURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), picturePath, CFSTR(“png”), NULL);
CFRelease(picturePath);
provider = CGDataProviderCreateWithURL (pictureURL);
CFRelease (pictureURL);
image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease (provider);
CGContextDrawImage (pdfContext, CGRectMake(200, 200, 207, 385),image);
CGImageRelease (image);
// End image code

// Adding some text on top of the image we just added
CGContextSelectFont (pdfContext, “Helvetica”, 16, kCGEncodingMacRoman);
CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
const char *text = “Yes I can Make PDF From Text”;
CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text));
// End text

// We are done drawing to this page, let’s end it
// We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage
CGContextEndPage (pdfContext);

// We are done with our context now, so we release it
CGContextRelease (pdfContext);
}

Below code to use this function.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *saveDirectory = [paths objectAtIndex:0];
NSString *saveFileName = @”myPDF.pdf”;
NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
const char *filename = [newFilePath UTF8String];
CreatePDFFile(CGRectMake(0, 0, 612, 792),filename);

That’s it……

Extract zip in iPhone

Below is the download link to unzip file within iPhone

http://zeeshanullah.com/blog/downloads/Zip.zip

Below is the code to use this zip library

ZipArchive *za = [[ZipArchive alloc] init];
if ([za UnzipOpenFile: fromFile]) {
BOOL ret = [za UnzipFileTo: toFile overWrite: YES];
if (NO == ret){} [za UnzipCloseFile];
}
[za release];

(fromFile : The zip file path)
(toFile : Where to place extracted zip)

UIColor from RGB iPhone

I’ve write the simple code.

#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 \
alpha:1.0]

Use it like that
self.navigationController.navigationBar.tintColor = UIColorFromRGB(0xa90606);

Note:- write 0x before your RGB code.

Monday 21 March 2011

Asynchronous Loading of Images in a UITableView

http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/

How to use JSON in Objective-C

Learn JSON

http://www.json.org/

Use In Objective C

http://iphone.zcentric.com/2008/08/05/first-json-iphone-application/
http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c

Good Tutorial

http://blog.grio.com/2009/04/dealing-with-json-on-iphone.html

Monday 14 March 2011

How to compare date string with current date

- (NSString *)dateDifferenceStringFromString:(NSString *)dateString withFormat:(NSString *)dateFormat;
{

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDate *now = [NSDate date];
double time = [date timeIntervalSinceDate:now];
time *= -1;
if(time < 1) {
return dateString;
} else if (time < 60) {
return @"less than a minute ago";
} else if (time < 3600) {
int diff = round(time / 60);
if (diff == 1)
return [NSString stringWithFormat:@"1 minute ago", diff];
return [NSString stringWithFormat:@"%d minutes ago", diff];
} else if (time < 86400) {
int diff = round(time / 60 / 60);
if (diff == 1)
return [NSString stringWithFormat:@"1 hour ago", diff];
return [NSString stringWithFormat:@"%d hours ago", diff];
} else if (time < 604800) {
int diff = round(time / 60 / 60 / 24);
if (diff == 1)
return [NSString stringWithFormat:@"yesterday", diff];
if (diff == 7)
return [NSString stringWithFormat:@"last week", diff];
return[NSString stringWithFormat:@"%d days ago", diff];
} else {
int diff = round(time / 60 / 60 / 24 / 7);
if (diff == 1)
return [NSString stringWithFormat:@"last week", diff];
return [NSString stringWithFormat:@"%d weeks ago", diff];
}
}

How to get Current date in iphone

NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSString* currentdate = [formatter stringFromDate:date];
NSLog(@"%@",currentdate);

Saturday 12 March 2011

Clear Webview in Viewwilldissapear

- (void) viewWillDisappear:(BOOL)animated{

[webView loadHTMLString:@"" baseURL:nil];
}

Friday 11 March 2011

how to remove white spaces from a string in objective C


A one line solution:

NSString *whitespaceString = @" String with whitespaces ";

NSString *trimmedString = [whitespaceString stringByReplacingOccurrencesOfString:@" " withString:@""];

Simple UIAlertView Use in IPhone

Define Delegate

UIAlertViewDelegate

then

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message?"
delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];
[alert release];

and

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex (NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 0)
{
NSLog(@"NO");
}
else
{
NSLog(@"YES");
}
}

How to link to Google Map directions from iPhone app by address

NSString* urlString = @"http://maps.google.com/maps?saddr=London+UK&daddr=Birmingham+UK";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlString]];

iphone google maps - direction using latitude and longitude

NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,
destLocation.coordinate.latitude, destLocation.coordinate.longitude];

[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];


How to make a phone call through programming in iPhone