iPhone Dev: UIWebView baseUrl to resources in Documents folder not App bundle

后端 未结 3 1931
-上瘾入骨i
-上瘾入骨i 2020-12-01 04:52

Greetings! Can anyone please kindly assist me finding a way around the following:

  • load an html into a UIWebView using loadHTMLString and incl
相关标签:
3条回答
  • 2020-12-01 05:16

    To get the path to the document directory:

    NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"index.xml"];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]]
    

    The easiest way to have everything working, is to simply load your index.html from the document directory instead of loading a string. Otherwise set the baseUrl to the document directory.

    0 讨论(0)
  • 2020-12-01 05:30
     // call this method and set the directory , Currently it is cache directory . If you want document directory then change NSCacheDirectory to NSDocumentDirectory
    
    -(NSString*)getBasePath
    {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
                                                         NSUserDomainMask, YES);
    NSMutableString *cacheDirectory = [NSMutableString stringWithString:[paths objectAtIndex:0]];
    
    [cacheDirectory appendString:@"/"];
    NSRange renge= NSMakeRange(0, cacheDirectory.length);
    [cacheDirectory replaceOccurrencesOfString:@"/" withString:@"//" options:NSCaseInsensitiveSearch range:renge];
    renge= NSMakeRange(0, cacheDirectory.length);
    [cacheDirectory replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:renge];
    [cacheDirectory insertString:@"file://" atIndex:0];
    //file:////Users//hb//Library//Application%20Support//iPhone%20Simulator//6.0//Applications//3A141D33-390A-4DD2-8825-7DDC07D29894//Library//Caches//
    return cacheDirectory;
    

    }

    0 讨论(0)
  • 2020-12-01 05:32

    After some googling I have finally found a fitting solution for the question I posed. Hopefully this would help those who face the same problem.

    The trick is with the formatting of the string path when creating an NSURL object for baseURL of a UIWebView. Although usually I use the typical "Users/...../dir/file" in most cases, loading using UIWebView's loadHTMLString:baseURL needs a different approach.

    As described in http://dblog.com.au/iphone-development/loading-local-files-into-uiwebview/, where I got the solution, string path to the resources just needs to have slashes to be replaced with double-slashes and spaces with %20:

    NSString *imagePath = [[NSBundle mainBundle] resourcePath];
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
    imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    
    NSString *HTMLData = @"
    <h1>Hello this is a test</h1>
    <img src="sample.jpg" alt="" width="100" height="100" />";
    [webView loadHTMLString:HTMLData baseURL:
               [NSURL URLWithString: 
               [NSString stringWithFormat:@"file:/%@//",imagePath]
               ]];
    

    Do take note of the replacing of the strings and also the:

    [NSString stringWithFormat:@"file:/%@//",imagePath]
    

    Although the example code above is retrieving the path to the mainBundle of the application, it can also work in other folders, ie Documents(and its subfolders) as I did in mine.

    Kind regards, oonoo

    PS Thanks again Nic for the reply :)

    0 讨论(0)
提交回复
热议问题