Twitter: cfjedimaster


Address: Lafayette, LA, USA

PhoneGap RSS Reader - Part 5

08-17-2012 5,027 views Mobile, JavaScript 43 Comments

For a while now I've been talking about, and upgrading, a basic PhoneGap-based RSS reader application. (See the related entries at the bottom for the earlier iterations.) I was thinking about the application this morning. One of the things I noticed about the application is that when you clicked to read a full article, the application launched the native web browser. In IOS, there wasn't a nice way to get back to the application. On Android, you could use back, but overall, the transition was a bit jarring. I thought this would be a perfect opportunity to try the ChildBrowser plugin.

The ChildBrowser plugin creates a "pop up window" that acts like an embedded browser for your application. You can load it with a URL and add event handlers for things like location changing or the browser being closed. The end result is that the user isn't really "leaving" the application when they view a web page.

The ChildBrowser plugin is available for iOS and Android. (You can also use it with Blackberry and Windows Phone.)

The API is really simple to use. Since I'm not concerned with any of the events thrown by the ChildBrowser and just need it to go some place, I added a class to my "Read Entry on Site" link and then added this touch handler:

I've talked about PhoneGap plugins before so I won't repeat the story behind how you use them, but the basic idea is that you get the native code bits installed with your editor and then copy over one JavaScript file that provides the connection to the native code. For the most part, it's really pretty simple.

That being said - I was in luck. Turns out that PhoneGap Build supports a few plugins - and one of them is the ChildBrowser. I opened up my config.xml file and added one line:

The last thing I needed to do was add a call to the childbrowser.js file in my index.html. Do not forget that even though you include a script tag that points to the file, you don't actually include the file when creating your zip. Another thing to note. My application - previously - had not actually done anything device related. Yeah, that makes the application kind of dumb I guess, but because I wasn't accessing the camera or doing anything special, I never actually included cordova.js. PhoneGap Build will not include a plugin JavaScript file unless you also have a call to cordova.js. I'm assuming some basic regex is being done on that back end and the first script tag is required before any plugins are checked for. Here's what I added to the head block of my index.html file:

And that's it! I uploaded my code and within about two minutes I had my iOS and Android versions up and running. I've included a full zip of the code as an attachment to this blog. You can download individual binaries via the embed below. Note that the iOS version will not work for you - sorry - I'm not adding to this to the AppStore.

Download attached file

Related Blog Entries

43 Comments

  • Sarbjit Singh #
    Commented on 08-29-2012 at 8:17 AM
    Is there a way to store that "read" state for the feed items. I want to give feedback to the user about new items in the feed. It will be great if we could notify the user when there are new items in the feed.
  • Commented on 08-29-2012 at 9:13 AM
    Certainly. All RSS items have a GUID (unique id). You could store those in WebSQL or LocalStorage and then just check. My gut says WebSQL would be a bit better for this - but LocalStorage would be easier.
  • sam #
    Commented on 10-08-2012 at 9:05 PM
    I got issues with Child Browser at the moment with an rss app I'm working on. If the app is in a landscape orientation and upon closing the child browser the parent page (app), is set to portrait mode and loses some height, so can't navigate out.. I can hook into the close of event of the child browser and I am trying to programmatically set the viewpoint, but not finding any helpful code..
  • Michael #
    Commented on 10-09-2012 at 12:16 PM
    Would you be willing to share your Xcode project version of this?
  • Commented on 10-09-2012 at 12:38 PM
    I didn't use XCode - I used PG Build. You can take my code and add it to an XCode/PG project locally though.
  • Michael #
    Commented on 10-09-2012 at 1:09 PM
    I guess a follow up question would be have you used PhoneGap Build or otherwise to successfully deploy an app to iTunes? This RSS reader or even another one. I have had numerous 10.6 rejections and it is starting to seem like Apple is clamping down on HTML 5 non-native apps. Just curious about other developer experiences.
  • Commented on 10-09-2012 at 1:10 PM
    I've not yet pushed to the Apple store, but I'd strongly urge you to check out this article by my coworker Andy:

    http://www.tricedesigns.com/2012/10/03/phonegap-ap...
  • Commented on 10-25-2012 at 11:14 AM
    I got a question for you on the Childbrowser that maybe you will know. This doesn't have anything to do with the rss reader, but I am making two apps right now. The childbrowser in one app works fine. I just started another app using the same childbrowser code. In this app, for some reason the Toolbar showing the back, forward, and exit does not pop up. Any idea why this is? I used your code here $(window).on("touchstart", ".fullLink", function(e) {
       e.preventDefault();
       window.plugins.childBrowser.showWebPage($(this).attr("href"));
    });
    and assigned a class of fullLink to the link.
  • Commented on 10-26-2012 at 7:54 AM
    Sorry, I don't know. Maybe try the Google Group? (And ping back if you figure it out please.)
  • Nitin Mahind #
    Commented on 12-24-2012 at 1:54 AM
    How can i get contents of <description> tag.

    actual I made some changes with javascript

    I am able to get data from <title> <author> <link> tags and put it into SQLlite database.

    but unable to get data from <description>. It gives me undefined.

    thanks in advance.....
  • Commented on 12-24-2012 at 10:23 AM
    Please share the RSS URL you are using.
  • Nitin Mahind #
    Commented on 12-27-2012 at 4:14 AM
    http://feedproxy.google.com/RaymondCamdensColdfusi...
  • Commented on 12-27-2012 at 11:32 AM
    From what I see in the Google Feed API, the description field is parsed into the content field in the entry object. Please see the Google Feed API docs for details.
  • Nitin Mahind #
    Commented on 01-02-2013 at 7:15 AM
    Should we use same script for my rss feed...?
    can we avoid use of feed burner...?
    because the data I have to parse is to confidential.
  • Commented on 01-02-2013 at 8:55 AM
    Please see my earlier posts where I parsed the feed manually.
  • Commented on 02-25-2013 at 4:04 AM
    Switched to this version of your reader... but how about to get the enclosure tags for the images (url) and what to do if there isn't an image in every entry... Thanks
  • Commented on 02-25-2013 at 7:12 AM
    Two things:

    1) Note that the latest version of PhoneGap supports ChildBrowser w/o using a plugin. Check the InAppBrowser spec.

    2) Pascal, if Google's Feed parser doesn't return it, then you need to switch back to parsing the XML yourself, as I did in earlier entries. To see if Google returned it, simply use the console.dir() command on the results to look at it.
  • Commented on 03-10-2013 at 11:51 PM
    I change the rss link into :
    //RSS url
    var RSS = "http://serambi.blankonlinux.or.id/rss20.xml";;

    But It's doesnt work.

    What I must to do?
  • Commented on 03-11-2013 at 6:24 AM
    You must tell me _how_ it isn't working. Use Chrome Dev Tools and spend some time diagnosing where it falls down.
  • Commented on 03-12-2013 at 12:34 AM
    Ah.. now it's working, I dunno why. :)
    Anyway, can I modified this and put it on my github?
  • Commented on 03-12-2013 at 6:09 AM
    I posted the code to share, so yes, you can use. If it helps your business, consider visiting my wishlist. ;)
  • Commented on 03-23-2013 at 12:35 PM
    I am thankful for these blog posts. I have a question. Would there be a way to enable childbrowser to open links within the rss post itself on the app?
  • Commented on 03-23-2013 at 1:53 PM
    Absolutely. jQuery lets you listen for events - so you could listen for all clicks within the 'post display' region. Then open them with InAppBrowser.

    Oh btw - don't forget PhoneGap now supports ChildBrowser natively, w/o a plugin. It is called InAppBrowser.
  • Commented on 03-25-2013 at 10:04 AM
    I still do not understand Inappbrowser am familiar with childbrowser and am not familiar of where the post display region is located and how to listen for events with jquery.
  • Commented on 03-25-2013 at 10:11 AM
    Well, you have a few things here you are asking about.

    InAppBrowser: Not familiar with it? Check the docs. It should clear it up.

    Post Display Region: Please read this tutorial closely, and the earlier parts, and you can see where an individual entry is displayed to the user.

    jQuery Events: Then I'd recommend learning jQuery. :) Yes I can write this for you pretty quickly, but honestly, you should be able to get up to speed on jQuery in an hour or so, and it would be well worth your time to take a look at it.
  • Commented on 03-27-2013 at 8:11 AM
    Thank you Raymond! I'm gonna look into it I'm self taught on everything but have to piece together codes to get them to work. I attempted to add the touch handler to the page display and could not get any results to work. I'm still learning in app browser and the api. I'll look into it further today. I'll post any codes if that helps, just need to attempt to get the browser to work with individual links in the rss feed posts.
  • Commented on 03-27-2013 at 3:36 PM
    I haven't tested this, but I believe this is what would work. So this code handles recognizing clicks on items that have the fullLink class.

    $(window).on("touchstart", ".fullLink", function(e) {
       e.preventDefault();
       window.plugins.childBrowser.showWebPage($(this).attr("href"));
    });

    You want the exact same code, but links within where the entry text is text. Entry text is stored in a div with the ID entryText. I think, stress think, this will do:

    $(window).on("touchstart", "div#contentPage a", function(e) {
    // the rest is the same
  • Commented on 04-06-2013 at 3:24 AM
    I looked at it and followed as stated above and was unable to get anything to work.
  • Commented on 04-06-2013 at 6:12 AM
    What exactly failed though? What do you see and in what environment?
  • Commented on 04-09-2013 at 1:29 PM
    I have the feed working perfectly. I just am struggling with the individual feed links using the in app browser. I am very novice to everything I can provide an example via paste bin and show you what I am doing and where I am inserting everything if that works. I also read a few pages back about description and images but am still learning that as well. Thank you for the quick response.
  • Commented on 04-09-2013 at 1:40 PM
    I'm using rsspect.com to host my feed content so I'm not sure if I need to go there and add a div class to each link on there its in html.
  • Commented on 04-10-2013 at 7:35 AM
    So when you say you are struggling, can you describe more what your issue is? I assume you get the list of entries. I assume you click to view. I assume when you click to view the full entry it is failing to use the child browser? If so, does it load in the app instead?
  • Commented on 04-11-2013 at 1:58 AM
    I have everything working, just cannot get childbrowser to open up any links within the entries. Here is my code: http://pastebin.com/awjz5D7n
  • Commented on 04-11-2013 at 8:01 AM
    I don't see where you tried what I suggested:

    $(window).on("touchstart", "div#contentPage a", function(e) {
  • Commented on 04-12-2013 at 12:37 AM
    I am not sure where to insert that at, could use some help there, everything else is working great.
  • Commented on 04-12-2013 at 6:26 AM
    It is just more JavaScript code. You would put it with the rest.
  • Panayiotis Georgiou #
    Commented on 04-26-2013 at 6:08 AM
    Good tutorial Raymond but I have a problem.
    I try to show the pubDate inside each post with
    $('#postdate' + i).text(entry.pubDate);
    but it doesn't pick the date from the feed

    any ideas ?
  • Commented on 04-26-2013 at 6:12 AM
    If you console.dir(entry), do you see it?
  • Commented on 05-01-2013 at 3:08 AM
    I figure out that your tutorial can build successfully on build.phonegap.com, but, I can't handle it when build on local. Any clue?
  • Commented on 05-01-2013 at 6:12 AM
    Nope. I'd need to know what goes wrong when you build locally.
  • Commented on 05-01-2013 at 10:52 PM
    childbrowser not working, my be something wrong on my build.xml (on local). Can I use your config.xml as build.xml ?
  • Commented on 05-02-2013 at 6:29 AM
    You should use InAppBrowser instead. It is the same thing (pretty much) but built *into* PhoneGap now.
  • Commented on 05-05-2013 at 10:50 PM
    Finnaly, I'm using InAppBrowser and succesed. Thanks.

    :)

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead.

Leave this field empty