Embed video in html file loaded in UIWebView from Documents folder of application

后端 未结 5 2091
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-13 10:23

I have an html file named videoplay.html with following content




    Title         


        
5条回答
  •  春和景丽
    2021-02-13 11:06

    I had a similar issue. An html file called index.html in a sub-folder of the Documents directory called assets.


    Documents/assets/index.html

     
     
    
     
     
     
    
     
     
          ...
     
     
    

    Note that it's referencing other files in the assets directory. Namely, bundle.js and styles.css.


    The following variation on the accepted answer's code example allowed the UIWebView to load index.html and have the js and css files it referenced still work:

     NSArray *documentsPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
    
     NSString *assetsPath = [[documentsPath firstObject] stringByAppendingPathComponent:@"assets"];
    
     NSString *indexHTMLFilePath = [assetsPath stringByAppendingPathComponent:@"index.html"];
    
     NSString *encodedAssetsPath = [assetsPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
     NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"file:///%@/", encodedAssetsPath]];
    
     NSData *indexHTMLFileData = [[NSFileManager defaultManager] contentsAtPath: indexHTMLFilePath];
    
     [self.webView loadData:indexHTMLFileData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:baseURL];
    

提交回复
热议问题