如果可能,那么我如何在我的代码中使用JavaScript?是否可以在Objective-C(iphone)中使用JavaScript?
1
A
回答
3
可以在UIWebView中注入JavaScript。
NSString * result = [myUIWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('foo').className"];
1
为了增加j_freyre的回答,这表明你如何出来的Javascript从目标C执行并获取数据,也可以从JavaScript调用在一个UIWebView运行目标C代码。这需要多一点工作,并且有点冒险。
在JavaScript端:
var call_stack = new Array(); //This will hold our Objective C function calls
function addWithObjC(a,b){
call_stack.splice(0,0,"add:"+a+":"+b); //We separate the components of the call (function name, parameters) by a colon, but it could be anything.
window.location.href = "::ObjCcall::";
}
function multiplyWithObjC(a,b){
call_stack.splice(0,0,"multiply:"+a+":"+b);
window.location.href = "::ObjCcall::";
}
这将尝试加载一个虚假的URL。 “:: ObjCcall ::”可以是任何不是可能有效的url。现在的关键是要赶上目的C.申请假设你有一个UIWebView和相应的UIViewController,在UIViewController接口注明:
@interface MyWebViewController : UIViewController <UIWebViewDelegate>{
//declare stuff
}
在实现方面,地方:
[(UIWebView *)self.view setDelegate:self];
你然后通过执行以下操作来捕获请求:
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *url = [[request URL] absoluteString]; //Our potentially bogus request url
if([url hasSuffix:@"::ObjCcall::"]){ //We're trying to call Objective C code
NSString *command; //Our specially formatted function call
//While our call stack is not empty...
while(![command = [webView stringByEvaluatingJavaScriptFromString:@"call_stack.pop()"]isEqualToString:@""]){
//Break the function call into its compoments
NSArray *segments = [command componentsSeparatedByString:@":"];
//We found a call we respond to
if(([[segments objectAtIndex:0] isEqualToString:@"add"])&&([segments count] > 2)){
//Here is where the Objective C action takes place
float result = [[segments objectAtIndex:1]floatValue] + [[segments objectAtIndex:2]floatValue];
//Do something with the results...
}
else if(([[segments objectAtIndex:0] isEqualToString:@"multiply"])&&([segments count] > 2)){
//Another function
float result = [[segments objectAtIndex:1]floatValue] * [[segments objectAtIndex:2]floatValue];
}
//Add more checks as needed
[segments release];
return NO; //This tells the UIWebView to not try to load the page
}
}
else{
return YES; //This is a valid URL request, load the page
}
}
这比看起来似乎有点复杂一点。但是,UIWebView并不喜欢连续快速更改window.location.href。我们实现一个调用堆栈以确保没有呼叫被跳过。
设置比Objective C中的Javascript更多的工作,当然,一旦你设置了样板,你需要做的就是在shouldStartLoadWithRequest中的while循环中添加检查。
相关问题
- 1. iPhone是否“可以使用”?
- 2. 是否可以使用JavaScript
- 3. 是否可以使用JavaScript
- 4. 您是否可以在iphone中使用iPhone日历数据?
- 5. 是否可以在iPhone中屏蔽CALayer?
- 6. 是否可以为iphone
- 7. JavaScript是否可以在UIWebView中运行?
- 8. 是否可以从JavaScript内禁用JavaScript?
- 9. 是否可以在ASP中使用V8?
- 10. 是否可以在CTE中使用Declare?
- 11. 是否可以在C#中使用IGetActivationFactory.GetActivationFactory?
- 12. 是否可以在PyGame中使用PyPy?
- 13. 是否可以在SKLabelNode中使用NSMutableAttributedString?
- 14. 是否可以在backbone.js中使用ExtJS?
- 15. 是否可以在HTMLService中使用localStorage?
- 16. 是否可以在XNA中使用JSON
- 17. 是否可以在cocos2d中使用UIPickerView?
- 18. 是否可以在re.search中使用'和'?
- 19. 是否可以在PostgreSQL中使用LinqPad?
- 20. 是否可以在WHERE中使用CASE?
- 21. 是否可以在MemSQL中使用EntityFramework?
- 22. 是否可以在DevForce2010中使用BreezeJS?
- 23. 是否可以在UMLdesigner中使用git?
- 24. 是否可以在OSMDroid中使用MapCSS?
- 25. 是否可以在UIWebView中使用iframe?
- 26. 是否可以在MySQL中使用Subtable?
- 27. 在C#中ID是否可以使用?
- 28. 是否可以在PHP中使用{}?
- 29. 是否可以在Grails中使用proguard
- 30. 是否可以在nhibernate中使用automapper?
通过在Objective-C代码中使用JavaScript,究竟是什么意思?他们完全是两种不同的语言;我能想到的唯一地方就是网络视图。 – BoltClock 2011-03-18 07:07:14