UIWebView is a box used to load web page data. UIWebView can be used to load pdf word doc and other files
There are two ways to generate a webview:
1. Drag and drag through storyboard
2. Initialize through alloc init
Create a webview, in the following text _webView.dataDetectorTypes = UIDataDetectorTypeAll; It is to identify the type in the webview. For example, when there is a phone number in the webview, click the number to make a call directly.
- (UIWebView *)webView { if (!_webView) { _webView = [[UIWebView alloc] initWithFrame:]; _webView.dataDetectorTypes = UIDataDetectorTypeAll; } return _webView; }
Loading the web page
// Let the browser load the specified string and search using it- (void)loadString:(NSString *)str { // 1. URL locates the resource, the address of the resource requiredNSString *urlStr = str; if (![str hasPrefix:@"http://"]) { urlStr = [NSString stringWithFormat:@"/s?word=%@", str]; } NSURL *url = [NSURL URLWithString:urlStr]; // 2. Tell the URL to the server, request, and request data fromNSURLRequest *request = [NSURLRequest requestWithURL:url]; // 3. Send a request to the server[ loadRequest:request]; }
Loading html
// HTML is the design language of web pages// <>Indicates a mark</>// Application scenario: Intercept a certain part of the web page to display// For example: The complete content of the web page contains ads! After loading the page, delete the HTML of the ad part and then load it// Used by many news applications[ loadHTMLString:@"<p>Hello</p>" baseURL:nil];
Load local files
#pragma mark - Loading files- (void)loadFile { // Application scenario: Load files downloaded from the server, such as PDF, word, picture, etc.NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"About.txt" withExtension:nil]; NSURLRequest *request = [NSURLRequest requestWithURL:fileURL]; [ loadRequest:request]; }
Load local files in secondary mode
#pragma Loads files as binary data- (void)loadDataFile { // The most common situation// Open IE, visit the website, and prompt you to install the Flash plugin// Without this application, you cannot open the corresponding file with UIWebView.// Application scenario: Load files downloaded from the server, such as PDF, word, picture, etc.NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"iOS 7 Programming " withExtension:nil]; NSURLRequest *request = [NSURLRequest requestWithURL:fileURL]; // The server's response object, the server receives the request and returns it to the clientNSURLResponse *respnose = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&respnose error:NULL]; NSLog(@"%@", ); // In iOS development, if it is not a special requirement, all text encodings are used in UTF8// First use UTF8 to interpret the received binary data stream[ loadData:data MIMEType: textEncodingName:@"UTF8" baseURL:nil]; }
The above is the method of using UIWebView to load web pages, files, and html in IOS that the editor introduced to you. I hope it will be helpful to you.