iPhone: Evite el redireccionamiento de JavaScript

Using the 2nd solution here: Call Objective C method from JavaScript with parameter, I have added the following line to some of my JavaScript code in order to pass some information back into Objective-C:

location.href = 'myApp://index=10';

The problem is, when I do this, the rest of the JavaScript on the page (after the location.href=... line) does not execute, and the browser is redirected to a blank page. Is there any way to execute that line without having the browser go to a blank page in order to do so?

I appreciate any help, please let me know if I can clarify at all. Thank you.

EDIT: Objective-C code:

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL] ;
    NSString *url2 = [[request URL] absoluteString];


    static NSString *urlPrefix = @"myApp://";
    if ([url2 hasPrefix:urlPrefix]) {
        NSString *paramsString = [url2 substringFromIndex:[urlPrefix length]];
        NSArray *paramsArray = [paramsString componentsSeparatedByString:@"&"];
        int paramsAmount = [paramsArray count];

        for (int i = 0; i < paramsAmount; i++) {
            NSArray *keyValuePair = [[paramsArray objectAtIndex:i] componentsSeparatedByString:@"="];
            NSString *key = [keyValuePair objectAtIndex:0];
            NSString *value = nil;
            if ([keyValuePair count] > 1) {
                value = [keyValuePair objectAtIndex:1];
            }

            if (key && [key length] > 0) {
                if (value && [value length] > 0) {
                    if ([key isEqualToString:@"index"]) {
                        NSLog(@"Works");
                        exit;
                    }
                }
            }
        }
        return NO;
    }


    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
            return YES; 
    }

    else {
       return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }

}

preguntado el 30 de enero de 12 a las 19:01

2 Respuestas

Establecer un href valor de la location object will always immediately result in a JavaScript redirect. That is, in fact, the main purpose of allowing the object to be writable.

If you're looking to update a URL fragment without a redirect or refresh try using the location.hash atributo.

Respondido el 31 de enero de 12 a las 00:01

Thanks for the reply. Unfortunately, when I try this, 'myApp://index=10'; doesn't even seem to get dealt with in AppDelegate.m. - Objc55

Attempt 1: Did you try setting window.location en lugar de location.href?

Attempt 2: Did you return NO in order to cancel the request on the Objective-C method?

Respondido el 31 de enero de 12 a las 00:01

Thanks for the reply, though unfortunately the problem persists. - Objc55

I believe I did return NO, but I've added my code to my initial post in case maybe I'm doing it incorrectly? Thanks. - Objc55

Try not doing it that complicated at first. Just check whether or not the scheme matches yours: if so, return NO and log something; if not, return YES. - Christian Schnorr

Well, it does get the data correctly, so I know it's getting the information from the redirect, yet even though I return NO, it still seems to redirect. - Objc55

For some reason, it seems like it's still redirecting even when I return no? - Objc55

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.