简体   繁体   中英

Webview displaying one HTML file in iPhone sdk

I have three HTML files in my local resources bundle. I need to display it as a reader. So i used a web view to display that using following code,

- (void)viewDidLoad
{
     [super viewDidLoad];
     totalArray=[[NSMutableArray alloc]init];
     [totalArray addObject:@"file1"];
[totalArray addObject:@"file2"];
[totalArray addObject:@"file3"];
    NSLog(@"totalArray count:%d",[totalArray count]);

    for (int i=0;i<3;i++)
{
    NSLog(@"i count:%d",i);

   NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
       NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];
       NSLog(@"webview %@", bundlePath);
   NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:i] ofType:@"html"];
       NSLog(@"filePath1:%@",filePath1);
       [htmlView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath1]]];
    }
}

Am getting the total count as 3, current index counts and file path. But still it displays only my first html file. My other two files were missing. What could be the problem here? Kindly help me. Thanking you.

For the local file urls you have to use fileURLWithPath: method of NSURL . Also, you could get file url from the bundle with [[NSBundle mainBundle] URLForResource: withExtension:] method. Further more, I don't think that loading urls in the loop is a good idea, you should try something else like loading url on button action. Good Luck!

You need wait till the file are loaded , looping is not god idea , instead you can achieve same using below code. Use fileURLWithPath to load local file.

- (void)viewDidLoad
{
    [super viewDidLoad];
    count = 0;
    totalArray=[[NSMutableArray alloc]init];
    [totalArray addObject:@"file1"];
    [totalArray addObject:@"file2"];
    [totalArray addObject:@"file3"];      
    [_htmlView setDelegate:self];
    NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:count] ofType:@"html"];
    NSURL *url = [NSURL fileURLWithPath:filePath1];
    [_htmlView loadRequest:[NSURLRequest requestWithURL:url]];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    count++;
    if (count < 3) {

        NSString *filePath1= [[NSBundle mainBundle] pathForResource:[totalArray objectAtIndex:count]
                                                             ofType:@"html"];
        NSLog(@"filePath1:%@",filePath1);
        NSURL *url = [NSURL fileURLWithPath:filePath1];
        [_htmlView loadRequest:[NSURLRequest requestWithURL:url]];
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM