Web page request

When I try to retrieve a web page contents in the Unity tool, it works fine, but whenever I build the application on the iPhone, it doesn’t get it for some reason.
C# code follows:

	public ArrayList getTopApps() {
			ArrayList titleArray = new ArrayList();
			string fetchUrl = "MYURL";
		
            XmlDocument doc = new XmlDocument();
            doc.Load(fetchUrl);
            XmlElement root = doc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("//top_apps/app"); 
            foreach (XmlNode node in nodes)
            {
            	Debug.Log("iterating through nodes");
                string thisTitle = node["appTitle"].InnerText;
                string instances = node["instances"].InnerText;
                int inst = Convert.ToInt32(instances);
                Debug.Log("the current instances are: "+inst);
               	AppTitle thisApp = new AppTitle(thisTitle,inst);
               	titleArray.Add(thisApp);
            }
 

		
	return titleArray;

Are there any security settings on the iPhone and iPod touch preventing url requests?

Hi,
.NET WebRequests are not supported on iPhone. Please use WWW class for downloading content via http.

I’m trying that, but with no success. I’ve got:

But it just returns empty. What is wrong here?

It does what you tell it to do and you tell it to use empty data.

If you want to get the data from the web, you need to add a yield return page; in there to wait for the data to be downloaded

Ok, sorry if i’m being dense, but I can’t figure out how to use yield in C#. It complains about not being an iterator type. How is this done?

Hi,
for some encoding + symbols combination WWW.data returns empty string. Try to use WWW.bytes.

I’m struggling to get this to work properly. Can someone point me in the right direction please? So far I have my loader class:

	using UnityEngine;
using System.Collections;

public class PageLoader: IEnumerator {

	
	public IEnumerator Request (string url) {
      WWW www = new WWW(url);
      yield return www;
   	}


}

called by:

XmlDocument doc = new XmlDocument();
            StringBuilder sb = new StringBuilder();
            PageLoader pl = new PageLoader();
            foreach(string str in pl.Request(fetchUrl)){
            	sb.Append(str);
            }
			string xmlString = sb.ToString();
			doc.LoadXml(xmlString);

At the moment I’m getting the error:

 foreach statement cannot operate on variables of type `System.Collections.IEnumerator' because it does not contain a definition for `GetEnumerator' or is not accessible

Make www a public variable of PageLoader. Then, use StartCoroutine to call the Request method. Then, use the www variable of PageLoader to access the data once the download has finished. The declaration of IEnumerator is purely to make the coroutine functionality work - it has nothing to do with foreach iteration in this case.

Thanks for your response. Now I have Loader class:

and I’m calling it like this:

But it is still coming back empty.

I have also had to make my calling class implement MonoBehaviour which raising a warning because of the way it is instantiated. Do I need to worry about this as it’s only a warning? If so, how would I replace:

DataLayer dl = new DataLayer();

I’ve tried using:

dl = AddComponent(DataLayer);

But it doesn’t work.

dl = AddComponent(DataLayer);

should be

DataLayer dl = (DataLayer)AddComponent(typeof(DataLayer));

Thanks for your reply. That gives the following error though:

Are you calling the function from a MonoBehaviour extending class?

AddComponent is called from my GUI controller class. This starts like this:

using UnityEngine;
using System.Collections;

public class GuiBehaviour : MonoBehaviour
{
    // define and create our GUI delegate
    private delegate void GUIMethod();
    private GUIMethod currentGUIMethod;
    private int length = 10;
    private int buttonHeight = 30;
    private int spacer = 2;
    //public TopTweetController gameController;
    private bool cubeSwitch = false;
    private bool commentBool = true;
    private DataLayer dataL;
    private ArrayList appList;
    private ArrayList commentsList;
    private int currentApp;
	public Transform brickObject;
           
    void Start ()
    {
    	appList = new ArrayList();
        // start with the main menu GUI
        this.currentGUIMethod = MainMenu;
        iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Portrait;
		dataL = (DataLayer)AddComponent(typeof(DataLayer));

StartCoroutine is then called from that DataLayer class which begins like this:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
using System.Text;
using System;


public class DataLayer : MonoBehaviour {
	
	Configs configOb = new Configs();
	PageLoader pl = new PageLoader();
	
	public ArrayList getTopApps() {
			ArrayList titleArray = new ArrayList();
			string fetchUrl = configOb.getRoot()+"TopTen.php";
            XmlDocument doc = new XmlDocument();
			StartCoroutine(pl.Request(fetchUrl));
			WWW webPage = pl.www;
			string xmlString = webPage.data;
			Debug.Log("The returned string is "+xmlString);
			doc.LoadXml(xmlString);

you could see if gameObject.AddComponent(…) helps.

Alternatively you could use
[RequireComponent(typeof(DataLayer))]

for your class and then use GetComponent instead of AddComponent

Thanks for your suggestions. I’ve now tried all of those options, but none of them would compile. Now I have dragged the scripts onto the same gameobject and am trying to reference them using:

dataL = GetComponent();

and I get the error:

Assets/UserScripts/GuiBehaviour.cs(29,42): error CS1002: Expecting `;’

I’m pretty sure this is exactly as it exists in the script reference though…

no that does not exist.
Thats a Unity Desktop 2.6 function and I would guess you got it from the online documentation which is Unity Desktop only, not from the iPhone documentation which comes with your iphone install and with which you should work exclusively when developping for the iphone.

You are restricted to .NET 1.1 on Unity iPhone, no access to generics at all.
You have to use the general one and cast the return, either through ()gameObject.GetComponent(…); or through gameObject.GetComponent(…) as

Ok, now it will compile ok. I have used:

pl = (PageLoader)gameObject.GetComponent(typeof(PageLoader));

within my DataLayer class and there are no errors, but when I come to call a method from one of the instantiated classes at runtime, I get:

sounds like you try to use DataLayer.getTopApps(…) instead of pl.getTopApps