Dynamic replacement of appdelegate under Adobe Air iOS ane using the hook mechanism of iOS

Source: Internet
Author: User

Development background

After the game has been developed with Adobe AIR, you need to pay for iOS or Android platform, Push SDK access, this article can be used to completely solve the iOS platform delegate life cycle several callback function calls, to achieve the native push, payment function access


Hook knowledge Background

(ObjC method Swizzling, this section of the content is transferred from Http://blog.csdn.net/yiyaaixuexi)


Calling a method in Objective-c is actually sending a message to an object, and the only way to find the message is by selector's name. By using the dynamic characteristics of objective-c, the corresponding method of selector can be realized at runtime to achieve the purpose of linking to the method.
Each class has a list of methods that hold the mapping of selector's name and method implementation. IMP is a bit like a function pointer, pointing to a concrete method implementation.



We can use Method_exchangeimplementations to exchange the IMP in 2 ways,
We can use Class_replacemethod to modify the class,
We can use Method_setimplementation to directly set the IMP for a method,
......
In the final analysis, the Imp of selector is changed, as shown:



Implementation ideas

1. Hook the Appdelegate class of Adboe air at the Mdchangedelegatehelper class loading stage to ensure that the appdelegate of Adobe Air is replaced with our newly implemented class before it is created

2. Requires 3 sel,

A appdelegate original Sel:oldsel,

A mdchangedelegatehelper default Sel:defaultsel that is used to add the default original Oldsel for the original appdelegate

A mdchangedelegatehelper target Sel:newsel

Method Replacement Ideas:


3, the corresponding SEL relationship after replacement is

Oldsel--Newmethod

Newsel--Oldmethod

So when the corresponding Appdelegate method is called,

Oldsel found the implementation of the Newmethod, Newmethod implementation of the Hookedxxx method within the MDCHANGEDELEGATEHELPER.M

The Newsel,newsel point Oldmethod is called in Newmethold, which realizes the compatibility of the original process.


Code implementation

mdchangedelegatehelper.m//changedelegatedemo////Created by Ashqal on 14-10-31.//Copyright (c) 2014 Moredoo. All rights reserved.//#import "MDChangeDelegateHelper.h" #import <UIKit/UIKit.h> #import <objc/runtime.h> void Hookmethod (sel oldsel,sel Defaultsel, sel newsel) {//ctappcontroller Class aclass = Objc_getclass ("Ctappcontrol    Ler "); if (AClass = = 0) {NSLog (@ "!!!!!!!!!!!!!        did not find Adobe class! ");    Return    } Class Bclass = [Mdchangedelegatehelper class];    Add the method to AClass Class_addmethod (AClass, Newsel, Class_getmethodimplementation (Bclass, Newsel), nil);        Class_addmethod (AClass, Oldsel, Class_getmethodimplementation (Bclass, Defaultsel), nil);    Method Oldmethod = Class_getinstancemethod (AClass, Oldsel);    ASSERT (Oldmethod);    Method Newmethod = Class_getinstancemethod (AClass, Newsel);    ASSERT (Newmethod);  Method_exchangeimplementations (Oldmethod, Newmethod);  } @implementation mdchangedelegatehelper+ (void) load{  NSLog (@ "mdchangedelegatehelper load");                                                 [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (createstarternotificationchecker:)    name:@ "Uiapplicationwillfinishlaunchingnotification" object:nil];            [Self changedelegate]; Hookmethod (@selector (application:didfinishlaunchingwithoptions:), @selector (Defaultapplication:didfinishlau    Nchingwithoptions:), @selector (hookedapplication:didfinishlaunchingwithoptions:));               Hookmethod (@selector (application:handleopenurl:), @selector (Defaultapplication:handleopenurl:)    , @selector (hookedapplication:handleopenurl:)); Hookmethod (@selector (application:openURL:sourceApplication:annotation:), @selector (Defaultapplication:openu        RL:sourceApplication:annotation:), @selector (hookedApplication:openURL:sourceApplication:annotation:)); Hookmethod (@seLector (Application:supportedinterfaceorientationsforwindow:), @selector (Defaultapplication:supportedinterfa               Ceorientationsforwindow:), @selector (Hookedapplication:supportedinterfaceorientationsforwindow:)    );               Hookmethod (@selector (applicationdidbecomeactive:), @selector (defaultapplicationdidbecomeactive:)        , @selector (hookedapplicationdidbecomeactive:)); Hookmethod (@selector (init), @selector (init), @selector (Hookedinit));} -(BOOL) Hookedapplication: (uiapplication*) Application didfinishlaunchingwithoptions: (nsdictionary*) dic{NSLog (@ "    Hooked didfinishlaunchingwithoptions ");    [Self hookedapplication:application didfinishlaunchingwithoptions:dic]; return YES;}    -(ID) hookedinit{NSLog (@ "hooked init!!!"); return [self hookedinit];} -(BOOL) Hookedapplication: (uiapplication *) application Handleopenurl: (nsurl *) URL {NSLog (@ "Hooked handleOpenURL ");    [Self hookedapplication:application handleopenurl:url]; return YES;} -(BOOL) Hookedapplication: (uiapplication *) application OpenURL: (nsurl *) URL sourceapplication: (NSString *)    sourceapplication annotation: (ID) annotation{NSLog (@ "Hooked openURL sourceapplication annotation");    [Self hookedapplication:application openurl:url sourceapplication:sourceapplication annotation:annotation]; return YES;} -(Nsuinteger) Hookedapplication: (uiapplication *) application Supportedinterfaceorientationsforwindow: (UIWindow *)    window{NSLog (@ "hooked Supportedinterfaceorientationsforwindow");    [Self hookedapplication:application Supportedinterfaceorientationsforwindow:window]; return 0;} -(void) Hookedapplicationdidbecomeactive: (uiapplication *) application{[self hookedapplicationdidbecomeactive:    Application]; NSLog (@ "hooked applicationdidbecomeactive");} -(BOOL) Defaultapplication: (uiapplication*) Application didfinishlaunchingwithoptions: (nsdictionary*) dic{return YES;} - (BOOL) Defaultapplication: (uiapplication *) application Handleopenurl: (Nsurl *) Url{return YES;} -(BOOL) Defaultapplication: (uiapplication *) application OpenURL: (nsurl *) URL sourceapplication: (NSString *) sourceapplication annotation: (ID) Annotation{return YES;} -(Nsuinteger) Defaultapplication: (uiapplication *) application Supportedinterfaceorientationsforwindow: (UIWindow *) Window{return 0;}    -(void) Defaultapplicationdidbecomeactive: (UIApplication *) application{}-(id) init{self = [super init]; if (self) {} return to self;} @end


watch out.

A) NSLog the appdelegate name of Adobe air at runtime is Ctappcontroller, so use this class as a replacement object

b) when replacing the Init function, it is found that Ctappcontroller does not implement the INIT function, so the default Init function is implemented as a basis, otherwise the instance cannot be created.


Dynamic replacement of appdelegate under Adobe Air iOS ane using the hook mechanism of 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.