Screenshot status Monitoring-IOS

Source: Internet
Author: User

After receiving the phone status monitoring needs to add the interception state again after listening, when using the App if the user to perform a screenshot operation needs to listen to the current state, there are two methods, which can replace the screenshot of the picture content (Plan A), you can also pop up the prompt box (plan B), Nonsense not much to say the steps below.

1 #pragmaMark-Monitor Screenshots2 //Plan A3 /**4 Listening Device screenshot5  */6- (void) Registertakescreenshotnotice {7 kweakself (self);8Nsoperationqueue *mainqueue =[Nsoperationqueue Mainqueue];9 [Knotificationcenter addobserverforname:uiapplicationuserdidtakescreenshotnotificationTen                                      Object: Nil One Queue:mainqueue Ausingblock:^ (Nsnotification *_nonnull Note) { -                                       -NSLog (@"Exam Screen Cutting"); the[Weakself Userdidtakescreenshot];//Screen Response -                                  }]; - } - /** + screenshot Response -  */ +- (void) Userdidtakescreenshot { ANSLog (@"screenshot Detected"); at     //man-made operation, get screenshot picture data -UIImage *image =[self imagewithscreenshot]; -NSLog (@"userdidtakescreenshot:\n%@", image); -      -Uiimageview *imagescreenshot = [[Uiimageview alloc] initwithimage:image];//here image resources can be operated according to actual needs, display the current screenshot picture or replace it with a fixed picture way, etc. -Imagescreenshot.frame = CGRectMake (Screen_width/2, Screen_height/2, Screen_width/2, Screen_height/2); in[Self.wkwebview Addsubview:imagescreenshot];//Show at current View level - } to /** + back to screenshot data -   the @return to return screenshot data *  */ $-(UIImage *) Imagewithscreenshot {Panax NotoginsengNSData *imagedata =[self datawithscreenshotinpngformat]; -     return[UIImage Imagewithdata:imagedata]; the } + /** A Get current screen the   + @return Get the current screen -  */ $-(NSData *) Datawithscreenshotinpngformat { $     //Source (under MIT License): -Cgsize imageSize =Cgsizezero; -Uiinterfaceorientation Orientation =kapplication.statusbarorientation; the     if(uiinterfaceorientationisportrait (orientation)) { -ImageSize =screen_rect.size;Wuyi     } the     Else { -ImageSize =Cgsizemake (Screen_height, screen_width); Wu     } -      AboutUigraphicsbeginimagecontextwithoptions (ImageSize, NO,0); $Cgcontextref context =Uigraphicsgetcurrentcontext (); -      for(UIWindow *windowinch[kapplication windows]) { - cgcontextsavegstate (context); - CGCONTEXTTRANSLATECTM (context, window.center.x, window.center.y); A CGCONTEXTCONCATCTM (context, window.transform); +CGCONTEXTTRANSLATECTM (Context,-window.bounds.size.width * window.layer.anchorpoint.x,-window.bounds.size.height *window.layer.anchorpoint.y); the          -         //Correct for the screen orientation $         if(Orientation = =uiinterfaceorientationlandscapeleft) { the CGCONTEXTROTATECTM (context, m_pi_2); theCGCONTEXTTRANSLATECTM (Context,0, -imagesize.width); the         } the         Else if(Orientation = =uiinterfaceorientationlandscaperight) { -CGCONTEXTROTATECTM (Context,-m_pi_2); inCGCONTEXTTRANSLATECTM (context,-imagesize.height,0); the         } the         Else if(Orientation = =Uiinterfaceorientationportraitupsidedown) { About CGCONTEXTROTATECTM (context, M_PI); theCGCONTEXTTRANSLATECTM (context,-imagesize.width,-imagesize.height); the         } the          +         if([Window respondstoselector: @selector (drawviewhierarchyinrect:afterscreenupdates:)]) { - [window DrawViewHierarchyInRect:window.bounds afterscreenupdates:yes]; the         }Bayi         Else { the [Window.layer Renderincontext:context]; the         } -          - cgcontextrestoregstate (context); the     } the      theUIImage *image =Uigraphicsgetimagefromcurrentimagecontext (); the Uigraphicsendimagecontext (); -      the     returnuiimagepngrepresentation (image); the } the  94 //Plan B the- (void) Intercepscreenshots { the //kweakself (self); the //nsoperationqueue *mainqueue = [Nsoperationqueue mainqueue];98 //[knotificationcenter addobserverforname:uiapplicationuserdidtakescreenshotnotification object:nil queue: Mainqueue usingblock:^ (nsnotification * _nonnull Note) { About //[Weakself checkscreenshots]; - //    }];101 [Knotificationcenter addobserver:self102 selector: @selector (checkscreenshots)103 name:uiapplicationuserdidtakescreenshotnotification104                               Object: nil]; the }106- (void) Checkscreenshots {107Uialertview *alertview = [[Uialertview alloc] Initwithtitle:@"Tips"108Message@" Don't"109                                                        Delegate: Self theCancelbuttontitle:@"YES"111Otherbuttontitles:@"NO", nil]; the [Alertview show];113}

This is the end of this share, hope that the content can be helpful to everyone, there is any shortcomings welcome guidance and common progress!

Screenshot status Monitoring-IOS

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.