Giving or changing the implementation of method at runtime in iOS - ios

Recently i caught with the a thought of changing the implementation of method after deployment
When i googled about objective c runtime and all, came to know about method swizzling methodExchangeImplementations etc.
I know that it could be possible by https://rollout.io/
But my thought is how to do Hot Patching by myself for simple things.
my idea is injecting the code using webservice call.
Webservice should give a replacement for particular method.
That string has to be converted to executable code
What i want to know is ...
How to inject the code in existing method of enterprise application.
For ex:
Consider this method in objective c
-(void)fetchTheResult{
// some code lines
}
After deployment i would like to change the method implementation to
-(void)fetchTheresult{
NSLog(#"test log");
//some Code lines
//some more lines
}
Please guide me the way to achieve this

This is a big question and you've some research to do to figure out an answer. Here as some things you can look into:
First you've referenced Rollout, you could follow the same idea and send your update as JavaScript. You'll need to study how to call JavaScript, swizzle methods, and probably dynamically add methods - more on that in a moment.
An alternative you can investigate is dynamic library loading - you can open, and call code in, a library which your app loads at runtime. So you could look at sending your update as a library. You'll still need to do method swizzling and probably dynamically add methods...
As well as method swizzling you may find you need to dynamically add methods - e.g. so you have something to swap the existing implementation to. A good place to find out how to do that is Mike Ash's writings on KVO - go DuckDuckGo (or Google)
HTH

It is not as easy as you think, at least in Objective C and other similar compiled languages. This kind of runtime changes to the code is only possible in interpreted languages like Javascript.
The basic problem is, the apps are not allowed to change the executable files themselves. The apps on iOS and Android run in a sandboxed environment, and thus have access to limited disk locations only.
Also, after compiling the code, the code does not know where the part of code is converted and stored in machine language. You have to understand the basics of compilers to understand this. There are heavy optimisations happening to your code during this process.

Related

Binding technology to use for iOS app

I am a novice iOS app developer. Basically i need to listen for any change in the UI (e.g., doing something on text change in textviewUI), and update UI to reflect any model change. I was looking into different technologies available for binding and am bit overwhelmed.
I know this is very subjective, but i would like to hear your suggestions on it (for swift and objective c both)
Also any pointers to the best practices would be really helpful.
Welcome to iOS! Cocoa (Touch for iOS)- Apple's framework- has some design patterns you will want to learn...
These include delegation and target/ action. Quickly summarized: delegation is handing some responsibilities off to an object that can take care of them. This provides methods you can use to get stuff (like changes) to that object. Read this link to learn more about delegates: https://useyourloaf.com/blog/quick-guide-to-swift-delegates/
Now, back to the text field example. A handy method in the UITextField delegate called textField(_:shouldChangeCharactersIn:replacementString:) will be called whenever the text field is going to change. As for target/ action, that's google-able. Is that a word? As for any pointers, learn MVC. Have fun!

Table with methods, how to handle/inspect them?

So without the context, I have a method that returns a table.
Trying to print it out, it seems that the table mainly contains methods to be called.
However not being very knowledgeable in LUA, i have no idea how to properly get some information about these methods i should call.
I tried to get documentation from the creator of the thing, but there is none as far as i know. And since this is inside computercraft (minecraft mod) i don't have a lot of features to rely on either.
So knowing only that it is a table with methods that i can call, how do i properly figure out how to call them, and with what parameters etc. ?
Generally speaking, modules/ libraries always come with docs, or a method to print the docs.
But if this is not the case, here's what you can do:
You can print everything in the table! This is a must, the names of the methods can be very useful
You can seek out help! Find people who have used the same module, and ask them how it works. Why solve something others have already figured out?
Use debug.getinfo and other hacky functions for the debug library! They can provide more info than anything else in the Lua standard libraries!
C-Side coding can reveal what Lua cannot! If you have access to the C-Side you can see exactly what the code is doing (or at least I think so)
Check out the source code! This shows you what the code does and how it does it
And above all else, experiment! Try the methods on different parameters, different values, and identify what it does through continuous testing!
Just knowing the names of the methods is not enough to figure how to call them.
Their names may be a clue, but there is no guarantee.
If the methods are written in Lua, you could use the debug API to discover the names of parameters, which again may just be an indication of how to call the methods.
Bottom line: you need documentation or example code.
I knew this existed, didn't know how it worked. So for future reference:
You can dump your peripherals and methods by doing /op_dump in the minecraft chat.
This generates an XML which writes out all methods it has found in peripherals OR objects/tables.
This means that you have to call every interesting method once, which generates the table as return. And then calling /op_dump will include that newly encountered object with all information about there methods/parameters etc.

Why is there no Objective C AddressBook Framework for iOS, and how to best tackle the problems with C in Swift?

This might be really generic and rather about the framework in general than a programming question. But, in the light of Swift, and the tedious and sometimes impossible tasks you have interacting with C APIs, that question is very relevant.
When building a new app for iOS, I discovered that you can really have a hard time working with address book framework. First, there is the uncomfortable pointer passing that you have to do for many CoreFoundation Methods. Secondly, the functions mostly return that ugly Unmanaged objects, where you have to figure out if they are retained or not (ARC is several years old now!). Accessing the properties through their identifiers is terribly cumbersome and far from typesafe. And lastly, since there is no C Function Pointer Support yet, you can´t even call ABAddressBookRegisterExternalChangeCallback(addressBook: ABAddressBook!, callback: ABExternalChangeCallback, context: UnsafeMutablePointer<Void>) because the ABExternalChangeCallback is so far only defined in Objective-C!
Then I found that there is some nice Objective-C Api in the Mac OS Version of AddressBookFramework. How unfair! Isn´t the iOS Frameworks younger? Why do you think Apple did this? And when will they change this in your opinion? Did I miss something, and is there an Objective-C Api for iOS, too?
Any suggestions for how to tackle above problems in the most convenient and beautiful way are welcome, too! For my part, I´m writing a complete wrapper to obscure all the nasty pointer- C-Function- and global constants uglyness. As soon as it´s ready I´ll commit it to StackExchange and maybe Github to let others benefit and discuss my solution.
EDIT: finally managed to upload my wrapper to GitHub. See
https://github.com/SocialbitGmbH/SwiftAddressBook
I agree with you about what iOS provides to access to the address book.
I've posted an answer explaining how I handled the problem, using some functional aspects of swift, and how I dealt with extracting unmanaged objects.
Briefly:
I defined a custom operator to allow me chaining function calls to transform some input data
I implemented 2 generic functions to extract unmanaged objects
posted some code to show how I access to the address book, loop through all contacts, and retrieve some data for each one

iOS Jailbroken devices development: How to dump method calls

I am pretty new to development for iOS devices with jailbreak. From what I am reading I understand that to be able to do all the cool things which you can't do on non-jailbroken phones you have to hook up to a given class and override some of its behaviour. Since there is no documentation how a developer tracks to which class exactly he should hook?
I imagine that for instance if I wanted to have my app respond to a given event such as phone boot, call hang up or user clicking on an icon I would manually generate the given event and see what invocations have been made. Is this the proper way to track where you should hook your code and if yes how is it done.
Note I am not interested in exactly those events mentioned above I am more interested the approach in general.
There are several approaches:
Disassemble binaries
You can disassemble a binary or just dump classes with something like class-dump.
So, you can see the whole hierarhy of classes.
Find dumped classes
Most of major iOS subsystems were dissasembled by somebody already. You can find quite a lot of useful stuff.
As example. Google search "Springboard headers" got this
Dump classes in a runtime.
Look at this question for explanation: List selectors for Objective-C object

API hooks discrepency

I have been trying to understand the concept of 'hooking' APIs and am currently facing a problem. I hope that experts here can clear it:
By going through a header file I noticed that the LoadLibrary function is actually mapped to one of the following two: LoadLibraryW or LoadLibraryA based on the flag used during compilation (UNICODE or ASCII, respectively). Now, if I have explicitly hooked the API 'LoadLibraryA' then will my hooked function get the control if it is compiled using the UNICODE flag?
It would be great if someone could explain or point me to a link that explains in detail how hooking works in such cases? How do compilation flags, character sets, locale, internationalization, impact hooking of APIs like LoadLibrary or CreateFile?
I'm currently in South Korea (Originally from UK) and for some reason some of my application is not working and I have a demo tomorrow. I presume the above mentioned doubt is the reason for the problem I'm facing.
I think that you need to read carefully this bible of api hooking Hooking in theory which explains everything about hooking like different approaches of hooking, lot of diagrams.... and also this one example. My suggestion is to use LoadLibraryW because it is superset of Acsii.
Bye

Resources