Friday, February 26, 2016

IOS LinkedIn Integration

 Add the linkedin sdk(this). Then on login button click, use the following code.
Dont forget to add the linkedin app id on info.plist file.

  [LISDKSessionManager createSessionWithAuth:[NSArray arrayWithObjects:LISDK_BASIC_PROFILE_PERMISSION, LISDK_EMAILADDRESS_PERMISSION, nil]
                                         state:@"some state"
                        showGoToAppStoreDialog:YES
                                  successBlock:^(NSString *returnState) {
                                     
                                      NSLog(@"%s","success called!");
                                      LISDKSession *session = [[LISDKSessionManager sharedInstance] session];
                                      NSLog(@"value=%@ isvalid=%@",[session value],[session isValid] ? @"YES" : @"NO");
                                      NSMutableString *text = [[NSMutableString alloc] initWithString:[session.accessToken description]];
                                      [text appendString:[NSString stringWithFormat:@",state=\"%@\"",returnState]];
                                      NSLog(@"Response label text %@",text);
                                     // NSString *session1 = text;
                                      self.lastError = nil;
                                      // retain cycle here?
                                   
                                      [[LISDKAPIHelper sharedInstance] apiRequest:@"https://www.linkedin.com/v1/people/~:(id,first-name,last-name,maiden-name,email-address,formatted-name,picture-url)"
                                                                           method:@"GET"
                                                                             body:[@"" dataUsingEncoding:NSUTF8StringEncoding]
                                                                          success:^(LISDKAPIResponse *response) {
                                   NSLog(@"success called %@", response.data);
                                                                             
                                     
                                    NSError *jsonError;
                                    NSData *objectData = [response.data dataUsingEncoding:NSUTF8StringEncoding];
                                    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
                                                                                                                                   options:NSJSONReadingMutableContainers
                                                                                                                                     error:&jsonError];

                                                                             
                                                                             
                                                                          }
                                                                            error:^(LISDKAPIError *apiError) {
                                                                                NSLog(@"error called %@", apiError.description);
                                                                               
                                                                               
                                                                            }];
                                     
                                  }
                                    errorBlock:^(NSError *error) {
                                        NSLog(@"%s %@","error called! ", [error description]);
                                        self.lastError = error;
                                      
                                    }
     ];
    NSLog(@"%s","sync pressed3");

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);
    }