Locate a html file in UIWebView

2019-08-16 08:44发布

I have a NotificationVC containing table view having 10 cells index 0 to 9. on click of each cell NotificationWebVC should open up that has a UIWebView that loads diff. html depending upon which cell is selected in NotificationVC. Right now my code works only for index=0 , but not for other indexes , seems bit weird.

NotificationWebVC
- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:@"html" inDirectory:nil] ;

    NSURL *url = [NSURL fileURLWithPath:htmlFile];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    _webView.delegate=(id)self;


}

Please suggest. _resourceName is a property having the name of html file.

4条回答
神经病院院长
2楼-- · 2019-08-16 09:22
// docfileName should be your file name call this method in tableview delegate didSelectrow

-(void)loadLocaldataonWebview :(NSString *)docfileName 
{
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:docfileName ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[documentsWebView loadHTMLString:htmlString baseURL:nil];
}

then in viewDidload add the following code

-(void)viewDidLoad{
documentsWebView=[[UIWebView alloc]init];
documentsWebView.delegate=self;
documentsWebView.frame=CGRectMake(0, yAxis, kWidth, 616);
documentsWebView.backgroundColor=[UIColor clearColor];
[self.view addSubview:documentsWebView];

}
查看更多
手持菜刀,她持情操
3楼-- · 2019-08-16 09:26

First you need to make an array containing your 10 urls. Then when a user tap on each cell, use the didSelectRowAtIndexPath delegate method.

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:[resources objectAtIndex:indexPath.row] ofType:@"html" inDirectory:nil] ;

    NSURL *url = [NSURL fileURLWithPath:htmlFile];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    _webView.delegate=(id)self;
}
查看更多
forever°为你锁心
4楼-- · 2019-08-16 09:27

Add line of code to NotificationWebVC.h

@property (nonatomic,retain)NSString *resopnseString;

then in .m class synthise it

@synthesize resopnseString;

after this go to didSelectMethod of tablevew datasouce in NotificationVC.m there you add the following code

NotificationWebVC *notif=[[NotificationWebVC alloc]init];
notif.resopnseString=[resources objectAtIndex:indexPath.row];
//here it may present view or push view or what ever it be

After doing all this now you shift to NotificationWebVC.m add the following code to in viewDidLoad method

 NSString *htmlFile = [[NSBundle mainBundle] pathForResource:resopnseString ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

after doing all this pl z be sure to add all 10 HTML file in your project and try to check that you are getting correct html file on select in table from

be sure you are passing correct name of file in responsString

 [resources objectAtIndex:indexPath.row];

or better put

 NSLog(@"%@",[resources objectAtIndex:indexPath.row]); 

to it check it.

i think this code will help you

查看更多
可以哭但决不认输i
5楼-- · 2019-08-16 09:32

try this code

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:_resourceName ofType:@"html" inDirectory:nil] ;

        [_webView loadHTMLString:htmlFile baseURL:nil];
        _webView.delegate=(id)self;
查看更多
登录 后发表回答