Monday, November 17, 2014

IOS checking string contains Uppercase, Lowercase and Digits

NSCharacterSet *lowerCaseChars = [NSCharacterSet characterSetWithCharactersInSt
ring:@"abcdefghijklmnopqrstuvwxyz"];
    
    NSCharacterSet *upperCaseChars = [NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLKMNOPQRSTUVWXYZ"];
    
    NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    
    if ([tx1.text rangeOfCharacterFromSet:lowerCaseChars].location == NSNotFound) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"no small"
                                                        message:tx1.text
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];

    }else if([tx1.text rangeOfCharacterFromSet:upperCaseChars].location == NSNotFound) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"no cap"
                                                        message:tx1.text
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        
    }else if([tx1.text rangeOfCharacterFromSet:numbers].location == NSNotFound) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"no num"
                                                        message:tx1.text
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        
    }else{
      
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"yes......"
                                                            message:tx1.text
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            
       
    }

IOS TableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section{
    return [array_name count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath{
  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             @"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:
                UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    //UIImage *cellImage =  [UIImage imageNamed:[imagesarray objectAtIndex:indexPath.row]];
    //cell.imageView.image = cellImage;
  
    cell.textLabel.text= [array_name objectAtIndex:indexPath.row];

    cell.detailTextLabel.text=[array_address objectAtIndex:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if([UIScreen mainScreen].bounds.size.height == 568.0)
        {
             return 70;
           
        }
        else
        {
            return 60;
        }
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath{
    position=indexPath.row;
    DetailsView *rslt=[[DetailsView alloc]init];
   [self presentViewController:rslt animated:YES completion:nil];
   
}

Saturday, November 15, 2014

Database operations on IOS

Create table-

NSString *docsDir;
    NSArray *dirPaths;
   
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];
databasePath = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"contacts.db"]];
 NSLog(@"aaa: %@",databasePath);
    NSFileManager *filemgr = [NSFileManager defaultManager];

       
    if ([filemgr fileExistsAtPath:databasePath] == NO)
    {
        const char *dbpath = [databasePath UTF8String];
      
        if (sqlite3_open(dbpath, &sql) == SQLITE_OK)
        {
            char *errMsg;
            const char *sql_stmt ="CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
           
            if (sqlite3_exec(sql, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
            {
                NSLog(@"Failed to create table");
            }
            sqlite3_close(sql);
        }
        else
        {
            NSLog(@"Failed to open/create database");
        }
    }


Save data-

    sqlite3_stmt    *statement;
    const char *dbpath = [databasePath UTF8String];
   
    if (sqlite3_open(dbpath, &sql) == SQLITE_OK)
    {
       
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")",tx1.text, tx2.text, tx3.text];
       
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(sql, insert_stmt,
                           -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Contact added");
        
        }
        else
        {
            NSLog(@"Failed to add contact");
        }
        sqlite3_finalize(statement);
        sqlite3_close(sql);
    }



Search values -

  const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt    *statement;
  

    if (sqlite3_open(dbpath, &sql) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat:
                              @"SELECT address, phone FROM contacts WHERE name=\"%@\"",
                              tx1.text];
       
        const char *query_stmt = [querySQL UTF8String];
       
        if (sqlite3_prepare_v2(sql,query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_ROW)
            {
                           
                NSString *addressField = [[NSString alloc]
                                          initWithUTF8String:
                                          (const char *) sqlite3_column_text(statement, 0)];

             
                NSString *phoneField = [[NSString alloc]
                                        initWithUTF8String:(const char *)
                                        sqlite3_column_text(statement, 1)];
                tx2.text = addressField;
                tx3.text = phoneField;
              
            }
            else
            {
                NSLog(@"No data Found");
            
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(sql);
    }

Xcode 6 Creating Xib From StoryBoard

The first step is to remove Main.storyboard, LaunchScreen.xib, ViewController.h, and ViewController.m—select them all, right-click, and choose to either remove them from the project, or delete them completely:
If you run the app at this point, you’ll get an exception: “Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Could not find a storyboard named ‘Main’ in bundle…” This is because the project is trying to use that storyboard file you just removed. This information is in the project’s Info.plist, and the entry is “Main storyboard file base name”:To fix this, simply remove the entry (select it and hit delete or hover and click the remove icon).
Open AppDelegate.m, and edit applicationDidFinishLaunchingWithOptions so that it looks like this:
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

In order to get to the functional equivalent of an Xcode 4 Single View Application, we need to add a View Controller and its corresponding xib file, and hook them up. The first step is to add a subclass of UIViewController and a corresponding xib file. Use ⌘-N or the File->New->File.. menu item to create a new file, and select “Cocoa Touch Class”: The next dialog asks you to name the class; I cleverly called mine “ViewController”. Make it a subclass of “UIViewController”, and check the “Also create XIB File”:
This will create three files in your project: ViewController.h, ViewController.m, and ViewController.xib:

Running the app at this point will give you the same behavior as before, as we’ve not yet told your app to actually use the ViewController. To do this, we need to add a property to the AppDelegate, to hold the ViewController; edit AppDelegate.h so it looks like this:
#import
 
@class ViewController;
 
@interface AppDelegate : UIResponder <UIApplicationDelegate>
 
@property (strong, nonatomic) UIWindow *window;
 
@property (strong, nonatomic) ViewController *viewController;
 
@end
 
and then edit didFinishLaunchingWithOptions  so it looks like this:
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}