2
votes

I am seeing problem happen only on iOS 6 but working fine on below iOS 6 version.

My app is loading image from a link which need to enlarge its image pixels to populate within size of UIWebView by using Javascript. The link image is originally in pixels 140x24.

I see it is running over the line, but the image not being scaled up using iOS 6 simulator. It does work in iOS 5 simulator. Please help.

(void)webViewDidFinishLoad:(UIWebView *)webView {

  [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(\"img\")(0).height=\"30\";document.getElementsByTagName(\"img\")(0).width=\"280\";"];

}
1

1 Answers

3
votes

The syntax in javascript to get the n-th element of an array is using square braces, not parentheses. So use document.getElementsByTagName("img")[0] not document.getElementsByTagName("img")(0).

Not sure why it was working in iOS5, WebKit is probably more strict in iOS6…

Additional tip: to debug javascript errors on iOS6, a new feature makes it very simple:

  • Go to the "Settings" application in your iPhone, enter the "Safari" settings and tap on "Advanced" at the bottom, then enable the "WebKit Inspector" from there
  • Display your page to be debugged on the screen (i.e. launch your app and go to your screen that contains the WebView to make it visible)
  • Plug your iPhone to your Mac via your USB cable
  • Open Safari.app on your Mac, go to the "Development" menu (1), select the menu item with the name of your iPhone and select your application in the submenu.

You will then be able to debug your web page presented in your iPhone using the powerful Web Inspector from your Mac, and can look into the javascript console, the DOM tree, etc.

(1) If you don't have the "Development" menu in Safari on your Mac, you can enable it in the "Advanced" preferences tab of the Safari application.