HTTP request via JavaScriptCore in iOS7

I am trying to use one of the new iOS7 features - JavaScriptCore Framework. I can successfully output the helloWorld string from Javascript, but what interests me is doing the HTTP POST in Javascript and then passing the response to Objective-C. Unfortunately, when I create an XMLHttpRequest object in Javascript, I get EXC_BAD_ACCESS (code=1, address=....) .

Here is the Javascript code ( 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"; }; 

Here is the Objective-C code inside my 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:@[]]; } 

Could it be that the HTTP request function is not provided in the JavaScriptCore Framework? If so, can I overcome this using UIWebView -stringByEvaluatingJavaScriptFromString: :? What if I compiled and included another Javascript Engine (for example, V8) in my project?

+6
source share
3 answers

I assume that HTTP requests are not part of the JavaScript Core, as it is really part of the browser, not the JavaScript language.
I would suggest that the core of JavaScript includes only that in the ECMAScript definition.

If you want AJAX, then WebView is the way to go.

+4
source

XMLHttpRequest, as mentioned above, is not part of JavaScript, but you can still wrap the iOS URLRequest so that it is available in your JS.

in JSUtils.h

  @protocol UtilsExport; @interface JSUtils : NSObject <UtilsExport> @end @protocol UtilsExport <JSExport> - (void)get:(NSString *)url then:(JSValue *)jsCallback; @end 

in 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]]; } }]; } 

Then bind the instance to JSContext somewhere in your code

 JSContext *context = [[JSContext alloc] init]; context[@"utils"] = [[JSUtils alloc] init]; 

from your js file you can call

 utils.getThen('http://localhost/api/dashboard', function(resultString){ console.log(resultString) } 

You can also use the block and bind it directly to JSContext to get the same result.

+19
source
 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; }; 

In JS:

 var body = request(url); 
0
source

Source: https://habr.com/ru/post/958324/


All Articles