3
votes

As far as I understand, the standard UIWebView is able to display local files of numerous extensions. Since I need to display formatted text in my application, I thought this method is the way to go.

So, I created a rich text file, called test.rtf, and I am currently trying to display it in my webview. However, I am unsuccessful. I see only a blank white screen instead of the text I typed into the rtf file.

Can someone look at my code and point out where I went wrong with the implementation?

Header file:

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController <UIWebViewDelegate> {
UIWebView * webView;
}
@end

Main file:

#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController

- (id)init
{
self = [super init];
if (self) {
    // Customization at initialization
    self.view.backgroundColor = [UIColor clearColor];

    //Adding a webview to display richtext documents
    int webHeight = self.view.bounds.size.height;
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 150.0, 320.0, webHeight-150.0)];
    webView.dataDetectorTypes = NO;
    webView.scrollView.bounces = NO;
    webView.delegate = self;
    [webView setBackgroundColor:[UIColor clearColor]];
    [webView setOpaque: NO];
    [self.view addSubview:webView];
    }
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

// Accessing richtext file
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"rtf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
if (request) {
    // Additional configuration
}

//Adding the content of the webiew
[webView loadRequest:request];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
  • (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"delegate called"); // Do whatever you want here return YES; } @end
1
I created the rtf with Xcode; so, it is properly added to my application and shows up in the Project Navigator as expected.Gergely Kovacs
Have you returned YES in your web view's delegate?sunkehappy
I did not create a webview delegate, nor did I link any existing file...Gergely Kovacs
@property (strong, nonatomic) UIWebView *webView; Try this.sunkehappy
Nope, didn't work. But you know what? Don't worry about it. I'll accept your answer and try to figure out something else. :)Gergely Kovacs

1 Answers

3
votes

You should set your UIWebView's delegate and return YES. UIWebViewDelegate Protocol Reference says:

webView:shouldStartLoadWithRequest:navigationType:

Sent before a web view begins loading a frame.

So you should return YES for the first time to load your test.rtf file.

As you have updated your question. You need to set

webView.delegate = self;
//webView.delegate = appDelegate;    // not this line