NSData needs to be updated - iphone

I am parsing HTML using hpple. so now I want my text to be updated as the user touches the next button. my code looks something like this
NSURL *ur = [NSURL URLWithString:[NSString stringWithFormat:#"%#",url.text]];
NSData *htmlData = [NSData dataWithContentsOfURL: ur];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements = [xpathParser search:#"//table[1]/tr[2]/td[2]/a/text()"]; // get the page title
TFHppleElement *element = [elements objectAtIndex:0];
NSString *h3Tag = [element content];
mi.text = h3Tag;
NSLog(#"%#",h3Tag);
[xpathParser release];
so I am kind of a new iPhone application development and fairly new to programming. So over here I think NSData needs to be updated.and yes when the user touches the next button the url also changes. so any help on that would be appreciated
thanks
Tushar

NSMutableString *tempString = [[NSMutableString alloc] initWithFormat:#"%#",url.text];
NSURL *ur =[[NSURL alloc] initWithString:tempString];
[tempString release];
NSData *htmlData = [[NSData alloc]initWithContentsOfURL:ur];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
[ur release];
[htmlData release];
NSArray *elements = [xpathParser search:#"//table[1]/tr[2]/td[2]/a/text()"]; // get the page title
TFHppleElement *element = [elements objectAtIndex:0];
NSString *h3Tag = [element content];
mi.text = h3Tag;
NSLog(#"%#",h3Tag);
[xpathParser release];
try this code.
Thanks.

Related

Extract YouTube Video Link Using YouTube API

I have developed an app that loads a user's videos and when they click on the cell in the tableview it goes to another viewcontroller that holds a bunch of different information.
I'm trying to extract the actual YouTube link from a specific video. I print out the links I get and they come in this form.
https://www.youtube.com/v/Xf5pXlZJr1U?version=3&f=user_uploads&app=youtube_gdata
But I want it to be in this form.
http://www.youtube.com/watch?v=Xf5pXlZJr1U
Reason being that I want people to share the link via social media, but it comes out weird when it's in the first format.
Is there any way to easily convert the links?
SAMPLE CODE AND ANSWER EDIT
GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry2 title] stringValue];
NSArray *contents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:contents withValue:#"application/x-shockwave-flash" forKeyPath:#"type"];
NSString *tempURL = [flashContent URLString];
NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaThumbnails];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
UIImage *image = [UIImage imageWithData:data];
GDataYouTubeMediaGroup *mediaGroup = [(GDataEntryYouTubeVideo *)entry2 mediaGroup];
GDataMediaDescription *desc2 = [mediaGroup mediaDescription];
NSString *mystring = [desc2 stringValue];
NSArray *listItems = [tempURL componentsSeparatedByString:#"/"];
NSString *NewURL = #"";
NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:0]];
NewURL = [NewURL stringByAppendingString:#"//"];
NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:2]];
NewURL = [NewURL stringByAppendingString:#"/"];
NewURL = [NewURL stringByAppendingString:#"watch"];
NewURL = [NewURL stringByAppendingString:#"?v="];
NSArray *listItems2 = [[listItems objectAtIndex:4] componentsSeparatedByString:#"?"];
NewURL = [NewURL stringByAppendingString:[listItems2 objectAtIndex:0]];
If you take a look at NSString docs then you will find number of useful methods for yourself to use, e.g componentsSeparatedByString splitting the string in two different chunks depending on some particular character & stringByAppendingString for appending string to an existing string. It is not difficult to use these methods, just use according to your needs.

Parsing HTML <Tag> into ios

i am Parsing HTML Tag into iOS using Hpple. i am able to parse the data where the HTML Tag is
<div id="NewsPageSubTitle">
<p><**span** hi how are you>
Using ios code:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p/span ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but in few case i don't have span, imean the string in html is accessed by tag "p" directly like:
<div id="NewsPageSubTitle">
<p>< hi how are you>
Here I am using ios code as:
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageArticle']/p ";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
but here i am getting a blank data in response.
can any one let me know how to solve the problem?
Since sometimes the para tag has span and sometimes it does not, I would suggest trying to handle that by looping over the children
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
TFHpple * tutorialsParser = [[TFHpple alloc] initWithHTMLData:data];
NSString *tutorialsXpathQueryString = #"//div[#id='NewsPageSubTitle']";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
for (TFHppleElement * element in tutorialsNodes) {
NSLog(#"%#", element);
NSLog(#"%#", [element tagName]);
NSLog(#"%#", [element attributes]);
NSLog(#"%#", [element children]);
for (TFHppleElement *childElement in [element children]) {
NSLog(#"%#", childElement);
}
}
Check with this: https://github.com/mwaterfall/MWFeedParser
This will provide the HTML Parser for iphone sdk.
More help on:
this blog and here.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"html" inDirectory:#"New Folder 2"];
NSData * data = [NSData dataWithContentsOfFile:filePath];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSString *htmlString = [[NSString alloc] initWithData:[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
TFHpple * Parser = [[TFHpple alloc] initWithHTMLData:data];
NSString *query = #"//p";
NSArray *nodes = [Parser searchWithXPathQuery:query];
for (TFHppleElement *item in nodes)
{
NSLog(#"Title : %#", item.content);
NSLog(#"URL : %#", [item.attributes valueForKey:#"href"]);
}

Attach a pdf file in-app-mail

I tried to attach my pdf to an in-app-mail. The in-app-mail displays an icon with the pdf but it doesn't send it. I don't know why...
Here's the code:
- (void)openInEmail {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
viewController.mailComposeDelegate = self;
[viewController setSubject:#"Stundenplan 1A"];
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/Stundenplan_1A.pdf", docDirectory];
NSMutableData *data=[NSMutableData dataWithContentsOfFile:filePath];
[viewController addAttachmentData:data mimeType:#"text/pdf" fileName:#"Stundenplan_1A.pdf"];
[self presentModalViewController:viewController animated:YES]; }
}
Any ideas?
Have you tried it with?
NSString *filePath = [documentsDirectory stringByAppendingFileComponent:#"%#/Stundenplan_1A.pdf"];
instead of
NSString *filePath = [NSString stringWithFormat:#"%#/Stundenplan_1A.pdf", docDirectory];
And instead of NSMutableData you could tried it with NSData
NSData *data = [NSData dataWithContentsOfFile:file];
Change the mime type like this.
NSURL *url = [NSURL URLWithString:pdfURL];
NSData *pdfData = [NSData dataWithContentsOfURL:url];
[mailComposeView addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"CheckList.pdf"];

Problem with getting images from server

I'm trying to fetch images from url. Can someone point where i get wrong here is my code?
NSString *filesContent = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.projects-demo.com/iphone/xml/Menu.xml"] ];
DDXMLDocument *ddDoc = [[DDXMLDocument alloc] initWithXMLString:filesContent options:0 error:nil];
DDXMLElement *ddMenu = [ddDoc rootElement];
NSArray *ddChildren = [ddMenu children];
for (int j = 0 ;j < [ddChildren count]; j++) {
DDXMLElement *image1st = [[ddMenu elementsForName:[NSString stringWithFormat:#"cookingType%d",j+1]] objectAtIndex:0];
for (DDXMLNode *n in [image1st children]) {
// if ([[n name] isEqualToString: #"cookingType"]) {
MenuModel *model = [[MenuModel alloc] init];
NSLog(#"image of cooking........%#",[n stringValue]);
model.imgsrc = [n stringValue];
[listofimages addObject:model];
//ss
//======
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:model.imgsrc]];
NSLog(#"printing my data ....",mydata);
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
I tried to print nsDAta but it get nothing.
Just an observation, your NSLog for the variable myData, misses %#, not sure if this is just a copy and paste error or something that the HTML doesn't show.
Also try and Log [myData length] there might be a problem with the download.
Last, I would recommend that you do all your URL calls asynchronously.
It would look somewhat like this
`
-(void) loadingThumnailFormURL:(NSString *) thumbnailURL {
[imageData release];
imageData = [[NSMutableData alloc] init];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:thumbnailURL]];
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
[urlRequest release];
[urlConnection start];
}`
Needless to say you have to implement the delegate methods for NSURLConnection and capture the data.

memory leaking iPhone sdk?

I am trying out this application found over here: http://blog.objectgraph.com/index.php/2010/02/24/parsing-html-iphone-development/
this application is contantly crashing.. i think there is a mem. leaking could any one help me fix this
thanks
okay here is yar solution...
you released htmlData at the end. dont release it. cause you didn't alloc that...
NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString: #"http://www.objectgraph.com /contact.html"]];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements = [xpathParser search:#"//h3"]; // get the page title
TFHppleElement *element = [elements objectAtIndex:0];
NSString *h3Tag = [element content];
NSLog(#"Html tags :%#",h3Tag);
mLabel.text = h3Tag;
[xpathParser release];