WebView Insert / Modify Content dynamically

折月煮酒 提交于 2019-11-30 16:13:05

Never Mind Solved it by this way
In AwakeFromNib Method, added following code,

-(void)awakeFromNib{

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"about:blank"]];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //Load the request in the UIWebView.
    [[pWebView mainFrame ]loadRequest:requestObj];
    [pWebView setEditable:YES];
    [pWebView setNeedsDisplay:YES];

}

and added this function to append the body element,

-(void)appendTagToBody:(NSString *)tagName InnerHTML:(NSString *)innerHTML
{
    // Gets a list of all <body></body> nodes.
    DOMNodeList *bodyNodeList = [[[pWebView mainFrame] DOMDocument] getElementsByTagName:@"body"];

    // There should be just one in valid HTML, so get the first DOMElement.
    DOMHTMLElement *bodyNode = (DOMHTMLElement *) [bodyNodeList item:0];

    // Create a new element, with a tag name.
    DOMHTMLElement *newNode = (DOMHTMLElement *) [[[pWebView mainFrame] DOMDocument] createElement:tagName];

    // Add the innerHTML for the new element.
    [newNode setInnerHTML:innerHTML];

    // Add the new element to the bodyNode as the last child.
    [bodyNode appendChild:newNode];
}

and whenever wanted to change the content,

-(void)appendString:(NSString *)pString{
    [self appendTagToBody:@"div" InnerHTML:@"<div><p> Hi there </p></div>"];
    [self setNeedsDisplay:YES];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!