Objective-C utility class using C functions in iOS
Introduction
Like C, I would like to use C functions in Objective-C class so i could use C functions anywhere anytime without using class name.
Defining function: Function definition in C programming language is as follows −
return_type function_name(parameter list)
{
body of the function
}
|
In Objective-C, you can create a Objective-C class having NSObject superclass.
I am creating a CommonFunctions.h & CommonFunctions.m as given below:
Header File (CommonFunctions.h)
#import <UIKit/UIKit.h> #pragma mark - NSUserDefaults void saveDataWithKey(id data,NSString *key); id dataWithKey(NSString *key); #pragma mark - Notification void removeNotification(id sender); void postNotification(NSString *name, id object); void addNotification(NSString *name, id sender,SEL selector,id object); #pragma mark - NSFileManager BOOL isFileExist(NSString* path); void removePath(NSString* path); void createDirectory(NSString* path); BOOL isDirectoryExist(NSString* path); #pragma mark - String // function to check empty or null string BOOL nonEmptyString(NSString *string); #pragma mark - Custom void customBackButtonForController(id controller); void startShake (UIView* view); void shakeEnded (NSString * animationID, BOOL finished, void *context); void showAlert (NSString * title,NSString * message); //Function to calculate height for label according to text at runtime CGSize dynamicHeightForLabel(NSString *text,float width, UIFont *font); |
Implementation File (CommonFunctions.m)
#import "CommonFunctions.h"
#pragma mark - NSUserDefaults
void saveDataWithKey(id data,NSString *key){
NSData *data_ = [NSKeyedArchiver archivedDataWithRootObject:data];
[[NSUserDefaults standardUserDefaults] setObject:data_ forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
id dataWithKey(NSString *key){
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if(!data)
return nil;
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
#pragma mark - Notification
void removeNotification(id sender){
[[NSNotificationCenter defaultCenter] removeObserver:sender];
}
void postNotification(NSString *name, id object){
[[NSNotificationCenter defaultCenter] postNotificationName:name object:object];
}
void addNotification(NSString *name, id sender,SEL selector,id object){
[[NSNotificationCenter defaultCenter] addObserver:sender
selector:selector
name:name
object:object];
}
#pragma mark - NSFileManager
BOOL isFileExist(NSString* path){
return [[NSFileManager defaultManager] fileExistsAtPath:path];
}
void removePath(NSString* path){
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
void createDirectory(NSString* path){
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
}
BOOL isDirectoryExist(NSString* path){
BOOL is = NO;
if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&is])
return YES;
return NO;
}
#pragma mark- String
BOOL nonEmptyString(NSString *string){
if (string == (id)[NSNull null] || string.length == 0){
return NO;
}else{
NSMutableString *inputString = [NSMutableString stringWithString:string];
[inputString replaceOccurrencesOfString:@" "
withString:@""
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [string length])];
if(inputString.length == 0)
return NO;
return YES;
}
}
#pragma mark- Custom
void startShake (UIView* view){
CGAffineTransform leftShake = CGAffineTransformMakeTranslation(-5, 0);
CGAffineTransform rightShake = CGAffineTransformMakeTranslation(0, 0);
view.transform = leftShake; // starting point
[UIView beginAnimations:@"shake_button" context:(__bridge void *)(view)];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:3];
[UIView setAnimationDuration:0.06];
shakeEnded(@"shake_button", YES, (__bridge void *)(view));
view.transform = rightShake; // end here & auto-reverse
[UIView commitAnimations];
}
void shakeEnded (NSString * animationID, BOOL finished, void *context){
if (finished) {
UIView* item = (__bridge UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}
void showAlert (NSString * title,NSString * message){
UIAlertView *alerView = [[UIAlertView alloc]initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[alerView show];
}
void customBackButtonForController (UIViewController *controller){
UIButton *customBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
customBackBtn.frame = CGRectMake(0, 0, 22, 22);
BUTTONIMAGE(customBackBtn, "backArrow");
BUTTON_ADD_TARGET(customBackBtn,controller,backBtnAciton:);
UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc] initWithCustomView:customBackBtn];
[controller.navigationItem setLeftBarButtonItem:leftBarBtnItem animated:YES];
}
CGSize dynamicHeightForLabel(NSString *text,float width, UIFont *font){
CGSize constrainedSize = CGSizeMake(width, CGFLOAT_MAX);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font,
NSFontAttributeName,nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text
attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize
options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > width) {
requiredHeight = CGRectMake(0, 0, width, requiredHeight.size.height);
}
return requiredHeight.size;
}
|
How to call CommonFunctions.h methods
Simple like other Objective-C class import “CommonFunctions.h” class in required controller or views and access methods by calling method name directly.
Example:
#import "CommonFunctions.h"
NSString *string = @"string";
if(notEmptyString(string)){
NSLog(@"string is not empty");
}else{
NSLog(@"string is empty or null");
}
|
Download
You can download the CommonFunctions.h & CommonFunctions.m from here.
