Skip to content

Commit b370c39

Browse files
Added custom table cell, some sample javascript eval code
1 parent f8fa93d commit b370c39

9 files changed

Lines changed: 368 additions & 339 deletions

File tree

Samples/WebView Explorer.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2D5A7870139C617000335643 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D5A786F139C617000335643 /* RootViewController.m */; };
2121
2D76610D142022CD00FA33D5 /* libGAJavaScript.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D76610C142022CD00FA33D5 /* libGAJavaScript.a */; };
2222
2D76610F142022E900FA33D5 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2D76610E142022E900FA33D5 /* QuartzCore.framework */; };
23+
2DF677F41420867E00285058 /* HtmlElementTableCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DF677F31420867E00285058 /* HtmlElementTableCell.m */; };
2324
/* End PBXBuildFile section */
2425

2526
/* Begin PBXFileReference section */
@@ -42,6 +43,8 @@
4243
2D5A786F139C617000335643 /* RootViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = "<group>"; };
4344
2D76610C142022CD00FA33D5 /* libGAJavaScript.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libGAJavaScript.a; sourceTree = BUILT_PRODUCTS_DIR; };
4445
2D76610E142022E900FA33D5 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
46+
2DF677F21420867E00285058 /* HtmlElementTableCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HtmlElementTableCell.h; sourceTree = "<group>"; };
47+
2DF677F31420867E00285058 /* HtmlElementTableCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HtmlElementTableCell.m; sourceTree = "<group>"; };
4548
/* End PBXFileReference section */
4649

4750
/* Begin PBXFrameworksBuildPhase section */
@@ -109,6 +112,8 @@
109112
2D5A786B139C617000335643 /* DetailView.xib */,
110113
2D5A786E139C617000335643 /* RootViewController.h */,
111114
2D5A786F139C617000335643 /* RootViewController.m */,
115+
2DF677F21420867E00285058 /* HtmlElementTableCell.h */,
116+
2DF677F31420867E00285058 /* HtmlElementTableCell.m */,
112117
2D5A785A139C617000335643 /* Supporting Files */,
113118
);
114119
path = "WebView Explorer";
@@ -194,6 +199,7 @@
194199
2D5A7864139C617000335643 /* ExplorerAppDelegate.m in Sources */,
195200
2D5A786A139C617000335643 /* DetailViewController.m in Sources */,
196201
2D5A7870139C617000335643 /* RootViewController.m in Sources */,
202+
2DF677F41420867E00285058 /* HtmlElementTableCell.m in Sources */,
197203
);
198204
runOnlyForDeploymentPostprocessing = 0;
199205
};

Samples/WebView Explorer/DetailViewController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@
2929
@property (nonatomic, retain) GAScriptEngine* scriptEngine;
3030
@property (nonatomic, assign) RootViewController * rootController;
3131

32+
- (IBAction)invokeJavaScript:(id)sender;
33+
3234
@end

Samples/WebView Explorer/DetailViewController.m

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// HtmlElementTableCell.h
3+
// WebView Explorer
4+
//
5+
// Created by Andrew Goodale on 9/6/11.
6+
// Copyright 2011 Wingspan Technology, Inc. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
enum DomNodeType
12+
{
13+
ELEMENT_NODE = 1,
14+
ATTRIBUTE_NODE = 2,
15+
TEXT_NODE = 3,
16+
COMMENT_NODE = 8
17+
};
18+
19+
@interface HtmlElementTableCell : UITableViewCell
20+
{
21+
@private
22+
id _domElement;
23+
}
24+
25+
@property (nonatomic, retain) id domElement;
26+
27+
@end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//
2+
// HtmlElementTableCell.m
3+
// WebView Explorer
4+
//
5+
// Created by Andrew Goodale on 9/6/11.
6+
// Copyright 2011 Wingspan Technology, Inc. All rights reserved.
7+
//
8+
9+
#import "HtmlElementTableCell.h"
10+
11+
12+
@implementation HtmlElementTableCell
13+
14+
@synthesize domElement = _domElement;
15+
16+
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
17+
{
18+
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
19+
20+
if (self)
21+
{
22+
// Initialization code
23+
}
24+
25+
return self;
26+
}
27+
28+
- (void)dealloc
29+
{
30+
[_domElement release];
31+
32+
[super dealloc];
33+
}
34+
35+
- (void)setDomElement:(id)domElement
36+
{
37+
[_domElement release];
38+
_domElement = [domElement retain];
39+
40+
NSNumber* nodeType = [_domElement valueForKey:@"nodeType"];
41+
42+
if ([nodeType intValue] == ELEMENT_NODE)
43+
{
44+
self.textLabel.text = [_domElement valueForKey:@"nodeName"];
45+
46+
if ([[_domElement valueForKey:@"childElementCount"] intValue] > 0)
47+
{
48+
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
49+
self.detailTextLabel.text = @"";
50+
}
51+
else
52+
{
53+
self.accessoryType = UITableViewCellAccessoryNone;
54+
self.detailTextLabel.text = [_domElement valueForKey:@"textContent"];
55+
}
56+
57+
}
58+
else if ([nodeType intValue] == TEXT_NODE)
59+
{
60+
self.accessoryType = UITableViewCellAccessoryNone;
61+
self.textLabel.text = [_domElement valueForKey:@"nodeValue"];
62+
}
63+
else
64+
{
65+
self.accessoryType = UITableViewCellAccessoryNone;
66+
self.textLabel.text = @"";
67+
}
68+
69+
self.textLabel.font = [UIFont systemFontOfSize:15.0];
70+
}
71+
72+
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
73+
{
74+
[super setSelected:selected animated:animated];
75+
76+
// Configure the view for the selected state
77+
if (selected)
78+
{
79+
[_domElement setValue:@"1px solid blue" forKeyPath:@"style.border"];
80+
}
81+
else
82+
{
83+
[_domElement setValue:@"0px" forKeyPath:@"style.border"];
84+
}
85+
}
86+
87+
@end

Samples/WebView Explorer/RootViewController.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,29 @@
1212

1313
@interface RootViewController : UITableViewController
1414
{
15-
id _domElement;
16-
id _childNodes;
15+
id _document;
16+
NSArray* _elements;
1717
}
1818

1919
@property (nonatomic, retain) IBOutlet DetailViewController* detailViewController;
2020

21-
@property (nonatomic, retain) id domElement;
21+
@property (nonatomic, retain) id document;
22+
23+
- (void)setRootNode:(id)rootNode;
2224

2325
@end
26+
27+
#pragma mark -
28+
29+
@interface DOMTraversal : NSObject
30+
{
31+
32+
}
33+
34+
- (id)createTreeWalker:(id)root whatToShow:(NSInteger)whatToShow;
35+
36+
- (id)firstChild;
37+
38+
- (id)nextSibling;
39+
40+
@end

0 commit comments

Comments
 (0)