MonoTouch UIWebView and EvaluateJavascript

2019-05-24 07:17发布

I have an application working on the following envirnoment:

  • Monotouch 2.1
  • MonoDevelop 2.4.2
  • MacOS 10.6.8
  • iOS SDK 4.3

The same application, migrated to iOS 5.0/5.1 and Monotouch 5.2.10 with MonoDevelop 2.8.8.4 under, has the following problem: When i click a button to navigate in a UIWebView do not work.

Code is that:(obviously it's same in Monotouch 2.1)

public void ScrollToTop (object sender, EventArgs evt)
{
    webView.EvaluateJavascript("window.scrollTo(0,0)");
}

What could i do?

SOLVED (with Jonathan.Peppers help):

public void ScrollToTop (object sender, EventArgs evt)
{
    if((UIDevice.CurrentDevice.CheckVersion(5, 0)){                 
                System.Drawing.PointF p = new System.Drawing.PointF(0, 0);
                webView.ScrollView.SetContentOffset(p,true);
            }
            else{
                webView.EvaluateJavascript("window.scrollTo(0,0)");                 
            }
}

i have made in that way because on 4.3 webView.ScrollView.SetContentOffset make application crash.

2条回答
Explosion°爆炸
2楼-- · 2019-05-24 07:44

Try this:

string error = webView.EvaluateJavascript("try { window.scrollTo(0,0); } catch (e) { return e.message; }");

(double check my code)

It should get you a better error message. Tell us the value of the string returned.

I suspect the issue isn't MonoTouch, but perhaps iOS5? Safari had drastic changes in iOS5, new javascript engine, etc.

查看更多
Bombasti
3楼-- · 2019-05-24 07:46

Note that UIWebView is not re-entrant. If the way of loading the (or just the timing of) page changes between iOS release then this may become a problem.

To avoid this just make sure you're not calling back into any UIWebView instance until the loading is completed, e.g. LoadFinished or LoadError is called.

If this does not help then please create a small, self-contained, test case that duplicate the issue and attach it to a bug report at http://bugzilla.xamarin.com

查看更多
登录 后发表回答