1.加载URL网页
- (
void
)loadWebPageWithString:(
NSString
*)URLString {
UIWebView *aWebView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
NSURLRequest *URLRequest = [[ NSURLRequest alloc ] initWithURL :[ NSURL URLWithString :URLString]];
UIWebView *aWebView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
NSURLRequest *URLRequest = [[ NSURLRequest alloc ] initWithURL :[ NSURL URLWithString :URLString]];
[aWebView loadRequest:URLRequest];
aWebView.
delegate
=
self
;
[
self
.
view
addSubview
:aWebView];
}
调用
2.加载本地的html
- (
void
)loadHTMLWithString {
UIWebView *aWebView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
NSString *htmlPath = [[ NSBundle mainBundle ] pathForResource : @"table" ofType : @"html" ];
NSString *htmlString = [ NSString stringWithContentsOfFile :htmlPath encoding : NSUTF8StringEncoding error : nil ];
[aWebView loadHTMLString :htmlString baseURL :[ NSURL URLWithString :htmlPath]];
aWebView. delegate = self ;
[ self . view addSubview :aWebView];
UIWebView *aWebView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
NSString *htmlPath = [[ NSBundle mainBundle ] pathForResource : @"table" ofType : @"html" ];
NSString *htmlString = [ NSString stringWithContentsOfFile :htmlPath encoding : NSUTF8StringEncoding error : nil ];
[aWebView loadHTMLString :htmlString baseURL :[ NSURL URLWithString :htmlPath]];
aWebView. delegate = self ;
[ self . view addSubview :aWebView];
}
3.以二进制的形式
加载文件
- (
void
)loadLocalFile {
UIWebView *aWebView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
NSURL *fileURL = [[ NSBundle mainBundle ] URLForResource : @"names.txt" withExtension : nil ];
NSURLRequest *URLRequest = [ NSURLRequest requestWithURL :fileURL];
NSURLResponse *response = nil ;
NSData *data = [ NSURLConnection sendSynchronousRequest :URLRequest returningResponse :&response error : nil ];
[aWebView loadData :data MIMEType :response. MIMEType textEncodingName : @"utf-8" baseURL :fileURL];
aWebView. delegate = self ;
[ self . view addSubview :aWebView];
UIWebView *aWebView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
NSURL *fileURL = [[ NSBundle mainBundle ] URLForResource : @"names.txt" withExtension : nil ];
NSURLRequest *URLRequest = [ NSURLRequest requestWithURL :fileURL];
NSURLResponse *response = nil ;
NSData *data = [ NSURLConnection sendSynchronousRequest :URLRequest returningResponse :&response error : nil ];
[aWebView loadData :data MIMEType :response. MIMEType textEncodingName : @"utf-8" baseURL :fileURL];
aWebView. delegate = self ;
[ self . view addSubview :aWebView];
}
常见的MIME类型
超文本标记语言文本 .html,.html text/html
普通文本 .txt text/plain
RTF文本 .rtf application/rtf
GIF图形 .gif image/gif
JPEG图形 .ipeg,.jpg image/jpeg
au声音文件 .au audio/basic
MIDI音乐文件 mid,.midi audio/midi,audio/x-midi
RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio
MPEG文件 .mpg,.mpeg video/mpeg
AVI文件 .avi video/x-msvideo
GZIP文件 .gz application/x-gzip
TAR文件 .tar application/x-tar