Objective-C RSS Reader - XML reading error - objective-c

I am new to obj-C and i'm trying to make an easy RSS Reader. It works with an external RSS file but not when i load the same file in my server based on AWS services.
This is the snippet code:
[super viewDidLoad];
feeds = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:#"http://images.apple.com/main/rss/hotnews/hotnews.rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
If i read that rss via web, i see this:
When I upload that file on my server, i see this:
I tried to avoid manual upload via Filezilla but using wget command and i also tried ascii mode transfer, but still not work. RSS Reader show an empty list.

It is the problem with URL. I had the crash with the same code with error
Unable to read data
I replaced code with
feedURL = NSURL(string:"http://images.apple.com/main/rss/hotnews/hotnews.rss#sthash.fuVEonEt.dpuf")!
parser = XMLParser(contentsOf:feedURL as URL)!
parser.delegate = self
parser.parse()
It work fine now.

As suggested by #Larme, it was the App Transport Security Issue How do I load an HTTP URL with App Transport Security enabled in iOS 9? .
This solved my issue!

Related

How to stream a video from an https url in objective-c

I'm trying to make a screensaver for Mac, that streams a simple mp4 video from a backend server. I'm not used to coding for Mac or in Objective-c, so I'm pretty lost. Until now I've figured out how to load a video locally, but when I try to switch out the file URL to an HTTPS URL it doesn't seem to work. This is how I'm loading the local mp4 file right now:
NSURL *url = [[NSBundle bundleForClass:MainScript.class] URLForResource: #"video" withExtension: #"mp4"];
AVAsset *asset = [AVAsset assetWithURL: url];
NSArray *assetKeys = #[#"playable", #"hasProtectedContent"];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset: asset automaticallyLoadedAssetKeys:assetKeys];
NSKeyValueObservingOptions options = NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew;
VideoPlayer *player = [VideoPlayer playerWithPlayerItem: playerItem];
[playerItem addObserver: player forKeyPath: #"status" options: options context: PlayerItemContext];
player.volume = 0;
As I said before, I'm not used to code in Objective-c, so I'm not 100 percent certain that this is the code that creates the local video stream, but I'm pretty sure it is.
What I want to do from here, is to replace the file URL: #"video" with an HTTPS URL like this: #"https://example.com/video". Everything is working on the backend server, so my problem is only how to load the video on a Mac screensaver.
As I understand it, I'm using AVPlayerItem to load the video right now. I don't know if there is a better way to do it, but if there is, please tell me :)
Try using VLCKit, I'm pretty sure that can do HTTPS out of the box.

How to load the next file while current one is playing through HTTP Live Streaming?

I'm working on an app in which I need to stream a large collection of audio files ranging from 5 to 15 seconds each.
I would like to reduce the load time between the files as much as possible.
Main Question:
Is there a way to start buffering the next file (through HLS) while the current one is playing the last segment?
Is AVQueuePlayer an appropriate solution for this on the iOS side?
A detailed explanation will be much appreciated, since I am new both to HTTP Live Streaming and the AV Foundation.
Related Question:
How do radio apps stream their audio with no lag between the songs?
Yes, AVQueuePlayer is an appropriate solution for playing a sequence of audio streamed from the internet via HTTP protocol.
I'm using AVQueuePlayer for quite a while now with excellent results and no lagging between songs. Here is a simple example on how to use AVQueuePlayer:
NSURL *url1 = [NSURL URLWithString:[urlsToPlay objectAtIndex:0]];
NSURL *url2 = [NSURL URLWithString:[urlsToPlay objectAtIndex:1]];
NSURL *url3 = [NSURL URLWithString:[urlsToPlay objectAtIndex:2]];
self.item1 = [[AVPlayerItem alloc] initWithURL:url1];
self.item2 = [[AVPlayerItem alloc] initWithURL:url2];
self.item3 = [[AVPlayerItem alloc] initWithURL:url3];
self.radioPlayerURLs = [[NSArray alloc] initWithObjects:self.item1,self.item2, self.item3, nil];
self.onDemandPlayer = [AVQueuePlayer queuePlayerWithItems:self.radioPlayerURLs];
[self.onDemandPlayer play];
For more details, please consulte Apple documentation:
AVFoundation
AVQueuePlayer

How to populate a UITableView from a remote .plist file

Sorry if this has been asked many times before, but nothing seemed to help my issue.
I have set up my app to work perfectly reading from local .plist files from within the app itself for testing purposes, but for the app to be of any use, I need to be able to update it on a web server and load it remotely.
Here is my current code for local plists.
How would I amend this with minimum changes elsewhere in the app?
Many thanks in advance.
- (void)viewDidLoad
{
[super viewDidLoad];
//Create a dictionary with the contents of Squad.plist
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Squad" ofType:#"plist"]];
//Fill the Squad array with contents of dictionary under the key "Players"
[self setSquad:(NSArray*) [dictionary objectForKey:#"Players"]];
//Reload the table data
[[self tableView] reloadData];
[[self navigationItem] setPrompt:nil];
}
You will need to first download your remote .plist while showing a loading or something, and after it's downloaded, use it locally the same way you are doing now. There's no way to read it remotely.

how to use my imported xml in my objective-c project?

i have this demo xml file:
<trains>
<israel>
<Lehavim>
<lat>31.370201</lat>
<lon>34.798336</lon>
</Lehavim>
<tel-aviv>
<lat>32.073847</lat>
<lon>34.793358</lon>
</tel-aviv>
</israel>
</trains>
loaded to my project using this code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:#"http://www.amotech.co/trains.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate:self];
BOOL success = [xmlParser parse];
if(success)
NSLog(#"success");
else
NSLog(#"Error");
}
i need to get my xml into a UITableView - i want to only to show the name of the train station (for example: "Lehavim" in the TableView.
and after i chose one of my table view items - i want to load the current lat and lon data into double objects.
i would like you to help me with this please.
thanks, amir.
Sounds like you need to get the fundamentals first:
This link takes you to the gory details of NSXMLParser directly from apple.
This link shows a fantastic example of how to use the NSXMLParser.
This link offers some simple examples for adding objects to an array.
This link is a very nice tutorial on UITableViewController.
You will need to provide a little elbow grease.
Quite Tip
To save yourself some headaches and to add flexibility to your xml, I recommend you modify your xml structure such that you aren't using your element names as values.
Change:
<trains>
<israel>
<Lehavim>
<lat>31.370201</lat>
<lon>34.798336</lon>
</Lehavim>
<tel-aviv>
<lat>32.073847</lat>
<lon>34.793358</lon>
</tel-aviv>
</israel>
</trains>
to:
<trains>
<train>
<name>israel</name>
<location>
<name>Lehavim</name>
<lat>31.370201</lat>
<lon>34.798336</lon>
</location>
<location>
<name>tel-aviv</name>
<lat>32.073847</lat>
<lon>34.793358</lon>
</location>
</train>
</trains>

How to get a DTD's public and system IDs with NSXMLParser

I'm trying to retrieve the public and system IDs for a DTD in an XML document via NSXMLParser. While NSXMLParser in principal offers publicID and systemID selectors they don't seem to work for me. The doctype tag looks like this:
<!DOCTYPE Article PUBLIC "-//SoftQuad Software//DTD Journalist v2.0 20000501//EN" "file:///C:/Program%20Files/Corel/XMetaL%204/Author/Rules/journalist.dtd">
Here's my code (the file was opened via NSFileHandle's readDataToEndOfFile:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
BOOL parseSuccessful = [parser parse];
In the delegate's parserDidStartDocument: I try to access the IDs:
NSLog(#"%# : %#", [parser publicID], [parser systemID]);
But I only see
(null) : (null)
From the documentation:
You may invoke this method once a parsing operation has begun or after an error occurs.
So I'd think this should work already in parserDidStartDocument: but I tried to call these selectors in different delegate methods (like parser:didStartElement:namespaceURI:qualifiedName:attributes: but without success.
Any ideas what I'm doing wrong?
You might try out another parser.
http://www.robbiehanson.com/expat.html provides a drop-in replacement for NSXMLParser based on expat.
You might also find this article interesting, comparing the performance of various XML parsers on the iPhone.
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project