Friday, February 25, 2011

parsing HTML file for iPhone apps using xcode, obj-c

Hay,

Recently I made a project where the application need to read the HTML file , parse it and display the data on the view.

Before you start , get the below files from https://github.com/topfunky/hpple


  1. HTFpple.h
  2. HTFpple.m
  3. HTFppleElement.h
  4. HTFppleElement.m
  5. XPathQuery.h
  6. XPathQuery.m

Once you get that, just do the following modifications to your project settings:

1) Goto Target on the left hand side tree. Right click on your project name and hit Get Info option from popup menu.

2) Select All configurations from Configuration combo box.

3) Search for Header Search path and the bloodline with recursive option selected

${SDKROOT}/usr/include/libxml2

4) In the same way, search for Other Linker Flag


add the below line to it:


-lxml2 


Here is the code to parse the HTML file. [ This code works for reading local HTML file attached in your project ]







NSMutableArray *array = [[NSMutableArray alloc]init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myHTMLfileNameWithoutExtension" ofType:@"html"];
NSData *htmlData = [[NSData alloc] initWithContentsOfFile:filePath];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *chapterName  = [xpathParser search:@"//html//h1"]; // get the page title
self.chapterIndexData = chapterName;
[xpathParser release];
[htmlData release];
[array release];


Above code reads all the H1 tags in the HTML page and put them in 

chapterIndexData
  which is of type NSArray *.



Please let me know if you have any doubts regarding this.




Tuesday, January 11, 2011

show text box while typing from keyboard/keypad in iPhone

Some times, while you try to enter the text in text field's , you might not be able to see the text which you are typing in the text box. It get hide with the popup keyboard. If you don't want to hide the text box while typing, just add the below 2 functions in your code.


- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardShown )
        return;
    if( textView.text != 0 ) {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height-44;
        frame.size.height += keyboardSize.height-44;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];
        viewMoved = YES;
    }
    keyboardShown = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height-44;
        frame.size.height -= keyboardSize.height-44;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];
        viewMoved = NO;
    }
    keyboardShown = NO;
}

Observe the screen shots below before and after the code:

Actual screen before typing

While typing, entire screen will be moved to upwards

Pleaes let me know if you have any questions or concerns.

Happy coding :)