{"id":1699,"url":"https://github.com/sushinoya/lumos","last_synced_at":"2025-08-02T04:32:20.438Z","repository":{"id":32774033,"uuid":"140947784","full_name":"sushinoya/lumos","owner":"sushinoya","description":"💡 A light wrapper around Objective-C Runtime","archived":false,"fork":false,"pushed_at":"2024-01-30T01:52:57.000Z","size":63,"stargazers_count":153,"open_issues_count":3,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-09T11:19:37.899Z","etag":null,"topics":["hacktoberfest","objective-c","objective-c-runtime","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sushinoya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-07-14T13:20:19.000Z","updated_at":"2025-05-12T11:23:05.000Z","dependencies_parsed_at":"2024-04-23T19:05:52.873Z","dependency_job_id":"ce3767f1-af8f-4dc9-b6b7-64364d724da6","html_url":"https://github.com/sushinoya/lumos","commit_stats":{"total_commits":43,"total_committers":3,"mean_commits":"14.333333333333334","dds":"0.046511627906976716","last_synced_commit":"af70e7623df6c4d5e95ac71fae92c19ac65f70d9"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/sushinoya/lumos","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushinoya%2Flumos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushinoya%2Flumos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushinoya%2Flumos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushinoya%2Flumos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sushinoya","download_url":"https://codeload.github.com/sushinoya/lumos/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushinoya%2Flumos/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268334615,"owners_count":24233793,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","objective-c","objective-c-runtime","swift"],"created_at":"2024-01-05T20:15:53.670Z","updated_at":"2025-08-02T04:32:20.064Z","avatar_url":"https://github.com/sushinoya.png","language":"Swift","funding_links":[],"categories":["Objective-C Runtime"],"sub_categories":["Push Notification Providers","Local Notifications"],"readme":"  \u003cimg width=\"300\" alt=\"LMMethod Methods\" src=\"https://user-images.githubusercontent.com/23443586/43039911-d3c7a22c-8d69-11e8-92d7-381bc08ed578.gif\"\u003e\n\nA *light* wrapper around Objective-C Runtime.\n# \n\n\n## What exactly is ***lumos***?\n\n*lumos* as mentioned is a *light* wrapper around objective-c runtime functions to allow an easier access to the runtime. It makes operations such as swizzling and hooking very simple in Swift.\n\nFor example, say you wish to run a block of code whenever a `ViewController`'s `viewDidLoad` method is called\n\nWith *lumos*, you can do the following:\n\n```swift\n// In AppDelegate (or any conveinient place)..\n\nlet method = Lumos.for(ViewController.self).getInstanceMethod(selector: #selector(ViewController.viewDidLoad))\n        \nmethod?.prepend {\n    // This block will be run every time a viewDidLoad is called\n    print(\"View Controller loaded\")\n}\n````\n\nSimilarily you can `append` a block to a method which will be called right before the method returns. You can even use `replace` to replace the method's implementation with the block you pass in as a parameter.\n\nIf you wanted more flexibility, you could swizzle the `viewDidLoad` method using the following lines:\n\n```swift\n@objc func myMethod() {\n    // Do anything here\n}\n\nlet myMethod = self.lumos.getInstanceMethod(selector: #selector(myMethod))\n\nmethod?.swapImplementation(with: myMethod)\n```\n\nDo you feel the superpower yet? Maybe you wish to list all the classes registered at runtime:\n\n```swift\nLumos.getAllClasses()\n```\n\n*Fun Fact:* There are almost *12,000* classes registered at runtime Try `Lumos.getAllClasses().count`\n\n\n\nYou could get the class hierarchy of any class just with:\n\n```swift\nmyObject.lumos.getClassHierarcy()   // For UIView: [UIView, UIResponder, NSObject]\n```\n*Fun Fact:* Some classes such as `URLSessionTask` are actually dummy classes which are replaced with underlying classes such as `__NSCFLocalSessionTask` during runtime.\n\nWith *lumos*, you can iterate through variables, functions, protocols etc and meddle with them at runtime. Have fun exploring!\n\n## Usage\nJust incantate `.lumos` on any instance of a `NSObject` subclass or use `Lumos.for(object)` for where `object` is of type `AnyClass`, `AnyObject`, `Protocol`, `Ivar`, `objc_property_t` or `objc_property_attribute_t`.\n\n\u003cimg width=\"728\" alt=\"LMMethod Methods\" src=\"https://user-images.githubusercontent.com/23443586/43019596-2127777a-8c90-11e8-9735-389171e59ff3.png\"\u003e\n\n\u003cimg width=\"728\" alt=\"LMClass Methods\" src=\"https://user-images.githubusercontent.com/23443586/43020763-b02b3314-8c93-11e8-852c-79e6365e556c.png\"\u003e\n\n\n\nP.s The code itself *is* the documentation for now. There are many more methods that *lumos* offers which are not discussed in this document. Cheers :)\n\n## Why ***lumos***?\n\nThe [Objective-C Runtime](https://developer.apple.com/documentation/objectivec/objective_c_runtime) provides many powerful methods to manipulate objects, classes and methods at runtime. Although disasterous when misused, these methods provide a great way to peek into the runtime and meddle with it.\n\n\nHowever, the methods are not exactly easy to use sometimes. For example the following method is used to obtain a list of all classes registered at runtime: \n```swift\nfunc objc_getClassList(_ buffer: AutoreleasingUnsafeMutablePointer\u003cAnyClass\u003e?, _ bufferCount: Int32) -\u003e Int32\n```\n\nOften, a lot of dirty work needs to be done before one gets the list out. Here is how I would do it:\n\n```swift\nstatic func getClassList() -\u003e [AnyClass] {\n    let expectedClassCount = objc_getClassList(nil, 0)\n    let allClasses = UnsafeMutablePointer\u003cAnyClass?\u003e.allocate(capacity: Int(expectedClassCount))\n\n    let autoreleasingAllClasses = AutoreleasingUnsafeMutablePointer\u003cAnyClass\u003e(allClasses)\n    let actualClassCount: Int32 = objc_getClassList(autoreleasingAllClasses, expectedClassCount)\n\n    var classes = [AnyClass]()\n    for i in 0 ..\u003c actualClassCount {\n        if let currentClass: AnyClass = allClasses[Int(i)] {\n            classes.append(currentClass)\n        }\n    }\n\n    allClasses.deallocate()\n    return classes\n}\n```\n\nNow all you would need to do to obtain the list of classes would be to invoke this method. Maybe you wish to get a list of classes that conform to a certain protocol:\n\n```swift\nstatic func classesImplementingProtocol(_ requiredProtocol: Protocol) -\u003e [AnyClass] {\n    return Lumos.getClassList().filter { class_conformsToProtocol($0, requiredProtocol) }\n}\n```\n\n\nPerhaps you wish to swizzle method implementations at runtime:\n\n```swift\nstatic func swizzle(originalClass: AnyClass, originalSelector: Selector, swizzledClass: AnyClass, swizzledSelector: Selector) {\n    guard let originalMethod = class_getInstanceMethod(originalClass, originalSelector),\n    let swizzledMethod = class_getInstanceMethod(swizzledClass, swizzledSelector) else {\n        return\n    }\n\n    let didAddMethod = class_addMethod(originalClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))\n\n    if didAddMethod {\n        class_replaceMethod(originalClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))\n    } else {\n        method_exchangeImplementations(originalMethod, swizzledMethod);\n    }\n}\n```\n\nYou can now use:\n```swift\nLumos.swizzle(originalClass: URLSessionTask,\n              originalSelector: #selector(URLSessionTask.resume),\n              swizzledClass: SwizzledSessionTask,\n              swizzledSelector: #selector(SwizzledSessionTask.resume))\n```\n\nP.S you might want to use `dispatch_once` with the method above to above swizzling more than once across multiple threads.\n\n## Installation\n\n### CocoaPods\n\n[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command:\n\n```bash\n$ gem install cocoapods\n```\n\nTo integrate *lumos* into your Xcode project using CocoaPods, specify it in your `Podfile`:\n\n```ruby\nsource 'https://github.com/CocoaPods/Specs.git'\nplatform :ios, '10.0'\nuse_frameworks!\n\ntarget '\u003cYour Target Name\u003e' do\n    pod 'Lumos'\nend\n```\n\nThen, run the following command:\n\n```bash\n$ pod install\n```\n\n## License\n\nLumos is released under the Apache-2.0. See [LICENSE](https://github.com/sushinoya/Lumos/blob/master/LICENSE) for details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsushinoya%2Flumos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsushinoya%2Flumos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsushinoya%2Flumos/lists"}