UIWebView is a box used to load data from a Web page. UIWebView can be used to load PDF Word doc and so on files
There are two ways to generate WebView:
1, through the storyboard drag
2, initialized by Alloc init.
Create WebView, _webview.datadetectortypes = Uidatadetectortypeall in the following text; is to identify the type in the WebView, for example, when there is a phone number in the WebView, click the number to call directly
-(UIWebView *) WebView
{
if (!_webview) {
_webview = [[UIWebView alloc] initWithFrame:self.view.bounds];
_webview.datadetectortypes = Uidatadetectortypeall;
}
return _webview;
}
Load Web page
Let the browser load the specified string and search using m.baidu.com
-(void) LoadString: (NSString *) str
{
//1. URL locates resources, requires the address of the resource
nsstring *urlstr = str;
if (![ Str hasprefix:@ "http://"]) {
urlstr = [NSString stringwithformat:@ "http://m.baidu.com/s?word=%@", str];
}
Nsurl *url = [Nsurl urlwithstring:urlstr];
2. Tell the URL to the server, request, request data from the m.baidu.com
nsurlrequest *request = [Nsurlrequest requestwithurl:url];
3. Send the request to the server
[Self.webview loadrequest:request];
Load HTML
HTML is the design language for Web pages
//<> notation </>//
scenarios: Intercepting a part of a Web page display
//For example: the full content of the Web page contains ads! After loading the completion page, Remove the HTML from the advertising section and then load
//be used by many news class applications
[Self.webview loadhtmlstring:@ "<p>Hello</p>" BaseURL: NIL];
Loading local files
#pragma mark-load file
-(void) loadfile
{
//scenario: Load files downloaded from the server, such as PDFs, or word, pictures, etc.
nsurl *fileurl = [ NSBundle Mainbundle] urlforresource:@ "about. txt" withextension:nil];
Nsurlrequest *request = [Nsurlrequest requestwithurl:fileurl];
[Self.webview Loadrequest:request];
Loading local files in a level two system
#pragma loading files as binary data
-(void) loaddatafile
{
//most common one case
//open IE, visit website, prompt you to install Flash plugin
// Without this application, it is impossible to open the corresponding file
//scenario with UIWebView: Load files downloaded from the server, such as PDFs, or word, pictures, etc.
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 to return to the client
nsurlresponse *respnose = nil;
NSData *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&respnose Error:NULL];
NSLog (@ "%@", Respnose.) MimeType);
In iOS development, if not a special requirement, all text encoding is used UTF8
///UTF8 to interpret the received binary data stream
[Self.webview loaddata:data mimetype:respnose. MimeType textencodingname:@ "UTF8" Baseurl:nil];
}
The above is a small set of iOS for you to introduce the use of UIWebView loading Web pages, files, HTML methods, I hope to help.