Detect if an iPhone OS device can make phone calls

- (BOOL)deviceCanMakePhoneCalls

{

BOOL canMakePhoneCalls;

if ([UIApplication instancesRespondToSelector:@selector(canOpenURL:)]) {

// OS 3.0+, so use canOpenURL

UIApplication *app = [UIApplication sharedApplication];

canMakePhoneCalls = ([app canOpenURL:[NSURL URLWithString:@"tel:+44-1234-567890"]]);

} else {

// OS 2.x, so check for iPhone

UIDevice *device = [UIDevice currentDevice];

canMakePhoneCalls = ([device.model isEqualToString:@"iPhone"]);

}

return canMakePhoneCalls;

}

Convert Data/Image to Base64


-(NSString *)Base64Encode:(NSData *)data{
//Point to start of the data and set buffer sizes
int inLength = [data length];
int outLength = ((((inLength * 4)/3)/4)*4) + (((inLength * 4)/3)%4 ? 4 : 0);
const char *inputBuffer = [data bytes];
char *outputBuffer = malloc(outLength);
outputBuffer[outLength] = 0;

//64 digit code
static char Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

//start the count
int cycle = 0;
int inpos = 0;
int outpos = 0;
char temp;

//Pad the last to bytes, the outbuffer must always be a multiple of 4
outputBuffer[outLength-1] = '=';
outputBuffer[outLength-2] = '=';

/* http://en.wikipedia.org/wiki/Base64
Text content M a n
ASCII 77 97 110
8 Bit pattern 01001101 01100001 01101110

6 Bit pattern 010011 010110 000101 101110
Index 19 22 5 46
Base64-encoded T W F u
*/


while (inpos < inLength){
switch (cycle) {
case 0:
outputBuffer[outpos++] = Encode[(inputBuffer[inpos]&0xFC)>>2];
cycle = 1;
break;
case 1:
temp = (inputBuffer[inpos++]&0x03)<<4;
outputBuffer[outpos] = Encode[temp];
cycle = 2;
break;
case 2:
outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xF0)>> 4];
temp = (inputBuffer[inpos++]&0x0F)<<2;
outputBuffer[outpos] = Encode[temp];
cycle = 3;
break;
case 3:
outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&0xC0)>>6];
cycle = 4;
break;
case 4:
outputBuffer[outpos++] = Encode[inputBuffer[inpos++]&0x3f];
cycle = 0;
break;
default:
cycle = 0;
break;
}
}
NSString *pictemp = [NSString stringWithUTF8String:outputBuffer];
free(outputBuffer);
return pictemp;
}

Convert HTML to String

-(NSString *) stringByStrippingHTML:(NSString *)s
{
NSRange r;
//NSString *s = [[self copy] autorelease];
while ((r = [s rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
s = [s stringByReplacingCharactersInRange:r withString:@""];

s = [s stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
return s;
}

Reduced PDF file size in Mac OS

Open ColorSync Utility in the Utilities folder off of the Applications folder. ColorSync Utility can create new Quartz filters. Open up ColorSync Utility and switch to the “Filters” pane. This will list all of the filters that you currently have available.
  1. In the lower left, click on the “+” button. This creates a new filter.
  2. Give the filter a name, and press return.
  3. To the right of the filter’s name, choose the down arrow. A menu will pop up.
  4. From the pop-up menu, choose “Add Image Effects Component”, and from that menu choose “Image Compression”.
  5. Adjust the image compression Mode to JPEG.
  6. Adjust the image compression quality however you prefer.
  7. The next time you use Preview to “Save As...”, you'll have a new option under Quartz filters.

Get First Letter/Character in String

NSString *firstLetter = [someGuysName substringToIndex:1];

Get All Local Notification

UIApplication *app = [UIApplication sharedApplication];
NSArray *alarmArray = [app scheduledLocalNotifications];

for (int i=0; i<[alarmArray count]; i++)
{
UILocalNotification* oneAlarm = [alarmArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneAlarm.userInfo;
NSDate *dateCurrent = [userInfoCurrent valueForKey:@"date"];
NSNumber *typeCurrent = [userInfoCurrent valueForKey:@"type"];
NSLog(@"Found scheduled alarm with date:%@, type:%@", dateCurrent,typeCurrent );
}

Cancel Local Notification

Cancel all local notifications with this code:
[[UIApplication sharedApplication] cancelAllLocalNotifications];

Cancel one local notification with this line of code:
[[UIApplication sharedApplication] cancelLocalNotification:theNotification];

Repeat Local Notification

NSInteger index = [scheduleControl selectedSegmentIndex];
switch (index) {
case 1:
notif.repeatInterval = NSMinuteCalendarUnit;
break;
case 2:
notif.repeatInterval = NSHourCalendarUnit;
break;
case 3:
notif.repeatInterval = NSDayCalendarUnit;
break;
case 4:
notif.repeatInterval = NSWeekCalendarUnit;
break;
default:
notif.repeatInterval = 0;
break;
}

  • NSEraCalendarUnit
  • NSYearCalendarUnit
  • NSMonthCalendarUnit
  • NSDayCalendarUnit
  • NSHourCalendarUnit
  • NSMinuteCalendarUnit
  • NSSecondCalendarUnit
  • NSWeekCalendarUnit
  • NSWeekdayCalendarUnit
  • NSWeekdayOrdinalCalendarUnit
  • NSQuarterCalendarUnit

Handling Local Notifications After They Fire

In your app delegate .m file and add the following code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Application was not running when notification was delivered.
Class cls = NSClassFromString(@"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[viewController showReminder:reminderText];
}
}
application.applicationIconBadgeNumber = 0;
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateInactive)
{
// Application was in the background when notification was delivered.
}
else
{
// Application was in the foreground when notification was delivered.
}
}

Schedule Local Notification

- (void) scheduleNotificationOn:(NSDate*) fireDate
text:(NSString*) alertText
action:(NSString*) alertAction
sound:(NSString*) soundfileName
launchImage:(NSString*) launchImage
andInfo:(NSDictionary*) userInfo
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *raceDate = [df dateFromString: [df stringFromDate: fireDate]];

//localNotification.fireDate = fireDate;
localNotification.fireDate = raceDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;
if(soundfileName == nil)
{
localNotification.soundName = UILocalNotificationDefaultSoundName;
}
else
{
localNotification.soundName = soundfileName;
}
localNotification.alertLaunchImage = launchImage;
self.badgeCount ++;
localNotification.applicationIconBadgeNumber = self.badgeCount;
localNotification.userInfo = userInfo;
// Schedule it with the app
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}