真的就是亂寫用的筆記

If you are working on a controller object which owns a WebView as its user interface, or a WebKit plug-in, you can use Javascript to call the methods of your controller object or plug-in. The Javascript-Objective C bridge can help you to convert the Javascript string to NSString, and convert Javascript number to NSNumber. However, if you pass a array from your Javascript code to your Objective C objects, the array will not be convert to NSArray, its class remains WebScriptObject.

If we want to obtain the content of a Javascript array, we have to use the interface of WebScriptObject. Suppose we have a Javascript array which contains various strings, we may try to get each element in the array by using the following code:

WebScriptObject *obj = (WebScriptObject *)JSArray;

NSUInteger count = [[obj valueForKey:@"length"] integerValue];
NSMutableArray *a = [NSMutableArray array];
for (NSUInteger i = 0; i < count; i++) {
	NSString *item = [obj webScriptValueAtIndex:i];
	if ([item isKindOfClass:[NSString class]]) {
		[a addObject:item];
	}
}
1 year ago
  1. pgtwitter reblogged this from zonble
  2. digdog said: 所以到頭來還是得用 HTML 當頁面的樣板比較有彈性,對吧?
  3. zonble posted this