I have a very large project with 3rd party frameworks and would like to disable method swizzling to make sure the 3rd party frameworks do not mess the default expected behaviour. Is it possible? Is there some flag in the project settings?
Asked
Active
Viewed 4,631 times
3
-
There is no way to do this if your class inherits from NSObject. All the UIKit classes are basically inherited from NSObject. For the classes which you define in your own project, it should not inherit from NSObject, only in this condition swizzling doesn't work. – Sachin Vas Oct 11 '16 at 08:15
-
get help from it. https://github.com/uraimo/SwizzlingInSwift – Jamshed Alam Oct 11 '16 at 08:39
1 Answers
1
There is no option to disable that although if you avoid subclassing from NSObject you would be safe . If you want to prevent Any Library from swizzling you may override the core methods for it namely
public func class_addMethod(_ cls: Swift.AnyClass!, _ name: Selector!, _ imp: IMP!, _ types: UnsafePointer<Int8>!) -> Bool
and
public func method_exchangeImplementations(_ m1: Method!, _ m2: Method!)
Like this
public func method_exchangeImplementations(_ m1: Method!, _ m2: Method!) {
}
public func class_addMethod(_ cls: Swift.AnyClass!, _ name: Selector!, _ imp: IMP!, _ types: UnsafePointer<Int8>!) -> Bool
{
return false
}
I do not suggest that though , but you might give it a try.

M.Othman
- 5,132
- 3
- 35
- 39
-
Very interesting. It just occurred to me: Are we sure that none of the system's libraries (Foundation, UIKit, etc.) rely on swizzling in their current implementations, and applying your solution wouldn't break anything? It would be a sloppy move by Apple (after all, they own the code: the can rewrite any part they need instead of recurring to such hacks...), but can never be sure... – Nicolas Miari Oct 11 '16 at 08:43
-
1Swizzling is dangerous and probably in frameworks like UIKit apple won't use it or maybe they have their internal swizzling functions that are not public(no assumptions hehh) . I have not try it in real project since I kinda always rely on swizzling , but it would be great if you(or anyone) give it a try and write the results. – M.Othman Oct 11 '16 at 09:03
-
I won't go to that length :) I just wanted to know if there's a flag to disable it and you confirmed my assumption that there's no such flag. Thank you – Yasmin Tiomkin Oct 12 '16 at 16:29
-
1Apple uses method swizzling in some frameworks, for example in WebKit (WebCoreThread.mm:363). – Borys Verebskyi Oct 15 '16 at 14:20
-
@BorisVerebsky aha, good to know that, after overriding the function in our module , would the other frameworks see our version of the function or the original !? – M.Othman Oct 15 '16 at 16:37
-
@M.Othman, see http://stackoverflow.com/questions/38488312/is-there-any-way-to-list-all-swizzled-methods-in-an-ios-app regarding a proper way to override `class_addMethod ` and `method_exchangeImplementations`. Although I also wouldn't recomend that way to disable method swizzling, it will probably break existing frameworks logic. – Borys Verebskyi Oct 15 '16 at 16:50
-