1
votes

I'm creating some web/native hybrid iOS App with some C# background task on MonoTouch.

For a starter, I tried to create very simple webView sample referring

(The above sample project code works, but I just need a webView without NavigatorView) and

My code so far is:

AppDelegate.cs

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iostest
{ 
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    { 
        UIWindow window;  
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            // create a new window instance based on the screen size
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.RootViewController = new WebViewController(); 
            // make the window visible
            window.MakeKeyAndVisible ();

            return true;
        }
    }
}

WebViewController.cs

using System;
using System.IO;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iostest
{
    public class WebViewController : UIViewController {

        UIWebView webView;
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            Console.WriteLine ("WebView Launched"); //this works

            webView = new UIWebView(View.Bounds);
            webView.ScalesPageToFit = false;
            webView.LoadRequest (new NSUrlRequest
                (new NSUrl (Path.Combine(NSBundle.MainBundle.BundlePath,
                               "www/app.html"), false)));

            this.View.AddSubview(webView);
        }
    }
}

This code runs without an error, but results with only a blank white page, without showing my HTML content "www/app.html" (or, "http://google.com" whatever).

I don't see what logic I miss. Any thought? Thanks.

1

1 Answers

1
votes

OK, things has been fuzzy, but I resolved by myself.

The problem is the local resource files ("www/app.html") should be flagged as 'content' at BuildAction Property of IDE.

I remember I was trapped by this a few years ago, and still see many people are trapped.

Lack of documentation cause wasting time. Please document Xamarin.

The current code for future reference:

AppDelegate.cs

using System;
//using System.Collections.Generic;
//using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iOStest2
{ 
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        UIWindow window;     
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);      
            window.RootViewController = new WebViewController ();
            window.MakeKeyAndVisible ();

            return true;
        }
    }
}

WebViewController.cs

using System;
using System.IO;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace iOStest2
{
    public class WebViewController : UIViewController
    {
        UIWebView webView;

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            Console.WriteLine ("WebView Launched");
            View.BackgroundColor = UIColor.Gray;
            //string url = "http://google.com";
            string url= Path.Combine(NSBundle.MainBundle.BundlePath,
                                 "Content/app.html");
            webView = new UIWebView(View.Bounds);
            webView.LoadRequest(new NSUrlRequest(new NSUrl(url,false))); 
            webView.ScalesPageToFit = false;

            View.AddSubview(webView);
        }
    }
}