开发者

WebView Insert / Modify Content dynamically

开发者 https://www.devze.com 2023-02-17 02:26 出处:网络
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,

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 get info from network开发者_开发百科 and depending upon them i need to set the style/font/attribute or probably i need to append the new text when the connected device is not responding,

So far i am using following code,

-(void)modifyString:(NSString *)string{
   [sourceString stringByAppendingString:errorString :string] 
}

   -(void)reloadPage{

     [[pWebView mainFrame] loadHTMLString:htmlString baseURL:nil];
    }

I don’t think its correct way to implement it, i am trying to make use of

[pWebView replaceSelectionWithMarkupString:@"<html><body><p>Hi there </p></br></body></html>”];

but nothing is displaying because, i have not selected and to select my question is How can i set the selection ?

Kind regards

Rohan


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

精彩评论

暂无评论...
验证码 换一张
取 消