Showing posts with label Tips and Tricks. Show all posts
Showing posts with label Tips and Tricks. Show all posts

Aug 5, 2014

CODING CONVENTIONS FOR OBJCTIVE-C

THANK YOU FOR CLOVER STUDIO




 **********************************************
 *                     CODING CONVENTIONS                     *
 **********************************************

 A. CODING RULES

    1. Each class or category has it's own file
        
        MyClass.h
        MyClass.m
        MyClass+Category.h
        MyClass+Category.m
        
    2. Each class or category has it's own group in project explorer
    
        ∆ Classes Folder
            ∆ MyClass
                MyClass.h
                MyClass.m
              ∆ MyClass+Category
                    MyClass+Category.h
                    MyClass+Category.m
            ∆ TestClass
                TestClass.h
                TestClass.m
                
    3. Never use duplicated code, instead copy/pasting create new method

Jul 17, 2014

[iOS] How To Read a File From Your Application Bundle

First you need to add your file to the Resources folder of your Xcode project. Then you can access the file like this (assuming the file is called MyFile.txt):

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
    // do something useful
}  

Here’s a complete example reading a help text file into a UIWebView.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData) {
    [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];
}  

If you want to read the file into a string, which you can then display in a UITextView, for example, then do this:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
if (filePath) {
    NSString *myText = [NSString stringWithContentsOfFile:filePath];
    if (myText) {
        textView.text= myText;
    }
} 

[iOS] Getting current device language in iOS?

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0]; 
Or

NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
NSArray* arrayLanguages = [userDefaults objectForKey:@"AppleLanguages"];
NSString* currentLanguage = [arrayLanguages objectAtIndex:0]; 

Or

NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

Jul 10, 2014

[iOS] how to clear notifications?

[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

Jul 1, 2014

[iOS 8] iOS registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

Its very simple try this (Xcode6 Beta only):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //-- Set Notification
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
        else
        {
            [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
        }
     //--- your custom code
     return YES;
}

Jun 21, 2014

[iOS] DEBUG mode in code

Check your projects build settings for debug to ensure that 'DEBUG' is being set - do this by selecting the project and clicking on the build settings tab. Search for 'DEBUG' and look to see if indeed DEBUG is being set.


then conditionally code for DEBUG in your source files

#ifdef DEBUG
// Something to log your sensitive data here
#else
//
#endif

Jun 7, 2014

[iOS] validate Url from NSString

- (BOOL) validateUrl: (NSString *) url

{  

     NSString *theURL =@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";

     NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", theURL];
    return [urlTest evaluateWithObject:url];
 }

Jun 5, 2014

[iOS] Get the screen width and height

If you need the screen size, you should look at the object that represents the screen itself, like this:
CGRect screenRect = [[UIScreen mainScreen] bounds];CGFloat screenWidth = screenRect.size.width;CGFloat screenHeight = screenRect.size.height;
Or
+ (CGFloat) window_height   {
    return [UIScreen mainScreen].applicationFrame.size.height;
}
+ (CGFloat) window_width   {
    return [UIScreen mainScreen].applicationFrame.size.width;
} 

Jun 2, 2014

[iOS] image With Color


- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0, 0, 60, 60);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();  
    return image;
}

[iOS] Validation email from string


+ (BOOL) isEmailFormat:(NSString *)email
{
    NSString *emailRegEx =
    @"(?:[A-Za-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%\\&'*+/=?\\^_`{|}"
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[A-Za-z0-9](?:[a-"
    @"z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?|\\[(?:(?:25[0-5"
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
    @"9][0-9]?|[A-Za-z0-9-]*[A-Za-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";    NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
    BOOL myStringMatchesRegEx = [regExPredicate evaluateWithObject:email];
    NSLog(@"check email result %i",myStringMatchesRegEx);
    return myStringMatchesRegEx;
}

May 27, 2014

[iOS] random Color


+ (UIColor*) randomColor
{
    float red   = arc4random() % 256;
    float green = arc4random() % 256;
    float blue  = arc4random() % 256;
  
    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}

May 26, 2014

[iOS] is Numeric ?

The simplest way to get to know one string is a number.

+ (BOOL) isNumeric:(NSString *)str
{
    NSScanner *sc = [NSScanner scannerWithString: str];
    if ( [sc scanFloat:NULL] )
    {
        return [sc isAtEnd];
    }
    return NO;

}

[iOS] detectIPhone or iPad

+ (BOOL) detectIPhone
{
    if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        return YES;
    }else{
        return NO;
    }

}


Use function userInterfaceIdiom of class UIDevice to detect iPhone or iPad.

May 25, 2014

[iOS] detected iOS 5, 6 & 7

Find version iOS, use function [UIDevice currentDevice] systemVersion] 
____________________________________________________________

+ (BOOL) isIOS7
{
    NSString *version = [[UIDevice currentDevice] systemVersion];
    if ([version floatValue] >= 7.0) {
        return YES;
    }else {
        return NO;
    }
}

____________________________________________________________

+ (BOOL) isIOS6
{
    NSString *version = [[UIDevice currentDevice] systemVersion];
    if ([version floatValue] >= 6.0 && [version floatValue] < 7.0) {
        return YES;
    }else {
        return NO;
    }
}

____________________________________________________________


+ (BOOL) isIOS5
{
    NSString *version = [[UIDevice currentDevice] systemVersion];
    if ([version floatValue] >= 5.0 && [version floatValue] < 6.0) {
        return YES;
    }else {
        return NO;
    }
}