2011-01-10 18 views

Respuesta

63

El siguiente código se carga un archivo HTML llamado index.html en su carpeta de proyecto:

[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]]; 
+0

La documentación de Apple sugiere que usar este método para cargar HTML local no es seguro: '' 'Para ayudarlo a evitar ser vulnerable a ataques de seguridad, asegúrese de usar este método para cargar archivos HTML locales; no use loadRequest: .''' He grabado su sugerencia ('loadHTMLString: baseURL:') en mi respuesta a continuación. –

+0

Sí, no dijo eso hace 5 años cuando publiqué esta respuesta. Siempre es bueno leer la documentación. Eliminaría esto, pero es la respuesta aceptada y obviamente ha ayudado a mucha gente. –

28

Cody Gray es correcto, pero también hay de esta manera:

// Load the html as a string from the file system 
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 

// Tell the web view to load it 
[WebView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]]; 

Esto es útil si necesita edite el html antes de cargarlo.

+0

Tiene razón, pero pasaría '[NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]]' en lugar de 'nil' para' baseURL'. – Jilouc

+0

Ah sí, no recordaba haber cargado otros recursos del html. He editado mi respuesta, ¡gracias! – deanWombourne

+0

Pasaría [[NSBundle mainBundle] bundleUrl] –

2

Mediante el uso de código siguiente se puede cargar un archivo HTML en una red WebView.here es vista web obj e inde

Web.delegate = self; 

[Web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]]; 
5

Swift

guard let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") else { 
     return 
    } 

    let url = NSURL(fileURLWithPath: path) 
    self.webview.loadRequest(NSURLRequest(URL:url)) 
0

Swift Versión 2.1

// load html to String with Encoding 
let path = NSBundle.mainBundle().pathForResource("policy", ofType: "html") 
do { 
    let fileHtml = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding) 
    webView.loadHTMLString(fileHtml as String, baseURL: nil) 
} 
catch { 

} 
0

Apple d OCUMENTACIÓN sugiere el uso de loadHTMLString:baseURL::

Utilice la loadHTMLString: URLbase: método para empezar a cargar los archivos HTML locales o el loadRequest: Método para comenzar la carga de contenidos web. Utilice el método stopLoading para detener la carga y la propiedad de carga para averiguar si una vista web está en proceso de carga.

La documentación loadHTMLString:baseURL: ofrece aún más el razonamiento:

Para ayudar a evitar ser vulnerables a ataques de seguridad, asegúrese de usar este método para cargar archivos HTML locales; no use loadRequest :.

Ésta podría ayudar: https://stackoverflow.com/a/29741277

0
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"privacy-Policy" ofType:@"html" inDirectory:nil]; 
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile]; 
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]]; 
Cuestiones relacionadas