---
title: "IOS调试"
author: "yehoubin"
date: "2015-03-11T15:59:48Z"
tags: []
categories: ["移动开发"]
slug: "ios"
draft: false
lastmod: "2015-03-11T15:59:47Z"
id: 2191509
url: "https://yehoubin.iteye.com/blog/2191509"
---

#include "objc/runtime.h"

@interface AppDelegate ()

 

@end

 

UIApplication \*\_tapplication;

void (\*origMehtodImp)(id,SEL,SEL,id,id,UIEvent \*);

 

void MethodSwizzle(Class c,Class c2,SEL origSEL,SEL overrideSEL)

{

    Method origMethod = class\_getInstanceMethod(c, origSEL);

    Method overrideMethod= class\_getInstanceMethod(c2, overrideSEL);

 if(class\_addMethod(c, origSEL, method\_getImplementation(overrideMethod),method\_getTypeEncoding(overrideMethod)))

      {

          class\_replaceMethod(c,overrideSEL, method\_getImplementation(origMethod), method\_getTypeEncoding(origMethod));

      }

 

  else

  {

      method\_exchangeImplementations(origMethod,overrideMethod);

  }

}

 

 

 

@implementation AppDelegate

 

 

\- (BOOL)application:(UIApplication \*)application didFinishLaunchingWithOptions:(NSDictionary \*)launchOptions {

    \_tapplication = application;

    Method origMethod = class\_getInstanceMethod(\[UIApplication class], @selector(sendAction:to:from:forEvent:));

     origMehtodImp= method\_getImplementation(origMethod);

    

    MethodSwizzle(\[UIApplication class],\[self class],@selector(sendAction:to:from:forEvent:),@selector(sendAction:to:from:forEvent:));

    return YES;

}

\- (BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent \*)event

{

    NSLog(@"action =%s target=%@",action,target);

     origMehtodImp(\_tapplication,@selector(sendAction:to:from:forEvent:),action,target,sender,event);

    

    return true;

}