Created
November 28, 2013 18:34
-
-
Save blitzvb/7696435 to your computer and use it in GitHub Desktop.
Swizzling Method in #xamarin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DllImport ("/usr/lib/libobjc.dylib")] | |
extern static IntPtr class_getInstanceMethod (IntPtr classHandle, IntPtr Selector); | |
[DllImport ("/usr/lib/libobjc.dylib")] | |
extern static Func<IntPtr,IntPtr,IntPtr> method_getImplementation (IntPtr method); | |
[DllImport ("/usr/lib/libobjc.dylib")] | |
extern static IntPtr imp_implementationWithBlock (ref BlockLiteral block); | |
[DllImport ("/usr/lib/libobjc.dylib")] | |
extern static void method_setImplementation (IntPtr method, IntPtr imp); | |
static Func<IntPtr,IntPtr,IntPtr> original_impl; | |
void HijackWillMoveToSuperView () | |
{ | |
var method = class_getInstanceMethod (new UIView ().ClassHandle, new Selector ("willMoveToSuperview:").Handle); | |
original_impl = method_getImplementation (method); | |
var block_value = new BlockLiteral (); | |
CaptureDelegate d = MyCapture; | |
block_value.SetupBlock (d, null); | |
var imp = imp_implementationWithBlock (ref block_value); | |
method_setImplementation (method, imp); | |
} | |
delegate void CaptureDelegate (IntPtr block, IntPtr self, IntPtr uiView); | |
[MonoPInvokeCallback (typeof (CaptureDelegate))] | |
static void MyCapture (IntPtr block, IntPtr self, IntPtr uiView) | |
{ | |
Console.WriteLine ("Moving to: {0}", Runtime.GetNSObject (uiView)); | |
original_impl (self, uiView); | |
Console.WriteLine ("Added"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment