Mega Bundle SALE is ON! Get ALL of our amazing iOS app codebases at 80% OFF discount 🔥

While working on a brand new iOS app template for WordPress sites, we encountered the problem of writing an RSS Feed Parser in Swift. It turns out there’s a straightforward approach to parsing a WordPress RSS Feed in Swift 3, so we thought to share this on our blog, hoping this will come in handy for a Swift developer working on a similar feature.

Swift code for an RSS Feed Parser

If you’re in a hurry and you know what you’re doing, just follow the next steps. If you want a more detailed explanation for what each piece of Swift code is doing, read on. We also wrote down a few hints about how you can design this iOS code better so that your app will keep its high-quality architecture unstained after integrating the RSS Feed Parser into its codebase.

  1. Install Alamofire and AlamofireRSSParser CocoaPods. Follow the links for more instructions if you’re not familiar with CocoaPods.
  2. Use the code below to create the request to your WordPress RSS Feed (or any RSS feed for that matter):
import Alamofire
import AlamofireRSSParser

public enum NetworkResponseStatus {
    case success
    case error(string: String?)

public class RSSParser {
    public static func getRSSFeedResponse(path: String, completionHandler: @escaping (_ response: RSSFeed?,_ status: NetworkResponseStatus) -> Void) {
        Alamofire.request(path).responseRSS() { response in
            if let rssFeedXML = response.result.value {
                // Successful response - process the feed in your completion handler
                completionHandler(rssFeedXML, .success)
            } else {
                // There was an error, so feel free to handle it in your completion handler
                completionHandler(nil, .error(string: response.result.error?.localizedDescription))

This is pretty much all you need in order to parse your XML RSS feed. This code automatically takes care of the request as well as processing the result data and mapping it out to the RSSFeed model. In your completion handler, you can simply use the RSSFeed object, by accessing its properties and members. Here’s an example of how you can call and use the results returned by the method above:

RSSParser.getRSSFeedResponse(path: "Insert your RSS Feed URL here") { (rssFeed: RSSFeed?, status: NetworkResponseStatus) in
            print(rssFeed) // it will be nil if status == .error

That’s all. Read on if you’re looking for a more detailed explanation of how this RSS Feed Parser is built.

Detailed explanation of the RSS Feed Parser

First of all, Alamofire is an HTTP networking framework for Swift – it basically provides an API whose set of methods take care of all the HTTP networking requests that you might have in your app. It’s simple and efficient. If you’re not familiar with Alamofire, we strongly recommend you to use it as the networking layer of your entire app. Classic requests over HTTP, that return JSON/XML are pieces of cake when using Alamofire.

AlamofireRSSParser is a small framework built on top of Alamofire and it provides a simple way to fetch XML RSS Feeds over HTTP networking requests. It takes care of making the request as well as parsing the XML response. As far as your application is concerned, you only need to initialize the request, and AlamofireRSSParser will send you back an object whose type is an RSSFeed (also defined in the framework), and whose properties can be trivially accessed. Take a look at the RSSFeed’s definition here.

More architectural improvements

If you put a lot of emphasis on your app’s architecture, you might want to refactor the code a little. First of all, it’s usually a good practice to avoid static methods, since they can make the testing a nightmare, so let’s just remove the “static” keyword above. Now, you can use the method independently across different instances of the RSSParser class, which will facilitate injecting them as dependencies for other classes. This is super helpful since it’s a whole lot easier to mock the network requests for your tests now.

RSSParser is clearly a controller (if you think in terms of the MVC pattern), so be careful how it interacts with the other components of your app. As we already said, RSSFeed is a model (defined in AlamofireRSSParser CocoaPod), so use it appropriately. Since it’s pretty generic (applicable to all RSS feeds), you might want to create a subclass or an extension, to adapt it to your app’s needs and context.

The Alamofire HTTP request is done asynchronously (on a different thread), so everything you plan on doing with its response should get executed after the completionHandler closure is executed. While this is a great optimization, an inexperienced iOS developer might run into painful issues if she’s not aware of this fact.

Please let us know in the comments whether you have any other suggestions of how we could improve this RSS Feed Parser even further 🙂

1 Comment

Desmond · May 29, 2019 at 12:12 pm

Hello i have an question i followed the tutorial but im getting an error the error is

:-1: Cycle in dependencies between targets ‘Alamofire’ and ‘AlamofireRSSParser’; building could produce unreliable results.
Cycle path: Alamofire → AlamofireRSSParser → Alamofire
Cycle details:
→ Target ‘Alamofire’ has link command with output ‘/Users/hjmediagroep/Library/Developer/Xcode/DerivedData/Testapp-chyknmfmgxmeiaethcrabbfhpafv/Build/Products/Debug-iphonesimulator/Alamofire/Alamofire.framework/Alamofire’
â—‹ Target ‘Alamofire’ has compile command for Swift source files
â—‹ Target ‘Alamofire’ has target dependency on Target ‘AlamofireRSSParser’
→ Target ‘AlamofireRSSParser’ has target dependency on Target ‘Alamofire’

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping Cart