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;
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.
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:
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);
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:
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