objC

Crash Log Helper

.h

@interface CrashLogHelper : NSObject

+(void) writeNoCrashFile;
+(void) removeNoCrashFile;

.m
+(void) writeNoCrashFile
{
    NSError *error;
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"crashloggername.txt"];
    NSLog(@"file path for write nocrash file%@", filePath);
    NSFileManager *filemgr;
   
    filemgr = [NSFileManager defaultManager];
   
    if ([filemgr fileExistsAtPath: filePath ] == YES)
    {
        NSLog (@"File exists, so it did crash");
        [App.persistenceHelper setString:@"-1" forPersistentKey:PERSISTENCEHELPER_ratingsequence];
    }
    else
    {
        NSLog (@"File not found");
        // write new file
        NSString *stringToWrite = @"no crash";
        [stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    }
}
+(void) removeNoCrashFile
{
    NSFileManager *filemgr;
    NSError *error;
    filemgr = [NSFileManager defaultManager];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"crashlogger.txt"];
  
    if ([filemgr fileExistsAtPath: filePath ] == YES){
        NSLog (@"File exists so remove");
        BOOL success = [filemgr removeItemAtPath:filePath error:&error];
        if (success) {
            NSLog(@"file removed");
        }
        else
        {
            NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
        }
    }
    else{
        NSLog (@"File not found"); // something fishy
    }
   
}

Tags: 

Screenwidth screen height

+ (CGFloat) getScreenWidth
{
    CGFloat swidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat sheight = [UIScreen mainScreen].bounds.size.height;
    return MIN(swidth, sheight);
}

+ (CGFloat) getScreenHeight
{
    CGFloat swidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat sheight = [UIScreen mainScreen].bounds.size.height;
    return MAX(swidth, sheight);
}

Tags: 

Read Write file IO

+(void) writeTextFile
{
    NSError *error;
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"text.txt"];
    NSFileManager *filemgr;
   
    filemgr = [NSFileManager defaultManager];
   
    if ([filemgr fileExistsAtPath: filePath ] == YES)
    {
        NSLog (@"File exists");
    }
    else
    {
        NSLog (@"File not found");
        // write new file
        NSString *stringToWrite = @"text text text";
        [stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
    }
}
+(void) removeTextFile
{
    NSFileManager *filemgr;
    NSError *error;
    filemgr = [NSFileManager defaultManager];
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"text.txt"];
  
    if ([filemgr fileExistsAtPath: filePath ] == YES){
        NSLog (@"File exists so remove");
        BOOL success = [filemgr removeItemAtPath:filePath error:&error];
        if (success) {
            NSLog(@"file removed");
        }
        else
        {
            NSLog(@"Could not delete file -:%@ ",[error localizedDescription]);
        }
    }
    else{
        NSLog (@"File not found"); // something fishy
    }
   
}

Tags: 

Write/Read data to .plist file

First of all add a plist to your project in Xcode. For example “data.plist”.

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@”data.plist”]; //3

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@”data” ofType:@”plist”]; //5

[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}

1) Create a list of paths.
2) Get a path to your documents directory from the list.
3) Create a full file path.
4) Check if file exists.
5) Get a path to your plist created before in bundle directory (by Xcode).
6) Copy this plist to your documents directory.
next read data:
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

//load from savedStock example int value
int value;
value = [[savedStock objectForKey:@”value”] intValue];

[savedStock release];

write data:
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

//here add elements to data file and write data to file
int value = 5;

[data setObject:[NSNumber numberWithInt:value] forKey:@”value”];

[data writeToFile: path atomically:YES];
[data release]

1) You must create a plist file in your Xcode project.
2) To optimize your app, better is to save all the data when application (or for example view) is closing. For instance in applicationWillTerminate. But if you are storing reeaaaally big data, sometimes it couldn’t be saved in this method, becouse the app is closing too long and the system will terminate it immediately.

Tags: 

check if app is running in 64 bit

There are two ways to determine this: at runtime (i.e. in your running code), and at compile time (i.e. before the code is compiled). Let’s take a look at both options.

Runtime Check
// testing for 64bit at runtime

if (sizeof(void*) == 4) {
self.textLabel.text = @"You're running in 32 bit";

} else if (sizeof(void*) == 8) {
self.textLabel.text = @"You're running in 64 bit";
}
Determines the size of a pointer.

Compile Time Check
#if __LP64__
// you're running 64bit
#else
// you're running 32bit
#endif
Asks the compiler which architecture it compiles for.

Tags: 

uiiamge background

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"statistics"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageView *imageView = [[UIImageView alloc] initWithImage: image];

[self.view addSubview: imageView];

[self.view sendSubviewToBack: imageView];

Tags: 

uinavigationcontroller

[[self navigationController] popToViewController:obj animated:YES];

[self.navigationController popToRootViewControllerAnimated:YES];

Tags: 

hide status bar

in plist file
set Status bar is initially hidden = YES
add row: View controller-based status bar appearance = NO

[self.navigationController setNavigationBarHidden:YES];

Tags: 
Subscribe to RSS - objC