// Name : CommonFunctions.h
#import <Foundation/Foundation.h>
/*
* System Versioning Preprocessor Macros
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
@interface CommonFunctions : NSObject {
}
+ (NSString *)documentsDirectory;
+ (void)openEmail:(NSString *)address;
+ (void)openPhone:(NSString *)number;
+ (void)openSms:(NSString *)number;
+ (void)openBrowser:(NSString *)url;
+ (void)openMap:(NSString *)address;
+ (void) hideTabBar:(UITabBarController *) tabbarcontroller;
+ (void) showTabBar:(UITabBarController *) tabbarcontroller;
+ (void) checkAndCreateDatabase;
+(void) setNavigationTitle:(NSString *) title ForNavigationItem:(UINavigationItem *) navigationItem;
+ (void)scrollViewToCenterOfScreen:(UIView *)theView onScrollView:(UIScrollView *) scrollview;
+(void) becomeNextFirstResponder:(UIView*)vc :(UITextField*)textField;
+(UIBarButtonItem*) getBackBtn;
+(void)BackBtnPressed;
+(UIImage*)getCachedImage:(NSString*)imageUrl;
+(void)showServerNotFoundError;
// user check session
+(BOOL)isUserLogin;
+(void) AlertTitle:(NSString*)title withMsg:(NSString*)msg;
+ (void)removeLoginFromStack;
@end
-------------------------------------------------
// Name : CommonFunctions.m
#import "CommonFunctions.h"
#import "ImgCache.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
@implementation CommonFunctions
//Default document paths
+ (NSString *)documentsDirectory {
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
return [paths objectAtIndex:0];
}
// Open application methods
+ (void)openEmail:(NSString *)address {
NSString *url = [NSString stringWithFormat:@"mailto://%@", address];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openPhone:(NSString *)number {
NSString *url = [NSString stringWithFormat:@"tel://%@", number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openSms:(NSString *)number {
NSString *url = [NSString stringWithFormat:@"sms://%@", number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openBrowser:(NSString *)url {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openMap:(NSString *)address {
NSString *addressText = [address stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *url = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
// Custom tab bar
+ (void) hideTabBar:(UITabBarController *) tabbarcontroller {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
[UIView commitAnimations];
}
// Tab bar custmize
+ (void) showTabBar:(UITabBarController *) tabbarcontroller {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
NSLog(@"%@", view);
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
}
}
[UIView commitAnimations];
}
// Database connect
+(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:DatabasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DatabaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:DatabaseName error:nil];
}
//Navigation custom title and font color need to write this method replacment of (self.title = @"";)
+(void) setNavigationTitle:(NSString *) title ForNavigationItem:(UINavigationItem *) navigationItem
{
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
[titleLbl setFont:[UIFont fontWithName:@"Helvetica" size:24.0f]];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTextAlignment:UITextAlignmentCenter];
[titleLbl setTextColor:[UIColor blackColor]];
//[titleLbl setShadowColor:UIColorFromRedGreenBlue(186,186,186)];
[titleLbl setShadowOffset:CGSizeMake(1.0f, 1.0f)];
[titleLbl setText:title];
[navigationItem setTitleView:titleLbl];
/** May also use this code for Whole App.
* Inharit NavigationBar (CustomNavBar.h, .m)
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont boldSystemFontOfSize:20.0f], UITextAttributeFont,
nil]];
*/
}
// set UITextField to be center of UISrollView pass the textFiled and scrollView obj as arguments
+ (void)scrollViewToCenterOfScreen:(UIView *)theView onScrollView:(UIScrollView *) scrollview {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
+(void) becomeNextFirstResponder:(UIView*)vc :(UITextField*)textField{
NSArray *arr=[[NSArray alloc] init];
NSMutableArray *arr1=[[NSMutableArray alloc] init];
BOOL isFound=NO;
for(UIView *tf in [vc subviews])
{
if([tf isKindOfClass:[UITextField class]] && ((UITextField*)tf).enabled && ((UITextField*)tf).hidden==NO)
{
[arr1 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:((UITextField*)tf).frame.origin.y ], @"y", ((UITextField*)tf), @"obj", nil]];
}
}
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"y"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
arr = [arr1 sortedArrayUsingDescriptors:sortDescriptors];
for(NSMutableDictionary *dic in arr){
UITextField *tf=[dic valueForKey:@"obj"];
if (isFound) {
[tf becomeFirstResponder];
break;
}
if (textField == tf) {
isFound=YES;
[textField resignFirstResponder];
}
}
}
// Make Custom back button for Navigation Bar
+(UIBarButtonItem*) getBackBtn{
UIButton * backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setFrame:CGRectMake(0.0f,0.0f,52.0f,32.0f)];
[backBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"back_button_17"]] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(BackBtnPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
return backBarButton;
}
+(void)BackBtnPressed{
[[dicGlobal objectForKey:@"navCont"] popViewControllerAnimated:YES];
}
// Image cache concept
+(UIImage*)getCachedImage:(NSString*)imageUrl
{
if ([Validate isNull:imageUrl]) {
return [UIImage imageNamed:@"no_image.png"];
}
ImgCache *ic = [[ImgCache alloc] init];
UIImage *img = [ic getCachedImage:imageUrl];
return img;
}
+(void)showServerNotFoundError{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Server not found" message:[NSString stringWithFormat:@"System can't connect to internet. Please check connectivity.\n Thank You!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
// Check for UIUserDefualt set
+(BOOL)isUserLogin{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults objectForKey:@"username"] && ![Validate isNull:[userDefaults objectForKey:@"username"]])
return YES;
else
return NO;
}
+(void) AlertTitle:(NSString*)title withMsg:(NSString*)msg{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
// Remove from UINavigation bar stack
// UINavigationBar Modification
+ (void)removeLoginFromStack{
AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
UINavigationController *navController = (UINavigationController*) [[appDelegate window] rootViewController];
NSMutableArray *mutableStack = [[NSMutableArray alloc] initWithArray:[navController viewControllers]];
for (id loginView in mutableStack) {
if ([loginView isKindOfClass:[LoginViewController class]]) {
[mutableStack removeObjectAtIndex:1];// login page index will be 1
NSArray *stack = [NSArray arrayWithArray:mutableStack];
[navController setViewControllers:stack];
break;
}
}
}
@end
======================================
=====Required File // ImgCache.h
// Name : ImgCache.h
#import <Foundation/Foundation.h>
@interface ImgCache : NSObject {
}
// Methods
- (void) cacheImage: (NSString *) ImageURLString;
- (UIImage *) getCachedImage: (NSString *) ImageURLString;
- (void) removeFromCache:(NSString *) ImageURLString;
@end
--------------------
// Name : ImgCache.m
#import "ImgCache.h"
#define TMP NSTemporaryDirectory()
@implementation ImgCache
- (void) cacheImage: (NSString *) ImageURLString
{
NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
[tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
// Generate a unique path to a resource representing the image you want
NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7]; // [[something unique, perhaps the image name]];
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
//NSLog(@"%@",uniquePath);
// Check for file existence
if(![[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
// The file doesn't exist, we should get a copy of it
// Fetch image
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
[data writeToFile:uniquePath atomically:YES];
/*NSFileManager *NSFm = [NSFileManager defaultManager];
NSDictionary *attr;
attr = [NSFm fileAttributesAtPath:uniquePath traverseLink: NO];
NSDate *fileCreationDate = [attr objectForKey:NSFileModificationDate];
NSString *fileCreationDateString = [fileCreationDate description];
NSLog(@"%@",fileCreationDateString);*/
//UIImage *image = [[UIImage alloc] initWithData: data];
// Do we want to round the corners?
// image = [self roundCorners: image]; // dont need in this app...
// Is it PNG or JPG/JPEG?
// Running the image representation function writes the data from the image to a file
/*
if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
{
[UIImagePNGRepresentation(image) writeToFile: uniquePath atomically: YES];
}
else if(
[ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound ||
[ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound
)
{
[UIImageJPEGRepresentation(image, 100) writeToFile: uniquePath atomically: YES];
}
*/
}
}
-(void) removeFromCache:(NSString *) ImageURLString{
NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
[tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
// Generate a unique path to a resource representing the image you want
NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7]; // [[something unique, perhaps the image name]];
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
//NSLog(@"%@",uniquePath);
// Check for file existence
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
[[NSFileManager defaultManager] removeItemAtPath:uniquePath error:nil];
}
}
- (UIImage *) getCachedImage: (NSString *) ImageURLString
{
NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
[tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7]; // [[something unique, perhaps the image name]];
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
//NSLog(@"filename = %@, uniquepath = %@",filename,uniquePath);
UIImage *image;
// Check for a cached version
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
// image = [UIImage imageWithContentsOfFile: uniquePath]; // this is the cached image
NSData* imageData = [[NSData alloc] initWithContentsOfFile:uniquePath];
image = [[UIImage alloc] initWithData:imageData];
}
else
{
// get a new one
[self cacheImage: ImageURLString];
NSData* imageData = [[NSData alloc] initWithContentsOfFile:uniquePath];
image = [[UIImage alloc] initWithData:imageData];
//NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:ImageURLString]];
//image = [[UIImage alloc] initWithData:imageData];
//image = [UIImage imageWithContentsOfFile: uniquePath];
}
return image;
}
@end
#import <Foundation/Foundation.h>
/*
* System Versioning Preprocessor Macros
*/
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
@interface CommonFunctions : NSObject {
}
+ (NSString *)documentsDirectory;
+ (void)openEmail:(NSString *)address;
+ (void)openPhone:(NSString *)number;
+ (void)openSms:(NSString *)number;
+ (void)openBrowser:(NSString *)url;
+ (void)openMap:(NSString *)address;
+ (void) hideTabBar:(UITabBarController *) tabbarcontroller;
+ (void) showTabBar:(UITabBarController *) tabbarcontroller;
+ (void) checkAndCreateDatabase;
+(void) setNavigationTitle:(NSString *) title ForNavigationItem:(UINavigationItem *) navigationItem;
+ (void)scrollViewToCenterOfScreen:(UIView *)theView onScrollView:(UIScrollView *) scrollview;
+(void) becomeNextFirstResponder:(UIView*)vc :(UITextField*)textField;
+(UIBarButtonItem*) getBackBtn;
+(void)BackBtnPressed;
+(UIImage*)getCachedImage:(NSString*)imageUrl;
+(void)showServerNotFoundError;
// user check session
+(BOOL)isUserLogin;
+(void) AlertTitle:(NSString*)title withMsg:(NSString*)msg;
+ (void)removeLoginFromStack;
@end
-------------------------------------------------
// Name : CommonFunctions.m
#import "CommonFunctions.h"
#import "ImgCache.h"
#import "AppDelegate.h"
#import "LoginViewController.h"
@implementation CommonFunctions
//Default document paths
+ (NSString *)documentsDirectory {
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
return [paths objectAtIndex:0];
}
// Open application methods
+ (void)openEmail:(NSString *)address {
NSString *url = [NSString stringWithFormat:@"mailto://%@", address];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openPhone:(NSString *)number {
NSString *url = [NSString stringWithFormat:@"tel://%@", number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openSms:(NSString *)number {
NSString *url = [NSString stringWithFormat:@"sms://%@", number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openBrowser:(NSString *)url {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
+ (void)openMap:(NSString *)address {
NSString *addressText = [address stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString *url = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
// Custom tab bar
+ (void) hideTabBar:(UITabBarController *) tabbarcontroller {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
[UIView commitAnimations];
}
// Tab bar custmize
+ (void) showTabBar:(UITabBarController *) tabbarcontroller {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
NSLog(@"%@", view);
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
}
}
[UIView commitAnimations];
}
// Database connect
+(void) checkAndCreateDatabase{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:DatabasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DatabaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:DatabaseName error:nil];
}
//Navigation custom title and font color need to write this method replacment of (self.title = @"";)
+(void) setNavigationTitle:(NSString *) title ForNavigationItem:(UINavigationItem *) navigationItem
{
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
[titleLbl setFont:[UIFont fontWithName:@"Helvetica" size:24.0f]];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTextAlignment:UITextAlignmentCenter];
[titleLbl setTextColor:[UIColor blackColor]];
//[titleLbl setShadowColor:UIColorFromRedGreenBlue(186,186,186)];
[titleLbl setShadowOffset:CGSizeMake(1.0f, 1.0f)];
[titleLbl setText:title];
[navigationItem setTitleView:titleLbl];
/** May also use this code for Whole App.
* Inharit NavigationBar (CustomNavBar.h, .m)
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont boldSystemFontOfSize:20.0f], UITextAttributeFont,
nil]];
*/
}
// set UITextField to be center of UISrollView pass the textFiled and scrollView obj as arguments
+ (void)scrollViewToCenterOfScreen:(UIView *)theView onScrollView:(UIScrollView *) scrollview {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
+(void) becomeNextFirstResponder:(UIView*)vc :(UITextField*)textField{
NSArray *arr=[[NSArray alloc] init];
NSMutableArray *arr1=[[NSMutableArray alloc] init];
BOOL isFound=NO;
for(UIView *tf in [vc subviews])
{
if([tf isKindOfClass:[UITextField class]] && ((UITextField*)tf).enabled && ((UITextField*)tf).hidden==NO)
{
[arr1 addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:((UITextField*)tf).frame.origin.y ], @"y", ((UITextField*)tf), @"obj", nil]];
}
}
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"y"
ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
arr = [arr1 sortedArrayUsingDescriptors:sortDescriptors];
for(NSMutableDictionary *dic in arr){
UITextField *tf=[dic valueForKey:@"obj"];
if (isFound) {
[tf becomeFirstResponder];
break;
}
if (textField == tf) {
isFound=YES;
[textField resignFirstResponder];
}
}
}
// Make Custom back button for Navigation Bar
+(UIBarButtonItem*) getBackBtn{
UIButton * backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setFrame:CGRectMake(0.0f,0.0f,52.0f,32.0f)];
[backBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"back_button_17"]] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(BackBtnPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
return backBarButton;
}
+(void)BackBtnPressed{
[[dicGlobal objectForKey:@"navCont"] popViewControllerAnimated:YES];
}
// Image cache concept
+(UIImage*)getCachedImage:(NSString*)imageUrl
{
if ([Validate isNull:imageUrl]) {
return [UIImage imageNamed:@"no_image.png"];
}
ImgCache *ic = [[ImgCache alloc] init];
UIImage *img = [ic getCachedImage:imageUrl];
return img;
}
+(void)showServerNotFoundError{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Server not found" message:[NSString stringWithFormat:@"System can't connect to internet. Please check connectivity.\n Thank You!"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
// Check for UIUserDefualt set
+(BOOL)isUserLogin{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults objectForKey:@"username"] && ![Validate isNull:[userDefaults objectForKey:@"username"]])
return YES;
else
return NO;
}
+(void) AlertTitle:(NSString*)title withMsg:(NSString*)msg{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
// Remove from UINavigation bar stack
// UINavigationBar Modification
+ (void)removeLoginFromStack{
AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
UINavigationController *navController = (UINavigationController*) [[appDelegate window] rootViewController];
NSMutableArray *mutableStack = [[NSMutableArray alloc] initWithArray:[navController viewControllers]];
for (id loginView in mutableStack) {
if ([loginView isKindOfClass:[LoginViewController class]]) {
[mutableStack removeObjectAtIndex:1];// login page index will be 1
NSArray *stack = [NSArray arrayWithArray:mutableStack];
[navController setViewControllers:stack];
break;
}
}
}
@end
======================================
=====Required File // ImgCache.h
// Name : ImgCache.h
#import <Foundation/Foundation.h>
@interface ImgCache : NSObject {
}
// Methods
- (void) cacheImage: (NSString *) ImageURLString;
- (UIImage *) getCachedImage: (NSString *) ImageURLString;
- (void) removeFromCache:(NSString *) ImageURLString;
@end
--------------------
// Name : ImgCache.m
#import "ImgCache.h"
#define TMP NSTemporaryDirectory()
@implementation ImgCache
- (void) cacheImage: (NSString *) ImageURLString
{
NSURL *ImageURL = [NSURL URLWithString: ImageURLString];
NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
[tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
// Generate a unique path to a resource representing the image you want
NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7]; // [[something unique, perhaps the image name]];
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
//NSLog(@"%@",uniquePath);
// Check for file existence
if(![[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
// The file doesn't exist, we should get a copy of it
// Fetch image
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
[data writeToFile:uniquePath atomically:YES];
/*NSFileManager *NSFm = [NSFileManager defaultManager];
NSDictionary *attr;
attr = [NSFm fileAttributesAtPath:uniquePath traverseLink: NO];
NSDate *fileCreationDate = [attr objectForKey:NSFileModificationDate];
NSString *fileCreationDateString = [fileCreationDate description];
NSLog(@"%@",fileCreationDateString);*/
//UIImage *image = [[UIImage alloc] initWithData: data];
// Do we want to round the corners?
// image = [self roundCorners: image]; // dont need in this app...
// Is it PNG or JPG/JPEG?
// Running the image representation function writes the data from the image to a file
/*
if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound)
{
[UIImagePNGRepresentation(image) writeToFile: uniquePath atomically: YES];
}
else if(
[ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound ||
[ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound
)
{
[UIImageJPEGRepresentation(image, 100) writeToFile: uniquePath atomically: YES];
}
*/
}
}
-(void) removeFromCache:(NSString *) ImageURLString{
NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
[tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
// Generate a unique path to a resource representing the image you want
NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7]; // [[something unique, perhaps the image name]];
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
//NSLog(@"%@",uniquePath);
// Check for file existence
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
[[NSFileManager defaultManager] removeItemAtPath:uniquePath error:nil];
}
}
- (UIImage *) getCachedImage: (NSString *) ImageURLString
{
NSMutableString *tempImgUrlStr = [NSMutableString stringWithString:[ImageURLString substringFromIndex:7]];
[tempImgUrlStr replaceOccurrencesOfString:@"/" withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempImgUrlStr length])];
NSString *filename = [NSString stringWithFormat:@"%@",tempImgUrlStr] ;//[ImageURLString substringFromIndex:7]; // [[something unique, perhaps the image name]];
NSString *uniquePath = [TMP stringByAppendingPathComponent: filename];
//NSLog(@"filename = %@, uniquepath = %@",filename,uniquePath);
UIImage *image;
// Check for a cached version
if([[NSFileManager defaultManager] fileExistsAtPath: uniquePath])
{
// image = [UIImage imageWithContentsOfFile: uniquePath]; // this is the cached image
NSData* imageData = [[NSData alloc] initWithContentsOfFile:uniquePath];
image = [[UIImage alloc] initWithData:imageData];
}
else
{
// get a new one
[self cacheImage: ImageURLString];
NSData* imageData = [[NSData alloc] initWithContentsOfFile:uniquePath];
image = [[UIImage alloc] initWithData:imageData];
//NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:ImageURLString]];
//image = [[UIImage alloc] initWithData:imageData];
//image = [UIImage imageWithContentsOfFile: uniquePath];
}
return image;
}
@end
No comments:
Post a Comment