WebView Insert / Modify Content dynamically

后端 未结 1 1449
迷失自我
迷失自我 2021-01-03 13:38

In my application, i am using WebView to display the content, Now is it possible to modify the content dynamically, the requirement is something like this,

i will g

相关标签:
1条回答
  • 2021-01-03 13:48

    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];
    }
    
    0 讨论(0)
提交回复
热议问题