0

I am a beginner in Obj-C and I try to make an app with this raywenderlich tutorial.

I parse html page with Title of articles (mininglife.ru) , also I getting an urls of each articles and keep it in an NSMutableArray for using it in my DetailviewController.

But when I try to pull urls off and put it in NSUrl nothing happens. I've tried a lot of ways, Can you help me with it and try to explain if I do something wrong. Thank you.

-(void)loadArticles{

   Articlelist *urlOfArticle = [_objects objectAtIndex:self]; // from previous parsing

   NSURL *articleUrl = [NSURL URLWithString:urlOfArticle.url];// I think this string is wrong, or?
   NSData *htmlUrlsData = [NSData dataWithContentsOfURL:articleUrl];

   TFHpple *articleParser = [TFHpple hppleWithHTMLData:htmlUrlsData];

   NSString *articleNameXpath = @"/html/body/div[1]/div/div[1]/main/article/h1";
   // NSString *articleContentXpath = @"//div[@class'entry-content']//p";

   NSArray *articleNameNodes = [articleParser searchWithXPathQuery:articleNameXpath];
   // NSArray *articleContentNodes = [articleParser searchWithXPathQuery:articleContentXpath];
   NSMutableArray *newArticleArray = [[NSMutableArray alloc] initWithCapacity:0];

   for (TFHppleElement *element in articleNameNodes) {
       Article *newArticleName = [[Article alloc] init];
       [newArticleArray addObject:newArticleName];

       newArticleName.name = [[element firstChild] content]; 
   }

   _articleName = newArticleArray;

   [self.tableView reloadData];
}
Kerberos
  • 4,036
  • 3
  • 36
  • 55

1 Answers1

0

Change interface to this

@interface MasterViewController () {
    NSMutableArray *_articles;
}
@end

Add this to MasterViewController

- (void)loadArticles
{
    NSURL *articlesUrl = [NSURL URLWithString:@"http://mininglife.ru"];
    NSData *articlesHtmlData = [NSData dataWithContentsOfURL:articlesUrl];
    TFHpple *articlesParser = [TFHpple hppleWithHTMLData:articlesHtmlData];
    NSString *articlesXpathQueryString = @"/html/body/div[1]/div/div[1]/main/article/h1";

    NSArray *articlesNodes = [articlesParser searchWithXPathQuery:articlesXpathQueryString];

    NSMutableArray *newArticles = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in articlesNodes) {
        Article *article = [[Article alloc] init];
        [newArticles addObject:article];
        article.title = [[element firstChild] content];
        article.url = [element objectForKey:@"href"];
    }
    _articles = newArticles;
    [self.tableView reloadData];
}

Change DetailviewController.h's interface to

@class Article;
@interface DetailViewController : UIViewController
    @property (strong, nonatomic) Article *article;
    @property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end

Change DetailViewController.m to

#import "DetailViewController.h"
#import "Article.h"
#import "TFHpple.h"

@implementation DetailViewController

@synthesize detailDescriptionLabel = _detailDescriptionLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadArticle];
}

- (void)loadArticle
{
    NSURL *articleUrl = [NSURL URLWithString:self.article.url];
    NSData *articleHtmlData = [NSData dataWithContentsOfURL:articleUrl];

    TFHpple *articleParser = [TFHpple hppleWithHTMLData:articleHtmlData];
    NSString *articleXpathQueryString = @"//div[@class'entry-content']//p";

    NSArray *articleNodes = [articleParser searchWithXPathQuery:articleXpathQueryString];

    NSMutableArray *newArticleContents = [[NSMutableArray alloc] initWithCapacity:0];
    for (TFHppleElement *element in articleNodes) {
        [newArticleContents addObject:[[element firstChild] content]];
    }
    NSLog(@"%@", newArticleContents);
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Detail", @"Detail");
    }
    return self;
}

@end

Don't forget to change all call to loadTutorials to loadArticles in MasterViewController. PS: This example only prints the articles content to the console

Your xPath might be wrong as well

Bohrnsen
  • 79
  • 6