views:

66

answers:

1

I'm porting a program I wrote on C# to Cocoa and I'm trying to figure out how to use XML to go online to my website, grab the file and parse it, then compare the version and pop a message box to ask if you want to open your browser to the update page.

Here's the code from C#:

 public void CheckVersion()
    {
        Version newVersion = null;
        string url = "";
        string newfeatures = "";
        try
        {
            string xmlURL = "http://myurl.com/version.xml";
            XmlRead = new XmlTextReader(xmlURL);
            XmlRead.MoveToContent();
            string elementName = "";
            if ((XmlRead.NodeType == XmlNodeType.Element) &&
                (XmlRead.Name == "myProgram"))
            {
                while (XmlRead.Read())
                {
                    if (XmlRead.NodeType == XmlNodeType.Element)
                        elementName = XmlRead.Name;
                    else
                    {
                        if ((XmlRead.NodeType == XmlNodeType.Text) &&
                            (XmlRead.HasValue))
                        {
                            switch (elementName)
                            {
                                case "version":
                                    newVersion = new Version(XmlRead.Value);
                                    break;
                                case "url":
                                    url = XmlRead.Value;
                                    break;
                                case "newfeatures":
                                    newfeatures = XmlRead.Value;
                                    break;
                            }
                        }
                    }
                }
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Could not connect to update checking server.");
        }
        finally
        {
            if (XmlRead != null) XmlRead.Close();
        }
        Version curVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        if (curVersion.CompareTo(newVersion) < 0)
        {
            string title = "New Version Online";
            string question = "Download new version? \nmyProgram Version: " + newVersion.ToString();
            if (DialogResult.Yes ==
             MessageBox.Show(this, question + "\n\n" + newfeatures, title,
                             MessageBoxButtons.YesNo,
                             MessageBoxIcon.Question))
            {
                System.Diagnostics.Process.Start(url);
            }
        }  
    }

I'm pretty new to cocoa and most of that C# code was a snippet so any advice or help would be great.

Thanks

+3  A: 

For updates the best solution available is Sparkle, as the others pointed out before.

But if you still want to know about loading XML from a server and parsing it you should read the documentation for the URL Loading System and on NSXMLDocument. With that it is not hard to get and parse the data, but you won’t be able to stuff everything into one method. NSURLConnection works with delegates so you don’t have to block the current thread while it is getting the data from the server.

Sven
Thanks a lot, yep sparkle worked perfectly, better than I had hoped. Coming from Windows I wasn't expecting it to be this easy. lol
Hector204