josh brown
josh brown

Reputation: 33

NSXMLParser is not being called in Swift 2.0 (Could not open data stream)

I am trying to get the NSXMLParser to work in Swift 2.0. I had a working parser in Swift 1. I migrated to Swift 2.0 and now the parser does not kick off properly.

init(urlToUse: String)
{
    self.eventFeedURL = urlToUse
    super.init()
}

func beginParsing() {
    // this should kick off the parsing functionality

    // lastDate starts off as the current date/time and will be used later to update available articles
    var lastDate = NSDate()

    let feedURL:NSURL = NSURL(string: self.eventFeedURL)!

    let feedParser:NSXMLParser? = NSXMLParser(contentsOfURL: feedURL)

    if let actualFeedParser = feedParser {

        // Download feed and parse out
        actualFeedParser.delegate = self

        actualFeedParser.parse()

    }

}

func getAvailableEvents() -> [Event] {
    return self.availableEvents
}

func updateAvailableEvents(newEvents:[Event]) {
    // add the new day's events to the avaiable events
    self.availableEvents += newEvents

    // notify that new events are available from this parser
    notificationCenter.postNotificationName("CityOfBoston_EventsUpdated", object: self)

}


// XML parsing functions specific to this feed
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    // add any other XML tags you care about to the if statement below (via || (or) logic)

    if elementName == "item" || elementName == "title" ||  elementName == "link" || elementName == "description" || elementName == "category" {

        self.currentElement = elementName
        self.attributes = attributeDict

    }

    if elementName == "item" {

        // Start a new event
        self.currentlyConstructingEvent = Event()

    }
}

func parser(parser: NSXMLParser, foundCharacters string: String) {
    // add any other XML tags you care about to the if statement below (via || (or) logic)
    if self.currentElement == "item" ||
        self.currentElement == "title" ||
        self.currentElement == "description" ||
        self.currentElement == "link" ||
        self.currentElement == "category"{

            self.foundCharacters += string

    }
}

func parserDidEndDocument(parser: NSXMLParser) {
    notificationCenter.postNotificationName("CityOfBoston_ParserFinished", object: self)
}

func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {

    if elementName == "title" {
        // Parsing of the title element is complete, save the data

        // update with appropriate title formatting
        foundCharacters = foundCharacters.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        foundCharacters = foundCharacters.stringByReplacingOccurrencesOfString("'", withString: "'", options: [], range: nil)
        let title:String = foundCharacters.stringByReplacingOccurrencesOfString(""", withString: "\"", options: [], range: nil)
        self.currentlyConstructingEvent.setEventTitle(title)

Upvotes: 0

Views: 539

Answers (1)

problem for this issue is Transport security in a latest version here.

So you should NSAppTransportSecurity dictionary to your info.plist. Then add NSAllowsArbitraryLoads key to that dictionary and set the boolean value to true.

<key>NSAppTransportSecurity</key>  
     <dict>  
          <key>NSAllowsArbitraryLoads</key><true/>  
     </dict>  

Upvotes: 1

Related Questions