2

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.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Luigi Saggese
  • 5,299
  • 3
  • 43
  • 94

2 Answers2

2

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.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • Excellent observation. In fact on iOS 4.3 works fine, instead on iOS 5 no. I have tried to execute your code but there are no exceptions and there are no errors written on string error. How do you think can solve? – Luigi Saggese Apr 10 '12 at 12:24
  • This question has a couple examples that might do what you need: http://stackoverflow.com/questions/2506958/how-to-find-in-javascript-the-current-scroll-offset-in-mobile-safari-iphon. They are in Objective-C, but should be easy to pull out the javascript – jonathanpeppers Apr 10 '12 at 12:59
  • I have solved in that way: i have check which iOS is running and i have execute different operation. i have update my question with answer. – Luigi Saggese Apr 10 '12 at 13:16
2

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

poupou
  • 43,413
  • 6
  • 77
  • 174