I would like to retrieve the data from a specific part of the html of a site, and I wrote the following code. Unfortunately, the code is not working. How can I fix this?
the html class name is "topic_content", and every article in applicable site contains it (see the comments in code).
#implementation DetailViewController
....
UIActivityIndicatorView *activityIndicator;
- (void)viewDidLoad {
.....
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator stopAnimating];
....
[self callDetailNews];
}
-(void)callDetailNews{
....
if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]!=NotReachable)
{
// article link :: http://el3en.com/?articles=topic&topic=20840 (token_ID)
// <td class="topic_content" colspan='6' itemprop="articleBody" style="text-align:justify; word-wrap: break-word; width:100%">
// NSString *someHTML = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('box')[0].innerHTML;"];
NSString *articleString = [NSString stringWithFormat:#"http://el3en.com/?articles=topic&topic=%#",token_ID];
NSURL *articleURL = [NSURL URLWithString:articleString];
NSError *error;
NSString *articlePage = [NSString stringWithContentsOfURL:articleURL
encoding:NSASCIIStringEncoding
error:&error];
[_webView loadHTMLString:articlePage baseURL:nil];
//NSString *someHTML = [_webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].innerHTML;"];
//NSLog(#"Content : %#",someHTML);
}
.....
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
self.webView.hidden=true;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].sinnerHTML;"];
[activityIndicator stopAnimating];
self.webView.hidden=false;
}
Thanks
You should use innerHtml instead sinnerHtml, besides this you need to get the string and load this string into another webview. Pls see example
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UIWebView *fakeView;
#end
#implementation ViewController
UIActivityIndicatorView *activityIndicator;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator stopAnimating];
[self callDetailNews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)callDetailNews{
if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]!=NotReachable)
{
// article link :: http://el3en.com/?articles=topic&topic=20840 (token_ID)
// <td class="topic_content" colspan='6' itemprop="articleBody" style="text-align:justify; word-wrap: break-word; width:100%">
// NSString *someHTML = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('box')[0].innerHTML;"];
NSString *articleString = [NSString stringWithFormat:#"http://el3en.com/?articles=topic&topic=%#",#"20840"];
NSURL *articleURL = [NSURL URLWithString:articleString];
NSError *error;
NSString *articlePage = [NSString stringWithContentsOfURL:articleURL
encoding:NSASCIIStringEncoding
error:&error];
NSLog(#"Request %#",articleURL);
self.fakeView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.fakeView.delegate = self;
[self.fakeView loadRequest:[NSURLRequest requestWithURL:articleURL]];
//NSString *someHTML = [_webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].innerHTML;"];
//NSLog(#"Content : %#",someHTML);
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
self.webView.hidden=true;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"error %#" ,[error description]);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if(webView == self.fakeView) {
NSString* javascriptString = [self.fakeView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].innerHTML"];
NSLog(#"%#",javascriptString);
[self.webView loadHTMLString:javascriptString baseURL:[NSURL URLWithString:#""]];
self.fakeView = nil;
}else{
[activityIndicator stopAnimating];
self.webView.hidden=false;
}
}
#end
Related
I am trying to get json response from a web-service api. I want to extract product data from the json. I also want to implement this using AFNetworking and i also trying to get response using NSURLSession and its completely working.
viewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *tblTableView;
- (IBAction)btnClickedPostData:(id)sender;
#end
viewController.m
#import "ViewController.h"
#import "AFNetworking.h"
#import "ResponseTableViewCell.h"
#interface ViewController ()
{
NSMutableDictionary *dictArray;
NSMutableArray *dataArray;
}
#end
#implementation ViewController
static NSString *CellIdentifier = #"cell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.tblTableView registerNib:[UINib nibWithNibName:#"ResponseTableViewCell" bundle:nil] forCellReuseIdentifier:#"ResponseTableViewCell"];
[self connectionString];
}
-(void)connectionString
{
//NSURL *URL = [NSURL URLWithString:#"Your URL"];
NSURLSession *session = [NSURLSession sharedSession]; // its working
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:#"YOUR URL"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#", jsonResponse);
dataArray = [jsonResponse objectForKey:#"objEventcategoryList"];
self.tblTableView.dataSource = self;
self.tblTableView.delegate = self;
[self.tblTableView reloadData];
NSLog(#"JSON: %#", dataArray);
}];
[dataTask resume];
// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // its working
// [manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
//
// NSMutableDictionary *jsonResponse = (NSMutableDictionary *)responseObject;
// dataArray = [jsonResponse objectForKey:#"objEventcategoryList"];
//
// self.tblTableView.dataSource = self;
// self.tblTableView.delegate = self;
//
// [self.tblTableView reloadData];
//
// NSLog(#"TableView: %#", _tblTableView);
//
//
// NSLog(#"JSON: %#", dataArray);
// } failure:^(NSURLSessionTask *operation, NSError *error) {
// NSLog(#"Error: %#", error);
// }];
}
#pragma marrk
#pragma marrk - TableView DataSource and Deleget
#pragma marrk
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataArray count];// its not working
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ResponseTableViewCell *cell = (ResponseTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"ResponseTableViewCell"];
dictArray = [dataArray objectAtIndex:indexPath.row];
//cell.lblCatID.text = [dictArray objrct:#""];
cell.lblCatID.text = [NSString stringWithFormat:#"%#", [dictArray valueForKey:#"EventCategoryId"]];
cell.lblEventName.text = [NSString stringWithFormat:#"%#", [dictArray valueForKey:#"EventCategoryName"]];
cell.lblCreateDate.text = [NSString stringWithFormat:#"%#", [dictArray valueForKey:#"CreatedDate"]];
cell.layoutMargins = UIEdgeInsetsZero;
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if ([tableView respondsToSelector:#selector(setSeparatorInset:)])
{
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([tableView respondsToSelector:#selector(setLayoutMargins:)])
{
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:#selector(setLayoutMargins:)])
{
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 144.0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnClickedPostData:(id)sender {
NSString *tokenString = #"65d188d3f0ab52487001c331584ac819";
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
[defaultConfigObject setHTTPAdditionalHeaders:#{ #"token" : tokenString}];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:#"YOUR URL"];
NSString *paramString = #"lang=en&title=&start=&end=";
NSData *httpBody = [paramString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:60.0];
NSString *msgLength = [NSString stringWithFormat:#"%lu", (unsigned long)[httpBody length]];
[urlRequest addValue: #"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[urlRequest addValue: msgLength forHTTPHeaderField:#"Content-Length"];
[urlRequest setHTTPMethod:#"POST"];
//[urlRequest setAllHTTPHeaderFields:paramString];
[urlRequest setAllHTTPHeaderFields:#{ #"token" : tokenString}];
[urlRequest setHTTPBody:httpBody];
//[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSError *parseError = nil;
NSHTTPURLResponse* respHttp = (NSHTTPURLResponse*) response;
if (!error && respHttp.statusCode == 200) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSMutableArray *arrArray = [responseDictionary objectForKey:#"newslist"];
NSString *title = [NSString stringWithFormat:#"%#", [arrArray valueForKey:#"title"]];
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Details" message:title delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel", nil];
[alert show];
NSLog(#"%#", arrArray);
}else{
NSLog(#"%#", error);
}
}];
[dataTask resume];
}
#end
//CustomeTableviewCell With XIB
**ResponseTableViewCell.h**
#import <UIKit/UIKit.h>
#interface ResponseTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *lblCatID;
#property (weak, nonatomic) IBOutlet UILabel *lblEventName;
#property (weak, nonatomic) IBOutlet UILabel *lblCreateDate;
#end
**ResponseTableViewCell.m**
#import "ResponseTableViewCell.h"
#implementation ResponseTableViewCell
#synthesize lblCatID,lblEventName,lblCreateDate;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
// and also download and check JSON demo : https://drive.google.com/open?id=0B0rS2ZVDMVRiSmJJb1BuQklJSzA[Click to show JSON demo Hear][1]
//add custom table view cell with custom table cell and add label to display information. i used pod to setup AFNetworking.
and also download and check JSON demo : https://drive.google.com/open?id=0B0rS2ZVDMVRiSmJJb1BuQklJSzAClick to show JSON demo Hear
Can anyone suggest a way to do this.me how the things will be done.
You Can use also use sqlite for the data.
I'm working on an iOS app for my school that will allow the user to access school documents like schedules, calendars, and grade/tuition sites. I've been doing research on how to save credentials for some of the sites that require logins, and my ultimate goal is not only save credentials, but automatically log in the user to the websites when the webview is loaded as well. So far my code looks like this:
#import "PJP Webview.h"
#implementation PJP_Webview
#synthesize webView, loginView, username, password, body, urlToLink,usernameField,passwordField, firstTime, loginNeeded, callerCell;
-(void) viewDidLoad {
[super viewDidLoad];
loginView.hidden = true;
if (firstTime && loginNeeded) {
loginView.hidden = false;
[self.view bringSubviewToFront:loginView];
}
else {
loginView.hidden = true;
[self.view addSubview:self.webView];
[self.view bringSubviewToFront:webView];
if (loginNeeded) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
self.username = [userDefaults objectForKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
self.password = [userDefaults objectForKey:passwordSave];
NSURL *url = [NSURL URLWithString: urlToLink];
NSString *bodyForUse = [NSString stringWithFormat: body, username, password];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [bodyForUse dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
}
else {
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
}
}
- (IBAction)acceptInput:(id)sender {
self.username = usernameField.text;
self.password = passwordField.text;
loginView.hidden = true;
[self.view addSubview:self.webView];
[self.view bringSubviewToFront:webView];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
[userDefaults setObject:username forKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
[userDefaults setObject:password forKey:passwordSave];
[userDefaults synchronize];
NSURL *url = [NSURL URLWithString: urlToLink];
NSString *bodyForUse = [NSString stringWithFormat: body, username, password];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [bodyForUse dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
}
#end
I modeled some of my code after another stack overflow article found here Loading a webpage through UIWebView with POST parameters
An example of one of the websites I am trying to integrate is https://adphila.gradeconnect.com/front/slogin.php
So far my code has not been successful at logging me into the website and taking me to the next page, so I'm not sure if maybe the body I am using for the parameters is wrong, if my code is faulty, or some other issue is present. Also, if you have any suggestions for how to make my previous code more efficient I welcome that too!
Edit: The school also uses gmail for student emails, so is it possible to embed a uiwebview of email content as well? I know Google has their own APIs but I have never used non-Apple libraries before.
Edit 2:
#import "PJP Webview.h"
#implementation PJP_Webview
#synthesize webViewCurrent, loginView, username, password, bodyUsername, bodyPassword, urlToLink,usernameField,passwordField, firstTime, loginNeeded, callerCell, bodyButton;
-(void) viewDidLoad {
[super viewDidLoad];
[webViewCurrent setDelegate:self];
loginView.hidden = true;
if (firstTime && loginNeeded) {
loginView.hidden = false;
[self.view bringSubviewToFront:loginView];
}
else {
loginView.hidden = true;
if (loginNeeded) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
self.username = [userDefaults objectForKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
self.password = [userDefaults objectForKey:passwordSave];
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
}
else {
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
}
[self.view addSubview:self.webViewCurrent];
[self.view bringSubviewToFront:webViewCurrent];
}
}
- (IBAction)acceptInput:(id)sender {
self.username = usernameField.text;
self.password = passwordField.text;
loginView.hidden = true;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
[userDefaults setObject:username forKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
[userDefaults setObject:password forKey:passwordSave];
[userDefaults synchronize];
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
[self.view addSubview:self.webViewCurrent];
[self.view bringSubviewToFront:webViewCurrent];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
NSString *fillData = [NSString stringWithFormat:#"document.getElementById('%#').value = '%#';document.getElementById('%#').value = '%#';", bodyUsername, username, bodyPassword, password];
[webView stringByEvaluatingJavaScriptFromString:fillData];
NSString *buttonData = [NSString stringWithFormat:#"document.getElementById('%#').click()", bodyButton];
[webView stringByEvaluatingJavaScriptFromString:buttonData];
}
#end
There is a new error though, void SendDelegateMessage(NSInvocation *): delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
For the website I am using, the bodyUsername is lusername, the bodyPassword lpassword, and the bodyButton login. The page loads but is not executing the javascript.
You can do this by Autopopulating the username and password field of that webpage. First get the name of the username and password field from the webpage. right click on the webpage.
Click on inspect element and then You van easily find it.
Then write the WebView Delegate Method.
// ViewController.m
// WebDemo
//
// Created by Nilesh on 7/31/16.
// Copyright © 2016 Nilesh. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()<UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlToLink = #"https://adphila.gradeconnect.com/front/slogin.php";
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
NSString* userId = #"userName" ;//Your userName
NSString* password = #"password";// Your Password
NSString* jScriptString1 = [NSString stringWithFormat:#"document.forms[0]['lusername'].value='%#'", userId];
NSString* jScriptString2 = [NSString stringWithFormat:#"document.forms[0]['lpassword'].value='%#'", password];
[webView stringByEvaluatingJavaScriptFromString:jScriptString1];
[webView stringByEvaluatingJavaScriptFromString:jScriptString2];
NSString *jsStat = #"document.forms[0]['login'].click()";
[webView stringByEvaluatingJavaScriptFromString:jsStat];
}
#end
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];
}
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
i'm using json rss feed parsing in my rss app, i have table view loading titles , thumbnails and sending links of titles to a web view each in it's own nsmutable array, however , i'm adding a pull to refresh in my app , i followed alot of tutorials , i added the code, when i pull , i get the spinning wheel , and loads and stops, but my new feed isn't loaded, how do i make sure the parsing method gets recalled , bnot just refreshen table happens , below is my code :
#import "APPMasterViewController.h"
#import "APPDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#interface APPMasterViewController () {
NSXMLParser *parser;
NSMutableArray *feeds;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *link;
NSMutableString *thumbnail;
NSString *element;
UIRefreshControl *refreshControl;
}
#end
#implementation APPMasterViewController
- (void)awakeFromNib
{
[super awakeFromNib];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0xcf1717)];
[[UINavigationBar appearance] setBarStyle:(UIBarStyleBlackTranslucent)];
}
- (void)viewDidLoad {
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:#"http://icuore.ly/category/ipad/feed/"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refresh) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];
}
- (void)refresh {
[self.tableView reloadData];
[refreshControl endRefreshing];
NSLog(#"fetching data from the server");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: #"title"];
//cell image set
NSString *imageStr = [[feeds objectAtIndex:indexPath.row] objectForKey: #"thumbnail"];
NSString *trimmedString = [imageStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *string1=[trimmedString stringByReplacingOccurrencesOfString:#"/n" withString:#""];
NSURL *url = [NSURL URLWithString:string1];
// NSData *data = [NSData dataWithContentsOfURL:url];
// UIImage *newImage = [UIImage imageWithData:data];
//cell.imageView.image = newImage;
CALayer * roudning = [cell.imageView layer];
[roudning setMasksToBounds:YES];
[roudning setCornerRadius:30.0];
[cell.imageView setImageWithURL:url
placeholderImage:[UIImage imageNamed:#"icuore_logo.jpg"]];
return cell;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:#"item"]) {
item = [[NSMutableDictionary alloc] init];
title = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
thumbnail = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"item"]) {
[item setObject:title forKey:#"title"];
[item setObject:link forKey:#"link"];
[item setObject:thumbnail forKey:#"thumbnail"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:#"title"]) {
[title appendString:string];
} else if ([element isEqualToString:#"link"]) {
[link appendString:string];
} else if ([element isEqualToString:#"thumbnail"]) {
[thumbnail appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
//
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey: #"link"];
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *string1=[trimmedString stringByReplacingOccurrencesOfString:#"/n" withString:#""];
NSLog(#"you clicked %#", [feeds[indexPath.row] objectForKey: #"link"]);
[[segue destinationViewController] setUrl:string1];
}
//my modified passing
}
#end
i found the answer , i had to empty the table view , and recall the nsurl , reload data and end refreshing , it works perfectly now
here is the code everyone:
- (void)refresh {
[feeds removeAllObjects];
NSURL *url = [NSURL URLWithString:#"http://icuore.ly/category/ipad/feed/"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[self.tableView reloadData];
[refreshControl endRefreshing];
NSLog(#"fetching data from the server");
}
xcode newbie trying to get UISearchBar functionality working.
I am using storyboards and my tableview controller shows a list of events (title, location & start date)in the cells from the array json. When the table firsts loads all of my results are displayed ok. When I click put some text into the search bar (text that exists in the title) nothing is shown in the view controller (I.e. it filters all of the events out). Hopefully this makes a bit more sense.
My header file is
#import <UIKit/UIKit.h>
#define kGETUrl #"http://www.max-momentus.com/fetchevents.php5"
#interface MMAllEventsViewController : UITableViewController <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *json;
NSMutableArray *jsonFiltered;
BOOL isFiltered;
}
#property (weak, nonatomic) IBOutlet UISearchBar *eventSearchBar;
#property (strong, nonatomic) IBOutlet UITableView *eventTableView;
#property (strong, nonatomic) NSString *categorySelected;
#property (strong, nonatomic) NSString *displayDate;
#end
My implementation file is
#import "MMAllEventsViewController.h"
#import "MMEventDetailViewController.h"
#import "MMCategoryViewController.h"
#import "MMEventCell.h"
#interface MMAllEventsViewController ()
#end
#implementation MMAllEventsViewController
#synthesize categorySelected, displayDate;
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"EventDetailSegue"]) {
// Segue code
}
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void) getData:(NSData *) data {
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
[self.tableView reloadData];
}
-(void) start {
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.eventSearchBar.delegate = self;
self.eventTableView.delegate = self;
self.eventTableView.dataSource = self;
[self start];
}
-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length == 0) {
isFiltered = NO;
}
else {
isFiltered = YES;
for (int i = 0; i < [json count]; i++)
{
NSMutableDictionary *temp = (NSMutableDictionary*) [json objectAtIndex:i];
NSString *name = [NSString stringWithFormat:#"%#", [temp valueForKey:#"Title"]];
NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
[jsonFiltered addObject:name];
}
i++;
}
}
[self.eventTableView reloadData];
}
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.eventTableView resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (isFiltered) {
return [jsonFiltered count];
}
return [json count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"EventCell";
MMEventCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[MMEventCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (!isFiltered) {
NSDictionary *info = [json objectAtIndex:indexPath.row];
cell.eventTitleLabel.text = [info objectForKey:#"Title"];
cell.locationLabel.text = [info objectForKey:#"Location"];
NSString *startDate = [info objectForKey:#"StartDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *sdate = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:#"EEE, d MMM yy"];
NSString *convertedStartDate = [dateFormatter stringFromDate:sdate];
NSString *endDate = [info objectForKey:#"EndDate"];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *edate = [dateFormatter dateFromString:endDate];
[dateFormatter setDateFormat:#"EEE, d MMM yy"];
NSString *convertedEndDate = [dateFormatter stringFromDate:edate];
if([sdate isEqualToDate:edate]) {
displayDate = convertedStartDate;
} else {
displayDate = [convertedStartDate stringByAppendingString:#" to "];
displayDate = [displayDate stringByAppendingString:convertedEndDate];
}
cell.datesLabel.text = displayDate;
}
else {
NSDictionary *info = [jsonFiltered objectAtIndex:indexPath.row];
cell.eventTitleLabel.text = [info objectForKey:#"Title"];
cell.locationLabel.text = [info objectForKey:#"Location"];
NSString *startDate = [info objectForKey:#"StartDate"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *sdate = [dateFormatter dateFromString:startDate];
[dateFormatter setDateFormat:#"EEE, d MMM yy"];
NSString *convertedStartDate = [dateFormatter stringFromDate:sdate];
NSString *endDate = [info objectForKey:#"EndDate"];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *edate = [dateFormatter dateFromString:endDate];
[dateFormatter setDateFormat:#"EEE, d MMM yy"];
NSString *convertedEndDate = [dateFormatter stringFromDate:edate];
if([sdate isEqualToDate:edate]) {
displayDate = convertedStartDate;
} else {
displayDate = [convertedStartDate stringByAppendingString:#" to "];
displayDate = [displayDate stringByAppendingString:convertedEndDate];
}
cell.datesLabel.text = displayDate;
}
return cell;
}
#end
Have you made sure you have the delegates: in your view controllers .h file? Also, make sure you've selected the "UISearchBar and UISearchDisplay Controller" as your search bar object from the storyboard (assuming you're using storyboards). Make sure you've linked your SearchBar to have your view controller be its delegate, and it's connected to an IBOutlet object in your code (again, assuming your using storyboards:
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
Finally, make that you have declared a UISearchDisplayController variable, linked it to your UITableView and then make sure when loading the results from a search, to check in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (NSInteger)numberOfRowsInTableView:(UITableView *)tableView
if:
if(tableView == searchDisplay):
{
...load results.
}
else
{
...load normal view
}