2
votes

I declared my App (a rich text editor) to be able to open any RTF file (coming, for example, from an email attachment). The application delegate sends a "loadContent: (NSURL *) url" message to the UiwebView controller when the user select the attachment. In the receiver, I get the url and load the RTF file in the UIWebView using

- (void) loadContent: (NSURL *) passedURL
{
    if (passedURL) { 
        [self.myWebView loadRequest:[NSURLRequest requestWithURL:passedURL]];
    } else {
        NSBundle *bundle = [NSBundle mainBundle];
        NSURL *indexFileURL = [bundle URLForResource:@"index" withExtension:@"html"];
        [self.myWebView loadRequest:[NSURLRequest requestWithURL:indexFileURL]];

    }
}

Doing that, everything goes all right, and "myWebView" displays the content of the RTF file. The problem is that this view should be content editable, and the code should contain some javascript that I have in my "index.html" bundled file. I load when starting application. So I added some code to extract the HTML code created from the RTF file to be able later to recreate a complete code, inserting the extracted code in a tag like that:

<div id="content" contenteditable="true"> imported html from rtf should be there</div>

I modified the loadContent method to get that.

- (void) loadContent: (NSURL *) passedURL
{
    if (passedURL) { 
        [self.myWebView loadRequest:[NSURLRequest requestWithURL:passedURL]];
        NSString *dum=[self.myWebView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML;"];
        NSLog(@"%@",dum);
    } else {
        NSBundle *bundle = [NSBundle mainBundle];
        NSURL *indexFileURL = [bundle URLForResource:@"index" withExtension:@"html"];
        [self.myWebView loadRequest:[NSURLRequest requestWithURL:indexFileURL]];

    }
}

Looking to the result of the NSLog, I get only the following html

<div id="content" contenteditable="true">Hello folks</div>

Which was the HTML content of myWebView BEFORE I loaded the rtf file...

Do you have any idea of what is going on? Why does dum displays the former code, as the displayed myWebView displays the new one? Is it because the loadRequest is executed in a separated thread and that when executing the next instruction, myWebView still contains the former code?

Thanks

[EDITED] I also tried to NSLog the content of the webView in webViewDidFinishLoad: delegate method, but get the same result: the content of this webView before I opened the RTF attachment file. Is It possible to make a load request in a synchronous way?

1

1 Answers

2
votes

This is because loadRequest is asynchronous. You need to wait for the load to finish before trying that NSLog. Take a look at the UIWebViewDelegate docs. Try that NSLog inside/after webViewDidFinishLoad: and you should get better results.