IOS UIWebView keyboard processing and iosuiwebview keyboard

Source: Internet
Author: User

IOS UIWebView keyboard processing and iosuiwebview keyboard

+ ------------------------- +

If you have the following questions, this article may help you.

+ ------------------------- +


Step by step: 1. Remove the keyboard

If you click the html text box control in UIWebView, the keyboard is automatically displayed. Of course, you need to obtain the keyboard information (height, etc.) by using UIViewController + Notification. The Code is as follows:

// UIKeyboardWillShowNotification and UIKeyboardWillHideNotification indicate the post notification name of the iOS system when the keyboard pops up or is removed. Here, you only need to define self as the notification recipient. // ViewWillAppear: And viewWillDisappear: You should be aware that the two methods are executed after self loadView and removefromsuperview respectively. // Note: the object parameter here must be nil. Otherwise, the user's userInfo-(void) viewWillAppear :( BOOL) animated {[super viewWillAppear: animated] cannot be obtained. [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillShow :) name: jsonobject: nil]; [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (handler :) name: jsonobject: nil];}-(void) viewWillDisappear :( BOOL) animated {[super viewWillDisappear: animated]; [[nsicationicationcenter defacenter center] removeObserver: self name: jsonobject: nil]; [[nsicationcenter center defacenter center] removeObserver: self name: UIKeyboardWillHideNotification object: nil];}-(void) keyboardWillShow :( NSNotification *) notification {NSDictionary * userInfo = [notification userInfo]; NSValue * value = [userInfo objectForKey: UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [value CGRectValue]; // the frame of the keyboard is obtained here // your operations, such as the appearance of the keyboard, control view move-up, etc.}-(void) keyboardWillHide :( NSNotification *) notification {// obtain the same method as info // your operations, such as keyboard removal and control view restoration}

2. Drag UIWebView to remove the keyboard

On the Internet, many people have done a lot of operations to implement this function, but in iOS 7, apple has provided us with the following code:

Self. webView. scrollView. keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; // remove the keyboard when dragging

If it is less than iOS 7, refer to 6 to set it. The general idea is to add a private flag to indicate whether the current keyboard exists. If it exists, use 6 to get the event and close the keyboard.

3. the keyboard masks the UIWebView solution. You can operate in keyboardWillShow in step 1 by changing the webView origin. 4. When the keyboard appears, Content in UIWebView is moved up, so that the highlighted text box exceeds the visible area of UIWebView.

In UIWebView, as long as the keyboard appears, UIWebView will certainly move up. It is hard to say that the combination is not suitable. If it is not suitable, you only need to disable automatic movement.

5. How to disable the Content in UIWebView from moving up when the keyboard pops up

I have been searching for this method for a long time, but I still find it. Thanks to the powerful netizens, the Code is as follows:

@ Interface XXX: UIViewController <UIScrollViewDelegate> // Add UIScrollViewDelegate and step 1self. webView. scrollView. delegate = self; // register the proxy, step 2-(UIView *) viewForZoomingInScrollView :( UIScrollView *) scrollView {// implement the proxy method, step 3 return nil ;}

6. How to obtain click coordinates in UIWebView

As we all know, UIWebView will eat all touch events, otherwise there will not be so many people to work on javascript, but it cannot be set. It does not mean it cannot be replaced by another method. The general idea is: add a gesture to the superView of the webView and implement the multi-gesture filter settings. Why do we need to set the multi-gesture filter? I would like to explain here that because UIWebView has its own gesture by default, it will intercept your gesture and the superView will not be able to receive the gesture. The Code is as follows:

@ Interface XXX: UIViewController <alert> // Add alert, step 1 // Add gesture, step 2 UITapGestureRecognizer * webTap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (webTap :)]; webTap. numberOfTouchesRequired = 1; webTap. numberOfTapsRequired = 1; webTap. delegate = self; webTap. cancelsTouchesInView = NO; [self. view addGestureRecognizer: webTap) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer consumer :( UIGestureRecognizer *) Consumer {return YES;}-(void) webTap :( handler *) sender {CGPoint tapPoint = [sender locationInView: self. webView. scrollView]; // obtain the coordinates relative to the webView. If it is changed to self. view obtains the coordinates of step 4 NSLog (@ "tapPoint x: % f y: % f", tapPoint. x, tapPoint. y );}

This is the only thing you can think of when processing the UIWebView keyboard.

BB: Reprint please indicate the source http://blog.csdn.net/assholeu/article/details/38714123
Reference:Thanks http://blog.csdn.net/abel_tu/article/details/12134261



How to return button events in ios webview to the app

Use of WebView for IOS Learning

1. Use UIWebView to load webpages
Run XCode 4.3 and create a new Single View Application named WebViewDemo.
2. Load WebView
Add the WebView member variable in ViewController. h and add the implementation in ViewController. m.
The network environment of the mobile phone changes in real time. When the network is slow, how can I prompt that the user's webpage is being opened? How can I prompt users when an error occurs when opening a webpage? At this time, we need to know when the webpage will be opened,
When the loading is complete and when an error occurs. We need to implement the <UIWebViewDelegate> protocol.
3. Modify the implementation protocol in ViewController. h as follows:
Press control + command + up key to switch to the ViewController. m file. Here we enter-(void) webView in the file to see the following implementation method:
Several important functions in UIWebView
1.-(void) webViewDidStartLoad :( UIWebView *) called when the webView page starts loading
2.-(void) webViewDidFinishLoad :( UIWebView *) called when the webView page is loaded
3.-(void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) Call when error webpage loading error

4. Add NSLog to implement these three methods.
Add
[WebView setDelegate: self]; sets the proxy. In this way, the preceding three methods can be called back.
The three methods are implemented as follows:

Run print:
15:20:29. 728 WebViewDemo [1001: f803] webViewDidStartLoad
15:20:29. 991 WebViewDemo [1001: f803] webViewDidFinishLoad
Let's try the error and turn off wifi and run the following command to print the result:

15:23:58. 939 WebViewDemo [1087: f803] webViewDidStartLoad
15:23:59. 016 WebViewDemo [1087: f803] webViewDidFinishLoad
The request results remain unchanged. Why is network disconnection still successful? Cache? I will try 163.com. This is the real result:
15:24:41. 131 WebViewDemo [1134: f803] webViewDidStartLoad
15:24:41. 149 WebViewDemo [1134: f803] didFailLoadWithError: Error Domain = NSURLErrorDomain Code =-1009 "The Internet connection appears to be offline. "UserInfo = 0x6b41660 {NSErrorFailingURLStringKey = www.163.com/, NSErrorFailingU ...... remaining full text>
 
Can ios uiwebview load json data?

Yes, but you need to convert json into your data structure. xml is also the same. It is impossible to load a json or xml data directly.

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.