1

I am working on creating an app and am a relatively new Objective C programmer. On a page, "test.php", I have a javascript variable. Here is a simplified version.

<script>
var idx = 45;
document.write(idx);
</script>

I would like to be able to view the value of the variable "idx" in my IOS app, which I have created using Xcode. What would be the optimal way to connect to the page, test.php, and transfer my Javascript variable to Objective C so I could use it as part of my IOS App?

Any help would be greatly appreciated. Thank you.

  • "xcode" is not a run-time environment. The title and most of the contents therefore make NO sense. Make sure to describe the *actual* context in more [accurate] detail: eg. is this a UIWebView? – user2864740 Aug 08 '14 at 02:26

1 Answers1

0

You will need to communicate between the webview and the native code using a combination of two pieces. First, you will need to "trigger" the webview into returning some value by calling stringByEvaluatingJavaScriptFromString: on the webview. When the webview tries to navigate because of the javascript call, you can intercept it on the native side with the WebView's shouldStartLoadWithRequest: method.

For details, see the specifics here Invoke method in objective c code from HTML code using UIWebView

That is only if you need some activity in the webview to trigger the fetch (or notify the native code that the value has been set). If the value is always available via JS, you can simply use stringByEvaluatingJavaScriptFromString: as follows:

NSString *returnvalue =  [self.webviewForHtml stringByEvaluatingJavaScriptFromString:@"var idx = "return_value";return idx;"];
Community
  • 1
  • 1
wottle
  • 13,095
  • 4
  • 27
  • 68