Some tips for UIWebView in iOS development

Source: Internet
Author: User

A long time did not write a blog, and today finally have time, summed up the time encountered in the UIWebView on some small technical issues. UIWebView is one of the most commonly used controls in the iOS SDK. is a built-in browser control that we can use to navigate the Web, open the document, and so on. But sometimes we encounter a problem, that is, how to achieve click WebView loaded images to enlarge the browsing, which will use the JavaScript scripting language, here to do objective-c and JavaScript interaction, you need to use the OC in the

Stringbyevaluatingjavascriptfromstring: Method, it's very powerful, very simple to use, through which we can easily manipulate the page elements in the UIWebView.

How to get the image URL of the loaded page content, here the main code is:

1. Add a pat gesture to the WebView first:

-(void) addtaponwebview{    uitapgesturerecognizer* singletap = [[UITapGestureRecognizer Alloc] initwithtarget:self action: @selector (Handlesingletap:)];    [SELF.CONTENTWEBV Addgesturerecognizer:singletap];    Singletap. delegate = self ;}

2. after setting the Uigesturerecognizerdelegate agent, implement the following method, this method the default implementation returns NO, This is usually called when a gesture recipient wants to prevent another gesture from receiving its own message:

-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) othergesturerecognizer{     return YES;}

Ask delegate, whether two gestures receive messages at the same time, return yes to receive simultaneously. Returns no, not received at the same time.

3. Click on the image in the WebView page to get the image URL:

1-(void) Handlesingletap: (UITapGestureRecognizer *) Sender2 {3Cgpoint pt =[Sender LocationInView:self.contentWebV];4NSString *imgurl = [NSString stringWithFormat:@"Document.elementfrompoint (%f,%f). SRC", Pt.x, Pt.y];5NSString *urltosave =[SELF.CONTENTWEBV Stringbyevaluatingjavascriptfromstring:imgurl];6NSLog (@"Image url=%@", urltosave);7     if(Urltosave.length >0) {8 [self showimageurl:urltosave point:pt];9     }Ten}

Note here that the use of Web scripting language JavaScript, through which we can easily implement the content of the Web page operation.

4. Once the URL is obtained, a uiscrollview is created and the URL is passed in to display it on the ScrollView:

1 // Render Picture 2 -(void) Showimageurl: (NSString *) URL point: (cgpoint)point3{4      Imagescrollviewcontroller *IMGSCROLLVC = [[Imagescrollviewcontroller alloc] init]; 5     imgscrollvc.imgurl = URL; 6     [Self.view.window.rootViewController PRESENTVIEWCONTROLLER:IMGSCROLLVC animated:no Completion:nil]; 7 }

The specific implementation effect is:

Some tips for UIWebView in iOS development

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.