iOS event blocker and application

Source: Internet
Author: User
Tags uikit

1. Overview

We know that the distribution of events is from application to window to all levels of view, so it is clear that the safest and most secure interception place is application. There is no chance of entering the Hit-test view process if you do not distribute it manually after intercepting the event.

Both UIApplication and UIWindow have sendevent: methods to distribute the event. We can inherit the class, re-implement the Sendevent: method, so that we can intercept the next event, complete some special processing, you can also use runtime, the Sendevent: method to replace with our own method, add some of our own implementation, and then call the original message flow.

2. Interception mode

1) Using runtime

#import "EventHookObject.h"#import<objc/objc.h>#import<objc/runtime.h>@implementationEventhookobject+ (void) initialize{Method sendevent= Class_getinstancemethod ([uiapplicationclass], @selector (sendevent:)); Method mysendevent= Class_getinstancemethod ([selfclass], @selector (mysendevent:)); IMP Sendeventimp=method_getimplementation (sendevent); Class_addmethod ([UIApplicationclass], @selector (Sendeventori:), Sendeventimp, method_gettypeencoding (sendevent)); IMP Mysendeventimp=method_getimplementation (mysendevent); Class_replacemethod ([UIApplicationclass], @selector (sendevent:), Mysendeventimp, method_gettypeencoding (sendevent));} /** Intercept to UIApplication's sendevent * We can finish the process before we continue to call the normal process*/- (void) Mysendevent: (uievent*)Event {    //Here you can add some of the processing you want[Self performselector: @selector (Sendeventori:) Withobject:Event];}

Then there is the function-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions within the call can be

2) inherit the UIApplication class and implement your own sendevent:

The application scenario is as follows, such as an application that requires a process to be touched in a region other than a particular view.

We can of course in each of the remaining view add code to judge, but this is more tired, easy to miss some places, the relatively simple solution is to inherit the UIApplication class, to achieve their own sendevent:, in this method of preliminary filtering events, is to touch the event to send notification, and the specific view will register the notification, received after the judge whether to touch the outside of their own area.

#import "EventApplication.h" nsstring *const Notificationscreentouch = @ "Notificationscreentouch"; @implementation eventapplication-(void) Sendevent: (uievent *) event{    if (event.type = = uieventtypetouches) {        if ([[Event.alltouches anyobject] phase] = = Uitouchphasebegan) {            [[Nsnotificationcenter Defaultcenter] Postnotification:[nsnotification Notificationwithname:notificationscreentouch Object:nil userInfo:[NSDictionary Dictionarywithobject:event forkey:@ "value"]];        }    }    [Super Sendevent:event];} @end

Replace the uiapplication call in the main.m file

#import<UIKit/UIKit.h>#import "AppDelegate.h"#import "EventApplication.h" intMainintargcChar*argv[]) {@autoreleasepool {//return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));        returnUiapplicationmain (argc, argv, Nsstringfromclass ([eventapplicationclass]), Nsstringfromclass ([appdelegateclass])); }}

So we can realize the pretreatment of the event, seize the source, want to do anything.

iOS event blocker and application

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.