0

I'm creating an iOS5 app (programmed in Objective-C) which provides a couple of functions. One of them is to allow a user to fill out a text field with JavaScript. When the user presses a "Test" button, I want it to save to a specific JS file and move to a new view, displaying an HTML page that will display the results of that JavaScript on a canvas element.

Unfortunately for me, I have no idea how to save to a JS file for a text field. Nor do I know if this is actually the best way to achieve the results I'm after.

So can anyone tell me the code I'd need to place in the IBAction of my test button to save the file, or if there is a better way to get the user's script into the HTML file with the canvas element?

FreelancerJ
  • 55
  • 1
  • 8

2 Answers2

2

You have some options to do that.

Add a callback in WebView is possible only in desktop apps, but you can make a workaround.

1- Set a handler to click in javascript;

2- This handler parse the value via windows.location (trying to change the current url);

3- In UIWebviewDelegate set webView:shouldStartLoadWithRequest:navigationType: to recognize this values parsed by javascript and returning NO (canceling the url change).

After you have the code typed by user, is easier pass to the new one UIWebView. You can save the file via NSData or other class and load in by the path, or you can parse directly the code to be showed via NSString.

EDIT

I, still, belive what I said is what you want, but with a little more info. Yes, 3 NSString probably solve your problem. You even can call eval in javascript, via objective-c and parse user code too. Those logic ideas are a good approaching. You can choose the easier for you.

There are more info in this another Q&A

Community
  • 1
  • 1
Ratata Tata
  • 2,781
  • 1
  • 34
  • 49
  • That Q&A seems to be more about accessing Objective-C data from an existing JavaScript? I'm trying to allow a user to write up JavaScript file which can then be saved as a JS file, and loaded by a pre-existing HTML page on the next view, and I'm really trying to keep it as simple as possible. – FreelancerJ Nov 11 '11 at 00:50
  • A friend has suggested I can probably just have three NSStrings, one immutable one that has all the HTML that comes before the JavaScript (doctype, title etc), one mutable one that will then contain the user's JavaScript, and a final immutable one that contains the rest of the page, that concatenate together and can then be sent to the UIWebView as the page source. Does that sound workable to you? – FreelancerJ Nov 11 '11 at 00:50
0

You can use html 5 local storage for that.

alexandresoli
  • 918
  • 1
  • 9
  • 18
  • This whole app is in Objective-C, is there a way I can actually be using HTML5 elements seamlessly within my existing code? – FreelancerJ Nov 11 '11 at 00:40