Wednesday, July 18, 2012

NSNumberFormatter remove the decimal point and trailing zeros


unsigned long int intToBeDisplayed = 1234567890;
NSNumber *longNumber = [NSNumber numberWithUnsignedLong:intToBeDisplayed];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setUsesGroupingSeparator:YES];
[formatter setGroupingSeparator:@","];
[formatter setGroupingSize:3];
[formatter setMaximumFractionDigits:0];
NSString *formattedNumber = [formatter stringFromNumber:longNumber];
NSLog(@"The formatted number is %@", formattedNumber);
Result : The formatted number is 1,234,567,890

Wednesday, June 20, 2012

How to Take ScreenShot and save in Photo Album

-(void)saveImageInPhotoAlbum{
    UIGraphicsBeginImageContext(
                              CGSizeMake(self.view.frame.size.width,
                                       self.view.frame.size.height));
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData *imageData = [NSData dataWithData:
                                UIImagePNGRepresentation(screenShot)];
    UIImage *image=[UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

[NSTimer scheduledTimerWithTimeInterval:0.1 target:self 
          selector:@selector(saveImageInPhotoAlbum) 
    userInfo:nil repeats:NO];


// include QuartzCore Framework 
// #import <QuartzCore/QuartzCore.h>

Tuesday, June 19, 2012

Get Seconds from TimeFormat

code :

-(NSString *)getSeconds:(NSString *)timeString {
    NSArray *time    = [timeString componentsSeparatedByString: @":"];
    NSString *hours = [time objectAtIndex: 0];
    NSString *mins  = [time objectAtIndex: 1];
    NSString *secs  = [time objectAtIndex: 2];
    int total              = (([hours intValue] * 3600)+ ([mins intValue] * 60) + [secs intValue]);
    NSString* totalSeconds = [NSString stringWithFormat:@"%d", total];
    return totalSeconds;
}

usage: 


NSLog(@"Seconds : %@", [self getSeconds: @"01:30:48"]);

Friday, June 08, 2012

NSUserDefaults

Save :

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


// saving an NSString
[prefs setObject:@"TextToSave" forKey:@"stringKey"];


// saving an NSInteger
[prefs setInteger:100 forKey:@"integerKey"];


// saving a Float
[prefs setFloat:1.2345678 forKey:@"floatKey"];



Retrieve :

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];


// getting an NSString
NSString *myString = [prefs stringForKey:@"
stringKey"];


// getting an NSInteger
NSInteger myInt = [prefs integerForKey:@"
integerKey"];

// getting an Float
float myFloat = [prefs floatForKey:@"
floatKey"];

Reading data from .plist file

    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSString *filePath = [bundlePath stringByAppendingPathComponent:@"Info.plist"];
    NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:filePath] retain];
   
    versionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100,100,60,25)];
    versionLabel.backgroundColor = [UIColor clearColor];
    versionLabel.textColor = [UIColor whiteColor];
    versionLabel.font = [UIFont systemFontOfSize:10];
    NSString *versionString = [NSString stringWithFormat:@"v%@", [plistData objectForKey:@"CFBundleVersion"]];
    versionLabel.text = versionString;
    [self.view addSubview:versionLabel];

or simply :

NSString *s = [NSString stringWithFormat:@"App Version %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
    versionLabel.text = s;

Wednesday, June 06, 2012

Prevent The iPhone From Sleeping

The code below will prevent the iPhone from dimming its screen and ultimately going to sleep.

[UIApplication sharedApplication].idleTimerDisabled = YES;

Monday, June 04, 2012

String matching and comparison

NSString* str1 = @"hello";
NSString* str2 = @"hello world";
NSRange matching = [str2 rangeOfString : str1];

// Searching for a string within another string
if(matching.location == NSNotFound){
    // str1 not found in str2
} else {
    // str1 found in str2
}

// Checking for equality
if([str1 isEqualToString:@"hello"]){
    // str1 is equal to "hello"
} else {
    // str1 is not equal to "hello"
}

Create a list in SwiftUI with sticky section headers

 Sample Code import SwiftUI struct ContentView : View {     @State var isCustomViewControllerDisplayed = false     @State private va...