Opening another application on the iPhone using NSUrl

The NSUrl class is what allows an application to open other applications within the iPhone OS while passing parameters. The calling sequence is fairly simple. There are two steps to making this call:
1. When the NSUrl class is instantiated, a string representing the URL scheme is called. Note that different applications will have different schemes and use different protocols.
2. The static method UIApplication.SharedApplication.OpenUrl(ns) is called, where ns is the NSUrl class. When the static method is called, if it is not possible to open a URL, a false is returned. If it is possible to open the URL that is passed, the URL is opened and a true is returned.

 Some code to do this looks like:

NSUrl ns = new NSUrl(prot + first + sec.Trim());

if (!UIApplication.SharedApplication.OpenUrl(ns))

            {

                var av = new UIAlertView("Badness happened"

                    , "The scheme '" + prot + "' is not supported on this device.",

                    null, "Ok thanks", null);

                av.Show();

            }

Ok, this all sounds cool, now lets look at the format of the url schemes that you can pass into NSUrl.  For a list of them, check out: 
http://wiki.akosma.com/IPhone_URL_Schemes

Want to know more about developing with the iPhone?  Check out my Wrox Blox eBook on developing applications with MonoTouch for the iPhone/iPod touch for .NET/C# developers.  

No Comments