sqlite in iphone

using sqlite database in iphone is having the following steps
1. first we need to create the database using commandline or firefox addon
2. now copy the database to the xcode project(drag and drop to project files)
3. write a funtion to get the database path.
-(NSString *)getDataBasePath
{   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory , NSUserDomainMask, YES);
    NSString *dbPath = [paths objectAtIndex:0];
    return [dbPath stringByAppendingPathComponent:@”databaseName.sqlite”];   
}
here i used NSLibraryDirectory, read the post so that we can decide where the database will be stored and then decided which is suitable for you.
4. now check weather database is available at the above path if not copy the data base from the resources
-(void)copyDatabaseIfNeeded
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSString *dbPath = [self getDataBasePath];
    NSString *resPath;
    BOOL success = [fileManager fileExistsAtPath:dbPath];
    if(!success)
    {
        resPath = [[NSBundle mainBundle] pathForResource:@”databaseName” ofType:@”sqlite”];
        success = [fileManager copyItemAtPath:resPath toPath:dbPath error:&error];
        if(!success)
        {
            NSLog(@”Failed to copy ‘%@’.”, [error localizedDescription]);
        }
        else
        {
            NSLog(@”successfully copied”);
        }
    }
}
5. now open the databasesqlite3 *dbConnect;
    NSString *dbPath=[self getDataBasePath];
    if (sqlite3_open([dbPath UTF8String], &dbConnect) == SQLITE_OK)
    {
        NSLog(@”opened the data base”);
    }
    else {
        NSLog(@”failed to open the database”);
    }
sqlite3_open will take two arguments. first argument is database path and is of type const char *, second one is database connection, it is of type splite3.
6. now we need to prepare the statement as follows
NSString *query = @”insert into emp values(123,123)”;
    const char *pre_query = [query UTF8String];
    sqlite3_stmt statement=nil;
    if (sqlite3_prepare_v2(dbConnect, pre_query, -1, &statement, NULL) == SQLITE_OK)
    {
        NSLog(@”prepared”);
    }
    else
    {
        NSLog(@”error in preparing”);
    }

sqlite3_prepare_v2 will take 5 arguments, first one is database connection, second one is prepared query of type const char *, fouth one is the pointer to the prepared statement result will be placed in this argument,

7. above preparation statement is static, most of the times we need to prepare the statement dynamically at that time we need to create the char pointer as
const char *stmt=”insert into xyz values(?,?)”;
so that we can bind the arguments to ‘?’ dynamically. to bind the value to ‘?’ use the following syntax.
    sqlite3_bind_text(<sqlite statement>,<position>,<value>,-1,SQLITE_TRANSIENT);

we maight face some problems when binding  the arguments, at that time i preferred to use the statement as follows
    NSString *str=[NSString stringWithFormat:@”insert into xyz values(%@,%@)”,val1, val2];

    const char *stmt=[str utf8String];

8. now execute the statement which was prepared in the above steps
    sqlite3_step(<statement>);
depends on the statement it will return the relusts if the statement returns rows than sqlite3_step will return SQLITE_ROW or if it is insert or update or delete statement it will return SQLITE_DONE, if it fails SQLITE_ERROR, there are other return values too,
to get the colomn values
sqlite3_column_text(<sqlite statement>,<colomn number>);//for text
9. reset the statement to nil
    sqlite3_finalize(<compiled statement>);
10. finally close the database connection sqlite3_colose(<database name>);

writable paths in iphone

we use NSDocumentDirectory mostly to save files which are used in the project. but we have two other paths in the iPhone application,  they are also read and writable paths as documents directory. now lets see the difference between all three

NSDocumentDirectory:
it is permanent storage(exists until the app will be deleted)
and all files which are in this directory are sharable in iTunes

NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [docs objectAtIndex:0];

NSLibraryDirectory:
it is permanent storage (exists until the app will be deleted)
and files in this directory will not be shared in iTunes.

NSArray *lib = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *library = [lib objectAtIndex:0];

NSCachesDirectory:
this is temporary storage i.e, files will be deleted by iphone(i think these files will be deleted if app is terminated) and files in this direcotry will not be sharable in iTunes. its better to store temperary files

NSArray *cach = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cache = [cache objectAtIndex:0];

x-code keyboard shortcuts

 

source apple.com

Table 1-1  Xcode menu command shortcuts
Command Key Binding Shortcut
About Xcode
Preferences… ⌘, Command-,
Hide Xcode ⌘H Command-H
Hide Others ⌥⌘H Option-Command-H
Show All
Quit Xcode ⌘Q Command-Q

Table 1-2  File menu command shortcuts
Command Key Binding Shortcut
New Tab ⌘T Command-T
New Window ⇧⌘T Shift-Command-T
New File… ⌘N Command-N
New Target…
New Project… ⇧⌘N Shift-Command-N
New Workspace ⌃⌘N Control-Command-N
New Group ⌥⌘N Option-Command-N
New Group from Selection
Add Files… ⌥⌘A Option-Command-A
Open… ⌘O Command-O
Open Quickly… ⇧⌘O Shift-Command-O
Close Window ⌘W Command-W
Close All Windows ⌥⌘W Option-Command-W
Close Tab ⇧⌘W Shift-Command-W
Close Other Tabs ⌥⇧⌘W Option-Shift-Command-W
Close Document ⌃⌘W Control-Command-W
Close Workspace
Save ⌘S Command-S
Save All ⌥⌘S Option-Command-S
Save Multiple… ⌥⇧⌘S Option-Shift-Command-S
Save As… ⇧⌘S Shift-Command-S
Revert…
Unlock…
Show in Finder
Open with External Editor
Workspace Settings…
Commit… ⌥⌘C Option-Command-C
Update… ⌥⌘X Option-Command-X
Update All ⌃⌥⌘X Control-Option-Command-X
Merge…
Discard Changes…
Add
Ignore
Mark as Resolved
Refresh Status
Show Repository Status
Repositories…
Create Snapshot… ⌃⌘S Control-Command-S
Page Setup… ⇧⌘P Shift-Command-P
Print… ⌘P Command-P

Table 1-3  Edit menu command shortcuts
Command Key Binding Shortcut
Undo ⌘Z Command-Z
Redo ⇧⌘Z Shift-Command-Z
Cut ⌘X Command-X
Copy ⌘C Command-C
Paste ⌘V Command-V
Paste Special ⌥⌘V Option-Command-V
Paste and Match Style ⌥⇧⌘V Option-Shift-Command-V
Duplicate ⌘D Command-D
Delete
Select All ⌘A Command-A
Find in Workspace… ⇧⌘F Shift-Command-F
Find and Replace in Workspace… ⌥⇧⌘F Option-Shift-Command-F
Find… ⌘F Command-F
Find and Replace… ⌥⌘F Option-Command-F
Find Next ⌘G Command-G
Find Previous ⇧⌘G Shift-Command-G
Use Selection for Find ⌘E Command-E
Use Selection for Replace ⇧⌘E Shift-Command-E
Filter in Navigator ⌥⌘J Option-Command-J
Filter in Library ⌥⌘L Option-Command-L
Show Fonts ⌃⇧⌘T Control-Shift-Command-T
Bold
Italics
Underline
Show Colors
Show Spelling and Grammar ⌘: Command-:
Check Document Now
Check Spelling While Typing
Check Grammar With Spelling
Correct Spelling Automatically
Show Substitutions
Smart Copy/Paste
Smart Quotes
Smart Dashes
Smart Links
Data Detectors
Text Replacement
Make Upper Case
Make Lower Case
Capitalize
Start Speaking
Stop Speaking
Rename
Extract
Create Superclass
Move Up
Move Down
Encapsulate

Table 1-4  View menu command shortcuts
Command Key Binding Shortcut
Project ⌘1 Command-1
Symbol ⌘2 Command-2
Search ⌘3 Command-3
Issue ⌘4 Command-4
Debug ⌘5 Command-5
Breakpoint ⌘6 Command-6
Log ⌘7 Command-7
Show Navigator ⌘0 Command-0
Standard ⌘↩ Command-↩
Assistant ⌥⌘↩ Option-Command-↩
Version ⌥⇧⌘↩ Option-Shift-Command-↩
Show Related Items ⌃1 Control-1
Show Previous History ⌃2 Control-2
Show Previous Files History ⌃⌘2 Control-Command-2
Show Next History ⌃3 Control-3
Show Next Files History ⌃⌘3 Control-Command-3
Show Top Level Items ⌃4 Control-4
Show Group Files ⌃5 Control-5
Show Document Items ⌃6 Control-6
Show Issues ⌃7 Control-7
Add Assistant Editor
Remove Assistant Editor ⌃⇧⌘W Control-Shift-Command-W
Reset Editor ⌥⇧⌘Z Option-Shift-Command-Z
Assistant Editors on Right
Assistant Editors on Bottom
Assistant Editors on Left
Assistant Editors on Top
Assistant Editors Stacked Horizontally
Assistant Editors Stacked Vertically
Assistant Editors Stacked Horizontally Right to Left
Assistant Editors Stacked Vertically Bottom to Top
Show Toolbar Bar
Show Tab Bar
Show Debug area ⇧⌘Y Shift-Command-Y

Table 1-5  Navigate menu command shortcuts
Context Command Key Binding Shortcut
Reveal in Project Navigator ⌘L Command-L
Open in Adjacent Editor ⌘< Command-<
Go Forward ⌃⌘⇢ Control-Command-Right Arrow
Go Forward in Alternate Editor ⌃⌥⌘⇢ Control-Option-Command-Right Arrow
Go Back ⌃⌘⇠ Control-Command-Left Arrow
Go Back in Alternate Editor ⌃⌥⌘⇠ Control-Option-Command-Left Arrow
Jump to Selection ⌘J Command-J
Jump to Definition ⇧⌘D Shift-Command-D
Move Focus To Next Area ⌥⌘K Option-Command-K
Move Focus To Previous Area ⌥⇧⌘K Option-Shift-Command-K
Interface Builder Jump to Next Object with Clipped Content
Jump to Previous Object with Clipped Content
Source editor Jump to Counterpart ⌃⌘⇡ Control-Command-Up Arrow
Jump To… ⇧⌘J Shift-Command-J
Jump to Next Placeholder ⌃/ Control-/
Jump to Previous Placeholder ⌃? Control-?
Jump to Next Issue ⌘’ Command-‘
Jump and Fix Next Issue ⌃⌘’ Control-Command-‘
Jump to Previous Issue ⌘” Command-“
Jump and Fix Previous Issue ⌃⌘” Control-Command-“

Table 1-6  Editor menu command shortcuts
Context Command Key Binding Shortcut
Data model Show Grid
Show Page Breaks
Add Entity
Add Fetch Request
Add Configuration
Add Attribute ⌃⌘A Control-Command-A
Add Fetched Property
Add Relationship ⌃⌘R Control-Command-R
Import…
Create NSManagedObject subclass…
Documentation Explore Documentation
Search Documentation
Documentation Bookmarks
Zoom In
Zoom Out
Actual Size
Add Bookmark
History
Interface Builder Show Bounds Rectangles
Show Layout Rectangles
Snap to Guides
Live Autoresizing
Align Left Edges ⌘[ Command-[
Align Right Edges ⌘] Command-]
Align Top Edges
Align Bottom Edges
Align Horizontal Centers
Align Vertical Centers
Align Baselines
Align Horizontal Center In Container
Align Vertical Center In Container
Send to Front
Send to Back
Send Forward
Send Backward
Unembed
Size to Fit ⌘= Command-=
Add Horizontal Guide ⌘_ Command-_
Add Vertical Guide ⌘| Command-|
Reveal in Document Structure
Simulate Document
PDF Automatically Resize
Zoom In
Zoom Out
Actual Size
Single Page
Single Page Continuous
Two Pages
Two Pages Continuous
Next Page ⌥⌘⇣ Option-Command-Down Arrow
Previous Page ⌥⌘⇡ Option-Command-Up Arrow
RTF Insert Page Break
Insert Line Break
Insert Paragraph Break
Show Ruler
Add Link…
Allow Hyphenation
Scripting definition Make Text Bigger ⌘+ Command-+
Make Text Smaller ⌘- Command–
Source code Show Completions ⌃\ Control-\
Edit All in Scope ⌃⌘E Control-Command-E
Fix All in Scope ⌃⌘F Control-Command-F
Show Issue
Show All Issues ⌃⌘M Control-Command-M
All Issues
Errors Only
Balance Delimiter
Re-Indent
Shift Right ⌘] Command-]
Shift Left ⌘[ Command-[
Move Line Up ⌥⌘[ Option-Command-[
Move Line Down ⌥⌘] Option-Command-]
Comment Selection ⌘/ Command-/
Fold ⌥⌘⇠ Option-Command-Left Arrow
Unfold ⌥⌘⇢ Option-Command-Right Arrow
Unfold All
Fold Methods & Functions ⌥⇧⌘⇠ Option-Shift-Command-Left Arrow
Unfold Methods & Functions ⌥⇧⌘⇢ Option-Shift-Command-Right Arrow
Fold Comment Blocks ⌃⇧⌘⇠ Control-Shift-Command-Left Arrow
Unfold Comment Blocks ⌃⇧⌘⇢ Control-Shift-Command-Right Arrow
Focus Follows Selection
Show Invisibles
Xcode project Add Target…
Hex editor Overwrite Mode ⌥⇧⌘O Option-Shift-Command-O

Table 1-7  Product menu command shortcuts
Command Variant Key Binding Shortcut
Run ⌘R Command-R
Run…1 ⌥⌘R Option-Command-R
Test ⌘T Command-T
Test…1 ⌥⌘T Option-Command-T
Profile ⌘I Command-I
Profile…1 ⌥⌘I Option-Command-I
Analyze ⌃⌘B Control-Command-B
Analyze…1 ⌃⌥⌘B Control-Option-Command-B
Archive
Build For Build For Running ⇧⌘R Shift-Command-R
Build For Testing ⇧⌘U Shift-Command-U
Build for Profiling ⇧⌘I Shift-Command-I
Build for Archiving
Perform Action Run Without Building ⌃⌘R Control-Command-R
Test Without Building ⌃⌘U Control-Command-U
Profile Without Building ⌃⌘I Control-Command-I
Build ⌘B Command-B
Clean ⇧⌘K Shift-Command-K
Clean Build Folder… ⌥⇧⌘K Option-Shift-Command-K
Stop ⌘. Command-.
Debug Pause ⌃⌘Y Control-Command-Y
Step Into F7 Function 7
Step Over F6 Function 6
Step Out F8 Function 8
Step Into Thread ⌃⇧F7 Control-Shift-Function 7
Step Into Instruction ⌃F7 Control-Command-Function 7
Step Over Thread ⌃⇧F6 Control-Shift-Function 6
Step Over Instruction ⌃F6 Control-Function 6
Add Breakpoint at Current Line ⌘\ Command-\
Activate BreakpointsDeactivate Breakpoints ⌘Y Command-Y
Shared Libraries…
Clear Console ⌘K Command-K
Window Behavior Normal
Xcode Behind
Xcode In Front
Edit Scheme… ⌘< Command-Less Than
New Scheme…
Manage Schemes…

1 Opens the scheme editing dialog for editing prior to performing the chosen command.

Table 1-8  Window menu command shortcuts
Command Key Binding Shortcut
Minimize ⌘M Command-M
Zoom
Select Next Tab ⌘} Command-}
Select Previous Tab ⌘{ Command-{
Welcome to Xcode ⇧⌘1 Shift-Command-1
Organizer ⇧⌘2 Shift-Command-2
Bring All to Front

Table 1-9  Help menu command shortcuts
Command Key Binding Shortcut
Xcode Help
Xcode 4 Transition Guide
Quick Help ⌃⌘? Control-Command-?
Search Documentation for Selected Text ⌃⌥⌘/ Control-Option-Command-/
Developer Documentation ⌥⌘? Option-Command-?

UIWebView navigation(back/forward) url

 

Hi all,
Here am posting one question.

I faced small problem while working with UIWebView.  We browse the web using webview, normally we use –(void)goBack, –(void)goForward, (or by clicking hyperlink) methods for navigation. But my requirement is to get the url of previous page(back) and next page(forward) from the current page i google it but nothing helped me. could you please help me. please write comment(top right of the post)
thanks in advance.
naresh

setting color for UInavigationBar title

 

To set color for the title of the Navigation bar we need to over write the setter method of the title as
– (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor whiteColor];
        
        titleView.textColor = [UIColor blackColor]; // Change to desired color
        
        self.navigationItem.titleView = titleView;
        [titleView release];
    }
    titleView.text = title;
    [titleView sizeToFit];
}

adding images to UIToolbar buttons

 

UIToolbar doesnt allow color images on it by default. so we need to add the button as
    UIImage *buttonImage = [UIImage imageNamed:@”buttonImage.png”];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
    [button addTarget:self action:@selector(creteNewFolder) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *newBarBtn = [[UIBarButtonItem alloc] initWithCustomView:button];