【问题标题】:ios - How to append javascript with HTML content?ios - 如何在 javascript 中附加 HTML 内容?
【发布时间】:2013-05-07 11:46:47
【问题描述】:

我正在尝试获取访问令牌,我正在关注this 链接以获取该令牌,但某些条件失败,我认为我没有正确地在 javascript 中附加 HTML 内容。

- (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;

}

有什么想法吗?提前致谢。

【问题讨论】:

    标签: iphone ios objective-c uiwebviewdelegate


    【解决方案1】:

    我不确定我是否完全理解你想要做什么,但我假设

    [UIWebView stringByEvaluatingJavaScriptFromString:@"yourJavaString"];
    

    可以提供帮助。我在我的一个项目中使用它来执行基于 JavaScript 的登录机制。

    请参阅 Apple 文档 here

    您必须在 WebView 加载后使用 stringByEvaluatingJavaScriptFromString。

    【讨论】:

      猜你喜欢
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2017-08-24
      • 1970-01-01
      相关资源
      最近更新 更多