IOS USES UIWebView to load web pages files and html
- 2020-05-13 03:30:32
- OfStack
UIWebView is a box used to load web page data. UIWebView can be used to load pdf word doc and so on
There are two ways to generate webview:
1. Drag and drop via storyboard
2. Initialize with alloc init
Create webview, in the following text _webView.dataDetectorTypes = UIDataDetectorTypeAll; Is to identify the type in webview. For example, when there is a phone number in webview, you can call directly by clicking on the number
- (UIWebView *)webView
{
if (!_webView) {
_webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
_webView.dataDetectorTypes = UIDataDetectorTypeAll;
}
return _webView;
}
Page loading
// Lets the browser load the specified string , use m.baidu.com To search
- (void)loadString:(NSString *)str
{
// 1. URL Locate resources , Where the resource is needed
NSString *urlStr = str;
if (![str hasPrefix:@"http://"]) {
urlStr = [NSString stringWithFormat:@"http://m.baidu.com/s?word=%@", str];
}
NSURL *url = [NSURL URLWithString:urlStr];
// 2. the URL Tell the server , request , from m.baidu.com The request data
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3. Send the request to the server
[self.webView loadRequest:request];
}
Loading html
// HTML Is the design language of web pages
// <> Said mark </>
// Application scenarios : Intercept something in a web page 1 Part of a display
// For example, : The full content of the page contains ads ! After loading the page , Take the advertising part HTML delete , And then load it
// Used by many news applications
[self.webView loadHTMLString:@"<p>Hello</p>" baseURL:nil];
Load local files
#pragma mark - Load the file
- (void)loadFile
{
// Application scenarios : Load the file downloaded from the server , For example, pdf, or word, Pictures and so on
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@" about .txt" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[self.webView loadRequest:request];
}
Load the local file in a level 2 fashion
#pragma In order to 2 Load files in the form of base data
- (void)loadDataFile
{
// The most common 1 Kind of circumstance
// Open the IE, Visit the web site , Prompt you to install Flash The plug-in
// If you don't have this application , It is impossible to use UIWebView Open the corresponding file
// Application scenarios : Load the file downloaded from the server , For example, pdf, or word, Pictures and so on
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"iOS 7 Programming Cookbook.pdf" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
// The server's response object , The server receives the request and returns it to the client
NSURLResponse *respnose = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&respnose error:NULL];
NSLog(@"%@", respnose.MIMEType);
// in iOS The development of , If it's not a special requirement , All text encoding is used UTF8
// Use first UTF8 Interpret what is received 2 Base data flow
[self.webView loadData:data MIMEType:respnose.MIMEType textEncodingName:@"UTF8" baseURL:nil];
}
The above is the site for you to use UIWebView IOS loading web pages, files, html method, I hope to help you.