JSON Tweeter APi using NSJSONSerialization

ViewController.h

#import <UIKit/UIKit.h>
#import “DetailViewController.h”
@interface ViewController : UIViewController<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    IBOutlet UILabel *resultLabel;
    
    IBOutlet UITableView *table;
    NSMutableArray *myArray;
    NSDictionary *twit;
}
@end

ViewController.m

#import “ViewController.h”
#import “DetailViewController.h”
@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad
{
    [super viewDidLoad];
   
    //scroll.contentSize=CGSizeMake(300, 500);
    NSURLRequest *req=[NSURLRequest requestWithURL:[NSURL URLWithString:@”http://api.twitter.com/1/statuses/user_timeline.json?screen_name=vaibhavlimbani”%5D%5D;
    
    //perform request and and get JSON as a NSData obj
    
    NSData *respone=[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil];
    
    NSError *jsonParsingErr=nil;
    NSArray *publicTimeline=[NSJSONSerialization JSONObjectWithData:respone options:0 error:&jsonParsingErr];
    myArray=[[NSMutableArray alloc]initWithArray:publicTimeline];
    
    
    
    for (int i =0; i<[publicTimeline count]; i++)
    {
        twit=[publicTimeline objectAtIndex:i];
        NSLog(@”Statses: %@”,[twit objectForKey:@”text”]);
        //resultLabel.text=[twit objectForKey:@”text”];
    }
    
}
    
    
    
    
    
    

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

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @”Cell”;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleBlue reuseIdentifier:CellIdentifier] ;
        twit=[myArray objectAtIndex:indexPath.row];

        
        cell.textLabel.text=[twit objectForKey:@”text”];
            }
    return cell;
    
}

– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    DetailViewController *detail=[[DetailViewController alloc]initWithNibName:@”DetailViewController” bundle:nil];
    
    [self presentViewController:detail animated:YES completion:nil];
    
    detail.resultLabel2.text=[[myArray objectAtIndex:indexPath.row]objectForKey:@”text”];
    NSLog(@”%@”,[[myArray objectAtIndex:indexPath.row]objectForKey:@”text”]);
    
}
Image

Database Demo for Sqlite

AppDelegate.h

– (void) copyDatabaseIfNeeded;
– (NSString *) getDBPath;

App Delegate.m

-(void) copydb{
   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *insPath = [NSString stringWithFormat:@”UnitConverterDB.sqlite”];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:insPath];
    NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:insPath];
    NSLog(@”\n src %@ \n dest %@”, srcPath, destPath);
    if (![[NSFileManager defaultManager] fileExistsAtPath:destPath])
    {
        NSError *error;
        NSLog(@”not exist”);
        [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:destPath error:&error];
    }else
    {
        NSLog(@”exist”);
    }
}

– (NSString *) getDBPath {
   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@”UnitConverterDB.sqlite”];
}

.h
import sqlite
sqlite3 *db;
-(NSString *) getDBPath

-(void)viewWillAppear:(BOOL)animated
{
    NSString *selectstmt=[NSString stringWithFormat:@”SELECT DISTINCT Category_Id,Cat_Name,Table_Name FROM Catergory_Master”];
   
    arrayDataFromDb=[self selectDataFromDatabase:@”UnitConverterDB.sqlite” query:selectstmt];
    NSLog(@”Return Data %@”,arrayDataFromDb);
}

.m

-(NSMutableArray *) selectDataFromDatabase:(NSString *)dbname query:(NSString *)query
{
   
    NSMutableArray *mainArray = [[NSMutableArray alloc] init];
    NSMutableDictionary *subDic = [[NSMutableDictionary alloc] init];
   
    //NSMutableArray *subArray = [[NSMutableArray alloc] init];
    NSString *dbPath = [self getDBPath];
    NSLog(@”dbPath :%@”,dbPath);
    sqlite3 *database;
    NSString * myRowData=[[NSString alloc] initWithFormat:@”%@”,@””];
    const char *sqlStatement;
    int columnCounter=0;
   
    // Open the database from the users filessytem
    if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        sqlStatement=[[NSString stringWithFormat:@”%@”,query ] UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
        {
            columnCounter=sqlite3_column_count(compiledStatement);
            while(sqlite3_step(compiledStatement) == SQLITE_ROW)
            {
                for(int i=0;i<columnCounter;i++)
                {
                    //NSString * test = @”Test”;
                   
                    NSString * test = ((char *)sqlite3_column_text(compiledStatement, i))  ? [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, i)] : @””;
                    NSString * colName = [NSString stringWithUTF8String:(char *)sqlite3_column_name(compiledStatement, i)];
                    myRowData = [myRowData stringByAppendingString:test];
                   
                    if(i != columnCounter )
                    {
                        [subDic setObject:myRowData forKey:colName];
                        myRowData = [[NSString alloc] initWithFormat:@””];
                    }
                }
                [mainArray addObject:subDic];
                subDic = nil;
                subDic = [[NSMutableDictionary alloc] init];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
    return mainArray;
}

tableview

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{  
    static NSString *CellIdentifier = @”Cell”;
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
       
    }
   
   
    cell.textLabel.text=[[arrayDataFromDb objectAtIndex:indexPath.row] valueForKey:@”Cat_Name”];
    //cell.detailTextLabel.text=
    //[[arrayDataFromDb objectAtIndex:indexPath.row]valueForKey:@”Table_Name”];
   
    return cell;

}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return [arrayDataFromDb count];
}
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    //[[arrayDataFromDb objectAtIndex:indexPath.row]valueForKey:@”Cat_Name” ];
    //[[arrayDataFromDb objectAtIndex:indexPath.row]valueForKey:@”Table_Name”];
    DetailViewController *detail=[[DetailViewController alloc]initWithNibName:@”DetailViewController” bundle:nil];
       detail.TableDict=[arrayDataFromDb objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}

-(NSString *) getDBPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *insPath  = [NSString stringWithFormat:@”UnitConverterDB.sqlite”];
    NSString *destPath = [documentsDirectory stringByAppendingPathComponent:insPath];
    return destPath;
}

UIActionSheet

Image

This is a simple ActionSheet demo for iOS beginners.

Image

You can Find  Code Here

ActionSheetDemoViewController.h

#import <UIKit/UIKit.h>

@interface ActionSheetDemoViewController : UIViewController<UIActionSheetDelegate>

@end

ActionSheetDemoViewController.m

#import “ActionSheetDemoViewController.h”

@interface ActionSheetDemoViewController ()
-(void)showActionSheet:(id)sender;
@end

@implementation ActionSheetDemoViewController

-(void)showActionSheet:(id)sender;
{
NSString * actionSheetTitle = @”Action Sheet Demo”;
NSString * destrcutiveTitle = @”Destructive Title”;
NSString * other1= @”Other Button 1″;
NSString * other2 = @”Other Button 2″;
NSString * other3 = @”Other Button 3″;
NSString * cancelTitle= @”Cancel Button”;

UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:actionSheetTitle
delegate:self
cancelButtonTitle:cancelTitle
destructiveButtonTitle:destrcutiveTitle
otherButtonTitles:other1, other2, other3, nil];
actionSheet.actionSheetStyle=UIActionSheetStyleAutomatic;
[actionSheet showInView:self.view];

}
– (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{

NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];

if ([buttonTitle isEqualToString:@”Destructive Title”])
{
UIActionSheet * desactionsht=[[UIActionSheet alloc]initWithTitle:@”Destructive pressed” delegate:self cancelButtonTitle:@”Cancel”destructiveButtonTitle:@”Hello” otherButtonTitles:@”option 1″,@”option 2″,@”option 3″, nil];
desactionsht.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
[desactionsht showInView:self.view];
NSLog(@”Destructive pressed –> Delete Something…”);
}
if ([buttonTitle isEqualToString:@”Other Button 1″])
{
NSLog(@”Other 1 pressed”);
}
if ([buttonTitle isEqualToString:@”Other Button 2″])
{
NSLog(@”Other 2 pressed”);
}
if ([buttonTitle isEqualToString:@”Other Button 3″])
{
NSLog(@”other 3 pressed”);
}
if ([buttonTitle isEqualToString:@”Cancel Button”])
{

UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@”Informative Alert” message:@”You pressed Cancel buttton” delegate:self cancelButtonTitle:@”Cancel” otherButtonTitles:@”Okay..”, nil];
alrt.alertViewStyle=UIAlertViewStyleDefault;
[alrt show];
}
}

– (void)viewDidLoad
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(20.0f, 186.0f, 280.0f, 88.0f);
[button setTitle:@”Show ActionSheet” forState:UIControlStateNormal];
button.tintColor = [UIColor whiteColor];
[button addTarget:self action:@selector(showActionSheet:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.
}

– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}