1010#import " RootViewController.h"
1111#import " GAScriptEngine.h"
1212#import " GAScriptBlockObject.h"
13- #import " UIWebView+GAJavaScript .h"
13+ #import " GAScriptMethodSignatures .h"
1414
1515@interface DetailViewController ()
1616
@@ -29,7 +29,37 @@ @implementation DetailViewController
2929@synthesize rootController = _rootController;
3030@synthesize popoverController=_myPopoverController;
3131
32- #pragma mark - Managing the detail item
32+ - (IBAction )invokeJavaScript : (id )sender
33+ {
34+ NSString * prompt = @" Hello from Objective-C" ;
35+ NSString * js = [NSString stringWithFormat: @" window.prompt('%@ ')" , prompt];
36+ NSString * result = [self .webView stringByEvaluatingJavaScriptFromString: js];
37+ NSLog (@" window prompt: %@ " , result);
38+
39+ // The importance of escaping data
40+ //
41+ prompt = @" This is a javascript prompt" ;
42+ js = [NSString stringWithFormat: @" window.proompt('%@ ')" , prompt];
43+ // js = [NSString stringWithFormat:@"try { %@; } catch (ex) { JSON.stringify(ex); }", js];
44+ result = [self .webView stringByEvaluatingJavaScriptFromString: js];
45+ NSLog (@" window prompt: %@ " , result);
46+
47+ // Often you need to call toString() manually
48+ //
49+ js = [NSString stringWithFormat: @" new Date(%.0f )" , [[NSDate date ] timeIntervalSince1970 ] * 1000 ];
50+ result = [self .webView stringByEvaluatingJavaScriptFromString: js];
51+ NSLog (@" new Date: %@ " , result);
52+
53+ js = [NSString stringWithFormat: @" new Date(%.0f ).toString()" , [[NSDate date ] timeIntervalSince1970 ] * 1000 ];
54+ result = [self .webView stringByEvaluatingJavaScriptFromString: js];
55+ NSLog (@" new Date: %@ " , result);
56+
57+
58+ // Can help with error detection
59+ // js = [NSString stringWithFormat:@"try { %@; } catch (ex) { ex.toString(); }", js];
60+ }
61+
62+ #pragma mark - UIViewController
3363
3464- (void )viewWillAppear : (BOOL )animated
3565{
@@ -88,6 +118,17 @@ - (void)viewDidLoad
88118 [super viewDidLoad ];
89119
90120 _scriptEngine = [[GAScriptEngine alloc ] initWithWebView: _webView];
121+
122+ // Add the methods for DOM Traversal
123+ [GAScriptMethodSignatures addMethodSignaturesForClass: [DOMTraversal class ]];
124+
125+ NSURL * url = [[NSUserDefaults standardUserDefaults ] URLForKey: @" WebView URL" ];
126+
127+ if (url)
128+ {
129+ [_webView loadRequest: [NSURLRequest requestWithURL: url]];
130+ [self .urlField setText: [url absoluteString ]];
131+ }
91132}
92133
93134
@@ -137,6 +178,8 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
137178- (BOOL )webView : (UIWebView *)webView shouldStartLoadWithRequest : (NSURLRequest *)request
138179 navigationType : (UIWebViewNavigationType)navigationType
139180{
181+ NSLog (@" DetailView will load %@ " , [request URL ]);
182+
140183 return YES ;
141184}
142185
@@ -148,26 +191,38 @@ - (void)webViewDidStartLoad:(UIWebView *)webView
148191
149192- (void )webViewDidFinishLoad : (UIWebView *)webView
150193{
151- NSString * js = [NSString stringWithFormat: @" window.alert('Hello from Objective-C')" ];
152- NSString * result = [webView stringByEvaluatingJavaScriptFromString: js];
153- NSLog (@" UIWebView: %@ " , result);
194+ // Save the URL for future runs
195+ [[NSUserDefaults standardUserDefaults ] setURL: [webView.request URL ] forKey: @" WebView URL" ];
154196
155- NSDate * date = [NSDate date ];
156- js = [NSString stringWithFormat: @" new Date(%.0f )" , [date timeIntervalSince1970 ] * 1000 ];
157- result = [webView stringByEvaluatingJavaScriptFromString: js];
158- NSLog (@" UIWebView: %@ " , result);
159-
160- js = [NSString stringWithFormat: @" new Date(%.0f ).toString()" , [date timeIntervalSince1970 ] * 1000 ];
161- result = [webView stringByEvaluatingJavaScriptFromString: js];
162- NSLog (@" UIWebView: %@ " , result);
197+ if (_rootController.document == nil )
198+ {
199+ [_rootController setDocument: [_scriptEngine documentObject ]];
200+ [_rootController setRootNode: [_scriptEngine documentObject ]];
201+ }
163202
164- js = @" var arrayValue = [1000, 2000, 3000, 'String with a comma,', 5000]" ;
165- result = [webView stringByEvaluatingJavaScriptFromString: js];
166- NSLog (@" UIWebView: %@ " , result);
167-
168- js = @" arrayValue.toString()" ;
169- result = [webView stringByEvaluatingJavaScriptFromString: js];
170- NSLog (@" UIWebView: %@ " , result);
203+ [[_scriptEngine documentObject ] setFunctionForKey: @" ontouchstart" withBlock: ^ (NSArray * arguments)
204+ {
205+ id touchEvent = [arguments objectAtIndex: 0 ];
206+ [[_scriptEngine windowObject ] callFunction: @" console.log" withObject: @" ontouchstart" ];
207+
208+ [_rootController setRootNode: [touchEvent valueForKey: @" target" ]];
209+ }];
210+
211+ // Override the webview console to call us back so we can log to NSLog()
212+ //
213+ id console = [[_scriptEngine windowObject ] valueForKey: @" console" ];
214+
215+ [console setFunctionForKey: @" log" withBlock: ^ (NSArray * arguments)
216+ {
217+ if ([arguments count ] == 2 )
218+ {
219+ NSLog (@" WebView: %@ %@ " , [arguments objectAtIndex: 0 ], [arguments objectAtIndex: 1 ]);
220+ }
221+ else
222+ {
223+ NSLog (@" WebView: %@ " , [arguments objectAtIndex: 0 ]);
224+ }
225+ }];
171226}
172227
173228@end
0 commit comments