load local html into UIWebView

Discussion in 'iOS Programming' started by knut, Jul 7, 2009.

  1. knut macrumors newbie

    Joined:
    Jun 30, 2009
    #1
    Hi all
    I have local html file wich is located in project folder
    I want load local html
    I add this code in applicationDidFinishLaunching
    Code:
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"] isDirectory:NO]]];
    
    but I get exception
    can you explain me why ? I get the directory dynamically why it is the nil?
    Thanks
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Try using loadData:MIMEType:textEncodingName:baseURL: or loadHTMLString:baseURL: instead.
     
  3. knut thread starter macrumors newbie

    Joined:
    Jun 30, 2009
    #3
    thanks for reply
    if I understand you correctly do

    Code:
     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"htm"];    
     NSData *htmlData = [NSData dataWithContentsOfFile:filePath];    
     if (htmlData) {    
     [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];    
     }  
    
    but what about baseURL:[NSURL URLWithString:mad:"????????"] my file is the local
    ? correct me please
    thanks
     
  4. knut thread starter macrumors newbie

    Joined:
    Jun 30, 2009
    #4
    one more question too
    when i use such method for loading my local html
    Code:
    
    NSString *savedUrlString = [item objectForKey: @"itemUrl"];
    NSString *tempUrlString = savedUrlString;
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:tempUrlString ofType:@"html"];
    NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
    NSString *imagePath = [[NSBundle mainBundle] resourcePath];
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",imagePath]]];
    
    on first line i got the error
    what means the first line and what type has the item ?
    Thanks
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Sounds like you are just copying and pasting code without understanding what it does. I suggest you step back from the real coding and go review the basics of Objective-C coding before you continue.
     
  6. knut thread starter macrumors newbie

    Joined:
    Jun 30, 2009
    #6
    Thanks it was copy past but before copy paste everything was understand for me except how get the path but now I solve this problem for me.
    The question is close.
    Thanks for reply
     

Share This Page