ios - How to append javascript with HTML content?

混江龙づ霸主 提交于 2019-12-24 07:17:04

问题


I'm trying to get access token,I'm following this link to get that,but some condition fails,I think am not properly append the javascript with HTML content.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(_data)
{
   NSString* content = [[NSString alloc] initWithData:_data
                                              encoding:NSUTF8StringEncoding];       
    [_data release];
    _data = nil;        
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>";

content = [jsString stringByAppendingString:content];                  
[webView loadHTMLString:content baseURL:_url];
}
}

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if(_url)
{           
    if([_url isEqual:[request URL]])
    {
        return YES;
    }       
    [_url release];
}
     //Here am getting http://LoginSuccess.aspx
_url = [[request URL] retain];
NSString* scheme = [_url scheme];//Here am getting http

//So here condition fails

if([scheme isEqualToString:@"acs"])
{
    // parse the JSON URL parameter into a dictionary
    _url = [NSURL URLWithString:@"https://converse.accesscontrol.windows"];
    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;

}

Any ideas? Thanks in advance.


回答1:


I´m not sure if i unterstand exactly what you´re trying to do, but I assume

[UIWebView stringByEvaluatingJavaScriptFromString:@"yourJavaString"];

could help. I use it to perform an JavaScript based Loginmechanism in one of my projects.

See the Apple doc here .

You have to use stringByEvaluatingJavaScriptFromString AFTER WebView has loaded.



来源:https://stackoverflow.com/questions/16418213/ios-how-to-append-javascript-with-html-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!