stringByEvaluatingJavaScriptFromString is not working in iOS 6

依然范特西╮ 提交于 2019-12-11 02:56:21

问题


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:


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.



来源:https://stackoverflow.com/questions/12704593/stringbyevaluatingjavascriptfromstring-is-not-working-in-ios-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!