How to load Web pages, files, and HTML using UIWebView in IOS _ios

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.