IOS7中通过JavaScriptCore的HTTP请求
-
27-12-2019 - |
题
我正在尝试使用Ios7新功能之一,JavaScriptCore框架。我可以成功地从Javascript输出一个helloWorld字符串,但我感兴趣的是在Javascript中执行HTTP帖子,然后将响应传递给Objective-C。不幸的是,当我创建一个 XMLHttpRequest
对象在Javascript中,我得到 EXC_BAD_ACCESS (code=1, address=....)
.
这里是Javascript代码(hello.js
):
var sendSamplePost = function () {
// when the following line is commented, everything works,
// if not, I get EXC_BAD_ACCESS (code=1, address=....)
var xmlHttp = new XMLHttpRequest();
};
var sayHello = function (name) {
return "Hello " + name + " from Javascript";
};
下面是我的目标C代码 ViewController
:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
JSContext *context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]];
NSString *scriptPath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"js"];
NSLog(@"scriptPath: %@", scriptPath);
NSString *script = [NSString stringWithContentsOfFile:scriptPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"script: %@", script);
[context evaluateScript:script];
JSValue *sayHelloFunction = context[@"sayHello"];
JSValue *returnedValue = [sayHelloFunction callWithArguments:@[@"iOS"]];
// this works!
self.label.text = [returnedValue toString];
JSValue *sendSamplePostFunction = context[@"sendSamplePost"];
// this doesn't work :(
[sendSamplePostFunction callWithArguments:@[]];
}
可能是JavaScriptCore框架中没有提供HTTP请求功能?如果是的话,我可以通过使用来克服这一点吗 UIWebView
'的 -stringByEvaluatingJavaScriptFromString:
?如果我编译并包含在我的项目中的另一个Javascript引擎(例如V8)?
解决方案
我的猜测是HTTP请求不是JavaScript核心的一部分,因为它实际上是浏览器的一部分,而不是JavaScript语言。
我会假设JavaScript核心只包含ECMAScript定义中的内容。
如果你想要AJAX,那么WebView就是要走的路。
其他提示
如前所述,XMLHttpRequest不是JavaScript的一部分,但您仍然可以包装iOS URLRequest,以便在JS中可用。
在JSUtils中。h
@protocol UtilsExport;
@interface JSUtils : NSObject <UtilsExport>
@end
@protocol UtilsExport <JSExport>
- (void)get:(NSString *)url then:(JSValue *)jsCallback;
@end
在JSUtils中。m
#import "JSUtils.h"
- (void)get:(NSString *)url then:(JSValue *)callback {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
[request setHTTPMethod:@"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] > 0 && error == nil) {
[callback callWithArguments:@[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], @YES]];
}
}];
}
接下来,将实例绑定到代码中的某个位置的JSContext
JSContext *context = [[JSContext alloc] init];
context[@"utils"] = [[JSUtils alloc] init];
从你的JS文件,你现在可以调用
utils.getThen('http://localhost/api/dashboard', function(resultString){
console.log(resultString)
}
您也可以使用块并将其直接绑定到JSContext以获得相同的结果。
JSContext *context = [[JSContext alloc] init];
context[@"request"] = ^(NSString *url) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
NSURLResponse *response = nil;
[request setHTTPMethod:@"GET"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return body;
};
在JS:
var body = request(url);
不隶属于 StackOverflow