Domanda

Sto cercando di usare una delle nuove funzionalità di IOS7, il framework JavaScriptCore.Posso produrre con successo una stringa di HelloWorld da JavaScript, ma a cosa mi interessa, sta facendo i messaggi HTTP in JavaScript e quindi passare la risposta a Objective-C.Sfortunatamente, quando sto creando un oggetto XMLHttpRequest in JavaScript, ottengo EXC_BAD_ACCESS (code=1, address=....).

Ecco il codice 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";
};
.

Ecco il codice Obiettivo-C all'interno del mio 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:@[]];
}
.

Potrebbe essere che la funzionalità delle richieste HTTP non venga fornita in JavaScriptCore Framework?Se sì, potrei superare questo usando UIWebViewTagCode -stringByEvaluatingJavaScriptFromString:?Cosa succede se ho compilato e incluso nel mio progetto un altro motore JavaScript (E.G. V8)?

È stato utile?

Soluzione

La mia ipotesi sarebbe che le richieste HTTP non fanno parte del Core JavaScript, poiché è davvero parte del browser, non il linguaggio JavaScript.
Assumerei che JavaScript Core includa solo ciò che è nella definizione di ECmascript.

Se vuoi Ajax, allora WebView è il modo di andare.

Altri suggerimenti

XMLHTTPRequest è, come affermato prima, non parte di JavaScript, ma puoi ancora avvolgere l'URLrequest iOS quindi è disponibile nel tuo 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]];
        } 
    }];
}
.

Avanti, vincola l'istanza al jscontext da qualche parte nel tuo codice

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

Dal tuo file JS, puoi ora chiamare

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

Potresti anche usare un blocco e legarlo direttamente dal jscontext per ottenere lo stesso risultato.

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);
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top