Wednesday, 24 October 2012

Open htaccess Password Protected Web-Page in UIWebView


In some cases we want to access a password protected (htaccess) webpage from UIWebView iPhone.
So for this we need add username and password between 'http://' and 'page url'. 
e.g. @"http://username:password@192.165.1.11/mypage"


NSString *url = @"http://username:password@192.165.1.11/mypage";
[webViewForhtaccessPageRendering loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: url]]];

here webViewForhtaccessPageRendering is UIWebVIew.

Monday, 22 October 2012

UITableView - Add or Remove Rows Dynamically (Objective C & Swift Source codes)


In some situations we need to add or remove rows dynamically from a UITableView.
We can achieve it very easily, just got through the tutorial :

1. Create a new project.
2.  In AppDelegate implementation file create an object of UINavigationController (navController here).




3. Add object of UIViewController (viewController here) as rootviewcontroller of UINavigationController.
4. In ViewController.xib nib file take a UITableView.
5. Set delegate and datasource of UITableView to File's Owner.
6. Create an IBOutlet to UITableView (tableViewForScreen here) in ViewControler.h header file.













7. Declare an object of NSMutableArray (arrayForTableContent here) in ViewControler.h header file.
8. In ViewController.m file in viewDidLoad method set edit and add buttons as leftBarButtonItem and rightBarButtonItem respectively.
9. Initialize arrayForTableContent array and add objects to it.
























10. Now set number of rows, section of UITableView.
11. In cellForRowAtIndexPath set the table cell label.














12. In  'setEditing:(BOOL)editing animated:(BOOL)animated' method set tableview editing and animated.





























13. In commitEditingStyle forRowAtIndexPath method:
      If editingStyle is delete remove object from array.
      And using 'deleteRowsAtIndexPaths withRowAnimation'; remove the row at provided index path and with animation style.

























14. To insert rows dynamically call insertNewObject method on rightbarbuttonitem.
15. In insertNewObject method add an object to array using 'insertObject atIndex'.
16. And for inserting row to tableview use 'insertRowsAtIndexPaths withRowAnimation' method.

























You can download Code
Swift: Swift_Editable_UITableView

Friday, 12 October 2012

Implementing iAd in iPhone/iPad/iPod Touch Application

Implementing iAds in your iPhone/iPad Application is really very simple.

1. First add "iAd.framework" Framework from Targets -> Build Phases -> Link Binary With Libraries.



2. In ViewController.h #import  <iAd/ADBannerView.h>
3. Take ADBannerViewDelegate.




4. Now take a ADBannerView in ViewController.xib.
5. Set IBOutlet to ADBannerView (as iADBannerView here).
6. Set delegate of ADBannerView to File Owner(self).




7. In ViewController.m set iADBannerView as Hidden in viewDidLoad method. Which will hide the ADBannerView while loading controller screen.
8. Now add delegate methods of ADBannerView mentioned below:

- (void)bannerViewDidLoadAd:(ADBannerView *)banner;

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error;

9. In bannerViewDidLoadAd method unhide the ADBannerView. As method name specifies this method is executed when ADBannerView loads.

10. If there is any issue in loading ADBannerView the bannerView: didFailToReceiveAdWithError is executed.






11. Now when top tap on ADBannerView it will show you a "Test Advertisement" screen as shown in below snapshot. This means you are all done, and ready to go.





You can download sample code from here: iAdDemo





Tuesday, 9 October 2012

Validate and Parse NSURL in Objective C


I need to check if Url is valid or not in my iPhone application so i found below snippet from  


We can validate a url by simply using Foundation Framework as follows: 

        NSURL *myURL = [NSURL URLWithString:@"http://google.com"];
        if (myURL && myURL.scheme && myURL.host) {
            NSLog(@"URL is validated");
        } else {
            UIAlertView *alertMsg = [[UIAlertView alloc] initWithTitle:@"Error" 
                                     message:@"URL is invalid" delegate:nil 
                                     cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertMsg show];
            [alertMsg release];
        }


Output: 
URL is validated


Above can be achieved using RegexKit also as follows:

    NSString *regexExpression = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSString *urlString = @"http://google.com";
    
    NSString *matchedString = [urlString stringByMatching:regexExpression];

if you want to validate url string like: www.google.com just replace
(http|https):// with ((http|https)://)* 

You can download RegexKit from 
http://regexkit.sourceforge.net/


Some time we need to parse a NSURL and fetch a particular part out from it.
We can parse a NSURL as follows:

            NSURL *url = [NSURL URLWithString:@"https://www.google.co.in/search?q=validate+url+objective+c&oq=validate+url+objective+c&sugexp=chrome,mod=6&sourceid=chrome&ie=UTF-8"];

            NSLog(@"URL Scheme: %@", [url scheme]); 
            NSLog(@"URL Host: %@", [url host]); 
            NSLog(@"URL Port: %@", [url port]);     
            NSLog(@"URL Path: %@", [url path]);     
            NSLog(@"URL Relative path: %@", [url relativePath]);
            NSLog(@"URL Path components as array: %@", [url pathComponents]);        
            NSLog(@"URL Parameter string: %@", [url parameterString]);   
            NSLog(@"URL Query: %@", [url query]);       
            NSLog(@"URL Fragment: %@", [url fragment]);


Output:
 Scheme: https
 Host: www.google.co.in
 Port: (null)
 Path: /search
 Relative path: /search
 Path components as array: (
    "/",
    search
)
 Parameter string: (null)
 Query: q=validate+url+objective+c&oq=validate+url+objective+c&sugexp=chrome,mod=6&sourceid=chrome&ie=UTF-8
 Fragment: (null)











Thursday, 4 October 2012

Database implementation using SQLite in iOS Applications


SQLite: 
I have worked on Core Data and SQLite both, but i personally feel that using SQLite is a good choice.
Working on Database directly using SQLite is better than working on Core Data which actually works as a wrapper above SQLite.
We can easily fire complex sql queries on Database using SQLite to store, update or fetch data; which is a pathetic job using Core Data. 

Below is a simple demo app which will help you understand how to use SQLite to create database, store, update, fetch and delete records from database.

For using SQLite database in iPhone, iPad App first you need to link libsqlite3.0.dylib to your build target. 
To do this select 'Project' at top left navigation panel -> 'under Targets' select your build target -> then click on 'Build Phases' tab -> then in 'Link Binary With Libraries' click on '+' button and add libsqlite3.0.dylib.




 There are number of C functions contained within the libsqlite3.dylib library which can be used for sqlite implementation.

1. sqlite3_open() - This function is used to opens database. In case database does not exist, it creates one.
2. sqlite3_prepare_v2() – This function is used to prepares a SQL statement for execution.
3. sqlite3_step() – This function is used to executes a SQL statement previously prepared by the sqlite3_prepare_v2() function.
4. sqlite3_column_<type>() – This function is used to returns a data field from the results of a SQL retrieval operation. Where <type> is the type of the data to be extracted (int, text, blob, bytes, etc).
5. sqlite3_finalize() - This function is used delete a previously prepared SQL statement.
6. sqlite3_exec() – This function is used combine the functionality of sqlite3_prepare_v2(), sqlite3_step() and sqlite3_finalize() into a single function call.
7. sqlite3_close() – This function is used to close a database file previously opened.


Below is the code snippet for using sqlite implementation:

In header file:

In implementation file:







In xib file:


Here is the sample code: SQLITE_CODE