Search code examples
iosobjective-caccess-token

ios - How to invoke javascript in ios?


I want to check the JSON URL after logged into Gmail or Yahoo,but the condition fails,i'm trying the below code.

How to declare that javascript,am I declaring the correct way?

I'm following this link,Please go through it http://www.stevesaxon.me/posts/2011/window-external-notify-in-ios-uiwebview/

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType

{   
    //_url = [[NSURL alloc] initWithString:websiteUrl];
    _url = [[NSURL alloc] initWithString:@"JSON URL"];
    if(_url)
    {

        if([_url isEqual:[request URL]])
        {
            return YES;
        }

        [_url release];
    }

    _url = [[request URL] retain];
    NSString* scheme = [_url scheme];

    //Condition fails here after logged in

    if([scheme isEqualToString:@"acs"])
    {
        // parse the JSON URL parameter into a dictionary
        NSDictionary* pairs = [self parsePairs:[_url absoluteString]];
        if(pairs)
        {
            WACloudAccessToken* accessToken;
            accessToken = [[WACloudAccessToken alloc] initWithDictionary:pairs];
            [WACloudAccessControlClient setToken:accessToken];

            [self dismissModalViewControllerAnimated:YES];
        }

        return NO;
    }

    [NSURLConnection connectionWithRequest:request delegate:self];

    return NO;

}



- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{
    if(_data)
    {
        NSString* content = [[NSString alloc] initWithData:_data
                                                  encoding:NSUTF8StringEncoding];

        [_data release];
        _data = nil;

        NSString *jsString = @"window.external =\
        {\
        'Notify': function(s) { document.location = 'acs://settoken?token=' + s; },\
        'notify': function(s) { document.location = 'acs://settoken?token=' + s; }\
        }";

        content = [jsString stringByAppendingString:content];

        //NSURL *url = [[NSURL alloc] initWithString:websiteUrl];
        NSURL *url = [[NSURL alloc] initWithString:@"JSON URL"];
        [webView loadHTMLString:content baseURL:url];
    }

}

Solution

  •         NSString *jsString = @"window.external =\
            {\
            'Notify': function(s) { document.location = 'acs://settoken?token=' + s; },\
            'notify': function(s) { document.location = 'acs://settoken?token=' + s; }\
            }";
    

    should be:

            NSString *jsString = @"<script type='text/javascript'>\
            window.external =\
            {\
            'Notify': function(s) { document.location = 'acs://settoken?token=' + s; },\
            'notify': function(s) { document.location = 'acs://settoken?token=' + s; }\
            }\
            </script>";
    

    Btw, a cursory glance shows some problems with your memory management (_url is leaking , content is leaking). If you're unsure of how to handle manual memory management, I would highly recommend turning on ARC (Automatic Reference Counting).