memory leaking iPhone sdk? - iphone

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];

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"]);
}

NSData needs to be updated

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.

Continuous conversion of text to speech from json data

I am designing an app that needs text to speech. I am using the library that is posted here to convert text to speech. I have to retrieve text from Json url, and pass the values to text to speech. I am able to retrieve Json data and convert it to text to speech for the ObjectatIndex 0 using the following code...
SBJSON *json = [[SBJSON alloc]init];
fliteEngine = [[FliteTTS alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.sampleurl.txt"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *jsonstring = [[NSString alloc]initWithData:response encoding:NSUTF8StringEncoding];
NSArray *asanasList = [json objectWithString:jsonstring error:nil];
NSArray *asanas =[asanasList objectForKey:#"yogagurubackpain"];
for(NSDictionary *test in asanas)
{
UrlValues *myasanas = [[UrlValues alloc]init];
myasanas.asanatitle = [test objectForKey:#"asanatitle"];
myasanas.asanatranscript = [test objectForKey:#"asanatranscript"];
myasanas.asanapicture = [test objectForKey:#"asanapicture"];
[data.yoga addObject:myasanas];
[myasanas release];
}
UrlValues *asana=[[data yoga]objectAtIndex:0];
self.AsanaName.text = [asana asanatitle];
self.AsanaTranscript.text = [asana asanatranscript];
NSString *imageUrl = [asana asanapicture];
NSString* mapUrl = [imageUrl stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapUrl]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
self.AsanaImage.image = image;
NSString *speak = self.AsanaTranscript.text;
[fliteEngine setVoice:#"cmu_us_rms"];
[fliteEngine speakText:speak];
[fliteEngine setPitch:100.0 variance:11.0 speed:0.4];
[imageData release];
[image release];
[jsonstring release];
[json release];
Now my problem is how can i go to the next object automatically after completion of the playing of first one. The process must continue for all the objects. The corresponding images etc must load on the page after the text to speech is done for the first one... Plz help me...
Looking at the source of the linked library, it looks like you'll have to hack a method into FliteTTS.m. If you look at the source, it's using an AVAudioPlayer to play back a generated WAV file. It's also setting itself as the audio player's delegate. If you implement the audioPlayerDidFinishPlaying:successfully: delegate method and play the next chunk when it's called, you should be able to have a semi-continuous text-to-speech stream.

iPhone Memory Management

I'm about to make a fool out of myself, but I've noticed many friendly and patient people around here, so I just give it a try:
I'm developing an iPhone app, that contains a database of car reviews. I want the user to be able to share the reviews by email. So, when he/she finds an interesting car in the app, he/she would hit a button, and the app composes an email through the iPhone's Mail.app.
Now. I'm a newbie, and I have to admit that I'm not too familiar with memory management on the iPhone. The code that I wrote, this specific mail method, exits the application with the scary "Program received signal: “EXC_BAD_ACCESS”" message. A bit of Googling suggest that this is the result of bad memory management.
With my little understanding of this matter, I started explicitly initializing and afterwards releasing all temporary variables, like a madman. Nevertheless, the “EXC_BAD_ACCESS” keeps on showing up.
Interesting point here: as soon as I kill my app, the constructed URL still triggers Mail.app, and happily creates the email for me.
Please consider the following sample code, and shoot me.
- (IBAction) sendCartoFriend
{
CarAppDelegate *appDelegate = (CarAppDelegate *)[[UIApplication sharedApplication] delegate];
//Read the html template
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *emailFile = [resourcePath stringByAppendingPathComponent:#"MailDummy.html"];
NSMutableString *eMailRaw = [[[NSMutableString alloc] initWithContentsOfFile:emailFile]autorelease];
//set the variables
NSString *carNamePlaceholder = [[NSString alloc] initWithString:#"CarTitle"];
NSString *carName = [[NSString alloc] initWithString:car.shortname];
[eMailRaw replaceOccurrencesOfString:carNamePlaceholder withString:carName options:NSCaseInsensitiveSearch range:NSMakeRange(0, [eMailRaw length])];
[carNamePlaceholder release];
[carName release];
NSString *carReviewPlaceholder = [[NSString alloc] initWithString:#"CarReview"];
NSString *carReview = [[NSString alloc] initWithString:car.review];
[eMailRaw replaceOccurrencesOfString:carReviewPlaceholder withString:carReview options:NSCaseInsensitiveSearch range:NSMakeRange(0, [eMailRaw length])];
[carReviewPlaceholder release];
[carReview release];
//there are 5 more of these find/replace actions. the "CarReview" though is the biggest. It might contain several hundred of characters.
//compose the message
NSString *eMailSubject = #"Nice little car!";
NSString *encodedSubject = [[NSString alloc] initWithString:[eMailSubject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *eMailBody = eMailRaw;
NSLog(eMailBody);
NSString *encodedBody = [[NSString alloc] initWithString:[eMailBody stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *urlString = [[NSString alloc] initWithString:[NSString stringWithFormat:#"mailto:?subject=%#&body=%#", encodedSubject, encodedBody]];
NSURL *url = [[NSURL alloc] initWithString:urlString];
[urlString release];
[encodedBody release];
[encodedSubject release];
[eMailRaw release];
[[UIApplication sharedApplication] openURL:url];
[url release];
}
Hmm.. at first glance:
you're releasing eMailRow even though it's set to autorelease.
Could this be the problem?