Add Event in iCal or Calendar

iPhone SDK Tips & Tricks

Add EventKit.framework

#import <EventKit/EventKit.h>

EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *newEvent = [EKEvent eventWithEventStore:eventStore];


newEvent.title = @"Event Title";
newEvent.startDate = [NSDate date];
newEvent.endDate = certDatePicker.date;
//newEvent.location = event.location;
newEvent.notes = @"Note for the event";
//EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:-86400]; // 1 Day
EKAlarm *alarm1 = [EKAlarm alarmWithRelativeOffset:-86400*30]; // 30 Day
EKAlarm *alarm2 = [EKAlarm alarmWithRelativeOffset:-86400*60]; // 60 Day

[newEvent addAlarm:alarm1];
[newEvent addAlarm:alarm2];

[newEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:newEvent span:EKSpanThisEvent error:&err];

Number Format

iPhone SDK Tips & Tricks

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
//[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setCurrencySymbol:@"Rp. "];
[numberFormatter setMaximumFractionDigits:0];
[numberFormatter setCurrencyGroupingSeparator:@"."];
//[numberFormatter setCurrencyCode:@"IDR"];

NSString *itemName = [numberFormatter stringFromNumber:[NSNumber numberWithInt:total]];

Creating Multithread

iPhone SDK Tips & Tricks

- (void) startNewThread {
// this kicks off a new thread like bruce-lee
[self performSelectorOnMainThread:@selector(loading) withObject:nil waitUntilDone:NO];
[NSThread detachNewThreadSelector:@selector(doNewThreadStuff) toTarget:self withObject:nil];
}

- (void)loading
{
if(looping)
{
//do loading animation
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(loading) userInfo:nil repeats:NO];
}
}

- (void) doNewThreadStuff {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// this is where you do your stuffs that takes forever and 2 days and 3 minutes
// then when done, you call the following to get back on track
[self performSelectorOnMainThread:@selector(newThreadDone) withObject:nil waitUntilDone:NO];
[pool release];
}

- (void) newThreadDone {
// yay we finished, now continue onwards and upwards. maybe sideways a bit too.
}

Remove or Release NSTimer

iPhone SDK Tips & Tricks
-(void)startTimer {
 [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];  //Don't forget the colon after timerFire if it has an argument!! }  -(void)timerFire:(NSTimer*)theTimer {  //Do timer stuff here   if (shouldKillTimer) [theTimer invalidate]; }

OR

[self performSelector:@selector(timerFunction) withObject:nil afterDelay:timeInSeconds];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timerfunction) object:nil];

Draw Route or Direction

iPhone SDK Tips & Tricks
Download http://blog.kadirpekel.com/wp-content/uploads/MapWithRoutes.zip

Include MapView, Place, PlaceMark, and RegexKitLite file (.h and .m) to your code

Target Info, find Other Linker Flags, add "-licucore"

code:

MapView* mapView = [[[MapView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];

[self.view addSubview:mapView];

Place* home = [[[Place alloc] init] autorelease];
home.name = @"Home";
home.description = @"Sweet home";
home.latitude = 41.029598;
home.longitude = 28.972985;

Place* office = [[[Place alloc] init] autorelease];
office.name = @"Office";
office.description = @"Bad office";
office.latitude = 41.033586;
office.longitude = 28.984546;

[mapView showRouteFrom:home to:office];


source:
http://blog.kadirpekel.com/2010/05/30/drawing_routes_onto_mkmapview_using_unofficial_google_maps_directions_api/

Get iPhone UDID

iPhone SDK Tips & Tricks

NSLog(@"iPhone udid: %@", [UIDevice currentDevice].uniqueIdentifier);

UIButton title and image alignment

iPhone SDK Tips & Tricks

[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];

Make a Phone Call

iPhone SDK Tips & Tricks


[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:1-877-829-7902"]];

Multiline UIButton title

iPhone SDK Tips & Tricks

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(200, 30, 100, 90); // size and position of button
[myButton setBackgroundImage:[UIImage imageNamed:@"blue_bars.png"] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[myButton setTitle:@"Call Us\nNow" forState:UIControlStateNormal];
myButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
myButton.titleLabel.textAlignment = UITextAlignmentCenter;
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
myButton.backgroundColor = [UIColor clearColor];

Color with RGB

iPhone SDK Tips & Tricks

UIColor *myColor = [UIColor colorWithRed:32/255.0 green:93/255.0 blue:137/255.0 alpha:1.0];

Set Date format

iPhone SDK Tips & Tricks

NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"dd-MMM-yyyy"];
NSString *myString = [dateFormat stringFromDate:[NSDate date]];

Determine device (iphone or simulator)

iPhone SDK Tips & Tricks

#if !(TARGET_IPHONE_SIMULATOR)
NSLog(@"iphone");
#else
NSLog(@"simulator");
#endif

Get sender title and tag

iPhone SDK Tips & Tricks


- (void)buttonClick:(id )sender{
NSLog(@"tag: %d", [sender tag]);
NSLog(@"title: %@", [sender currentTitle]);
}