Unity App + Web Service = ?

Hi there!

After many attempts of trying to get a response from the Answer-section, I will now try to use this post to hopefully get some feedback. Anyhow, I am currently trying to make an app with Unity for iOS. Basically I have now faced a rather complicated problem, and hopefully some of you have the answer for it.

In my app I want to display posts (news) by date. To improve my security, I have chosen not to connect directly to my database, but instead use a web service (php pages) to communicate. I have managed to write a PHP script that displays 3 posts from my database (header, and content) on the page. But the thing is - how would I proceed if I wanted for each ‘new post’ to automatically create a new panel with a Text for the header and a text for the content in my app? I have tried to look into XML - but I am still not able to solve this problem. I have included a picture as an example.

So as you see, for each new post - I want to generate a new small panel, and to texts for its content. How would I proceed for this? If you have any tutorials that you think it’s worth for me checking out, please tell me about it!

Thanks in advance!

So… I’m assuming that because you say:

That you’ve managed to write a php script that generates html and returns that as its response.

Well… first off I’d say that this isn’t a suitable interface for retrieving post information. It’s great for generating html web pages… but not so much for retrieving a feed. Problem is that because it’s HTML you’d have to parse the data out of that HTML.

When accessed via a browser this is fine, because a browser expects to retrieve HTML. Its entire job is to parse HTML and render the results on screen.

So here’s the thing with php. How it works is that when an HTTP request comes in to a web server, and the address it requrest points at a php file, the server first hands it off to the php engine which then processes the script in it generating text that will be returned back as the hypertext.

You can return any valid hypertext. You just ‘print’ whatever characters you want to form valid hypertext… usually some XML of some sort. This includes the HTML you returned (HTML being a retro-active extension of XML).

What you can do instead though is just format the data into your own desired structure. And on the receiving end parse it as that sort of data.

Lets say you decide to format it as:

<?xml version="1.0" encoding="UTF-8"?>
<feed section="news">
    <entry>
        <date>08.05.2015 3:32PM EST</date>
        <header>Header 1</header>
        <text>sometext here</text>
    </entry>
    <entry>
        <date>08.04.2015 2:18PM EST</date>
        <header>Header 1</header>
        <text>sometext here</text>
    </entry>
    <entry>
        <date>08.03.2015 10:15AM EST</date>
        <header>Header 1</header>
        <text>sometext here</text>
    </entry>
</feed>

On the receiving end you can just request the address, then use the System.Xml namespace to parse the xml in it.

You KNOW the structure, so you just validate and read it.

There’s a lot of methods you could read with. You can read it as a stream with XmlReader, or you could traverse the nodes independently with XmlDocument, or you could have a more robust system with the System.Xml.Linq namespace and the XDocument class.

Personally I prefer the XDocument method:

1 Like

Thanks for a well detailed and rather fast response. But then I have a question; how would/could this be automated? XML won’t be generated for each new post I make? I am sorry, but I’ve never really dealt with any of this on the programming side before - but I am very eager to learn how to make a proper feed. Could you possibly describe both the web-side, and the ‘app-side’ a bit more, as I am very intrested to find out how it works. I felt that I have asked all around the web, and also searched through google without any proper solutions.

This is a new side of programming I’ve never really dealt with before, but I really want to learn. Thanks for your feedback. I really appreciate it.

The php page would generate the xml based off the query it pulls from the database.

You build xml in php the same way you’d build html. Just in your custom xml schema, rather than the standard html schema.

Personally I don’t write PHP, haven’t in several years, and swore to myself I would NEVER return to that wasteland that is somehow more annoying to write than javascript. If it weren’t for that, I’d actually show you some code for server side.

Don’t forget that Apple will reject your app if the API you’re implementing is non-public. Just in case you were thinking of locking it up.

In that case nobody could not build a game with a online highscore system because i would not make the way i send and get data from my webserver publicly available.

Misunderstanding on my part as to what their guidelines meant. Non-public only refers to internal (iOS) APIs that are not documented and openly provided by Apple. External services are not considered non-public.

My bad!

Edit: However, I do feel like you could have a public API available that just required an authorization token to retrieve specific data. This wouldn’t qualify as a private service, but could still protect certain content. I’ve never actually had to do this in practice, though, so I admittedly don’t really know what I’m talking about. :slight_smile:

That makes a lot more sense. Using internal or not disclosed iOS api is no way to go.