[Basic iOS control and basic ios Control

Source: Internet
Author: User

[Basic iOS control and basic ios Control
A. Basic usage1. concept iOS built-in browser control Safari browser is implemented through UIWebView 2. purpose: make a simple browser (1) create a basic request to create a request for loading (2) Listen to webView loading by proxy, for example, prompt loading (3) forward and backward using NavigationController, add the navigation bar (check showToolBar) at the bottom, and the back and forward buttons (4) Add the UISearchBar proxy listening "Search" button in the top navigation bar, extract address bar text 1 // 2 // ViewController. m 3 // UIWebViewdDemo 4 // 5 // Created by hellovoidworld on 15/1/30. 6 // Copyright (c) 2015 hellovoidworld. all rights reserved. 7 // 8 9 # import "ViewController. h "10 11 @ interface ViewController () <UISearchBarDelegate, UIWebViewDelegate> 12 13 @ property (weak, nonatomic) IBOutlet UIWebView * webView; 14 15 @ property (weak, nonatomic) IBOutlet handle * backButton; 16 @ property (weak, nonatomic) IBOutlet UIBarButtonItem * forwardButton; 17-(IBAction) back :( handle *) sender; 18-(IBAction) forward :( UIBarButtonItem *) sender; 19 20 @ end21 22 @ implementation ViewController23 24-(void) viewDidLoad {25 [super viewDidLoad]; 26 // Do any additional setup after loading the view, typically from a nib.27 28 // create a search bar 29 UISearchBar * searchBar = [[UISearchBar alloc] init]; 30 searchBar. frame = CGRectMake (0, 0,300, 40); 31 searchBar. delegate = self; 32 33 self. navigationItem. titleView = searchBar; 34 35 self. webView. delegate = self; 36} 37 38 39 40-(IBAction) back :( UIBarButtonItem *) sender {41 NSLog (@ ""); 42 [self. webView goBack]; 43} 44 45-(IBAction) forward :( UIBarButtonItem *) sender {46 NSLog (@ "forward"); 47 [self. webView goForward]; 48} 49 50 # pragma mark-UIWebViewDelegate51-(void) webViewDidStartLoad :( UIWebView *) webView {52 NSLog (@ "Start loading "); 53} 54 55-(void) webViewDidFinishLoad :( UIWebView *) webView {56 NSLog (@ ""); 57 58 // set back, forward button 59 if ([self. webView canGoBack]) {60 self. backButton. enabled = YES; 61} else {62 self. backButton. enabled = NO; 63} 64 65 if ([self. webView canGoForward]) {66 self. forwardButton. enabled = YES; 67} else {68 self. forwardButton. enabled = NO; 69} 70} 71 72 # pragma mark-UISearchBarDelegate73/** click the search button (on the virtual keyboard) */74-(void) searchBarSearchButtonClicked :( UISearchBar *) searchBar {75 NSString * str = searchBar. text; 76 NSURL * url = [NSURL URLWithString: [NSString stringWithFormat: @ "http: // % @", str]; 77 NSURLRequest * request = [NSURLRequest requestWithURL: url]; 78 79 [self. webView loadRequest: request]; 80} 81 82 @ end3. load local resources html, txt, doc, ppt, MP4, pdf and directly request 4. automatic Identification of telephone, email, address // identify all types of data self. webView. dataDetectorTypes = UIDataDetectorTypeAll;B. Other Purposes1. Execute javascript1/** run js Code */2-(IBAction) runJs {3 NSString * str = @ "alert ('hello, world! '); "; 4 [self. webView stringByEvaluatingJavaScriptFromString: str]; 5}2. javascript calls OC to send webpage requests in javascript and uses OC to intercept them. Based on the Content judgment, call the corresponding method. Here, use js Code to call out the album: 1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" 2 pageEncoding = "UTF-8" %> 3 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 4

1/** webView is called before sending a request. You can intercept the request and choose whether to load (return YES) or not (return NO) */2-(BOOL) webView :( UIWebView *) webView shouldStartLoadWithRequest :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {3 4 // intercept request 5 NSString * urlStr = request. URL. absoluteString; 6 nsange range = [urlStr rangeOfString: @ "ios: //"]; 7 8 // if it is an ios request 9 if (range. length! = 0) {10 // method name 11 NSString * method = [urlStr substringFromIndex: range. location + range. length]; 12 13 // wrap SEL14 SEL selector = NSSelectorFromString (method); 15 16 // execution method 17 [self defined mselector: selector withObject: nil]; 18} 19 20 return YES; 21} 22 23/** open album */24-(void) openAlbum {25 // create a Photo Select controller 26 UIImagePickerController * imagePC = [[UIImagePickerController alloc] init]; 27 [imagePC setSourceType: UIImagePickerControllerSourceTypePhotoLibrary]; // use album source 28 29 [self presentViewController: imagePC animated: YES completion: ^ {30 NSLog (@ ""); 31}]; 32}

 

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.