How MobileSubstrate Works

Discussion in 'iOS Jailbreak & Cydia' started by Axis, Feb 20, 2010.

  1. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    Preface:

    Many of you have MobileSubstrate installed on your devices. Many of you use MobileSubstrate extensions/add-ons. I would like everyone to learn a bit about how it works, and perhaps you'll gain an appreciation for the things you may take for granted.

    Fully understanding what MobileSubstrate does requires grounding in the Objective-C runtime and programming concepts. Of course, I do not expect any of you to have that knowledge, and I will not be overly technical in my writing.

    Some Background Info:

    The iPhone operating system, along each one of your applications, comprises these things called 'objects'. Objects are an abstraction used by programmers to effectively write programs and to foster reusable code.

    Some examples of objects: Every icon on your SpringBoard is an object. So is the "slide to unlock" bar, and the pop-up messages that you see here and there. Those are all objects represented on the screen (we'll call them 'views'). However, there are even more objects behind the scenes. For example, some objects communicate with servers. Others parse XML, store data, or open and write to files. Objects typically do a certain job, which allows programmers to accomplish a task by using combinations of them.

    Objects can "own" other objects. For example, the icon objects on your SpringBoard own a label object, which displays the name of the app. See how each object does a specific task and how beneficial that is?

    Objects, obviously, can do things. We'll refer to the actions of objects as 'methods'. Ready for an analogy? Objects are to nouns, as methods are to verbs. Got it?

    Programmers can tell objects to do certain things. Typically, this is referred to as "calling a method on an object".

    An object is a data structure—it stores data in a certain way—that can perform certain actions.

    So what does this have to do with MobileSubstrate extensions like QuickReplySMS or SBSettings?

    Here we go with analogies again.

    Methods are like verbs. Verbs have a defined meaning. The verb "obfuscate" means "to make unclear".

    Methods also have "meanings" (we'll call these implementations). An implementation is basically what happens when the method is called. More technical: the code executed when a method is called. In Obj-C specifically, a method name is bound to an implementation (which is function pointer) at runtime

    So let's think of methods and their implementations just like verbs and their meanings.

    We find the meanings of verbs in a dictionary. For now, let's think of there being a "dictionary" of methods and their implementations also.

    MobileSubstrate allows programmers to effectively "change"/"replace" the implementation of methods. So it's just like someone editing a dictionary, resulting in a verb having a new meaning.

    The "crazy" part is that the new meaning is in effect whenever anybody uses the verb. The new implementation is used whenever the method is called.

    Let's look at SBSettings for example. It's slides onto your screen whenever you swipe the status bar. Well, guess what—there's a method that gets called on some object whenever the status bar gets swiped.

    The makers of SBSettings "edited" the the implementation of that method, so their extension could appear on the screen.

    Another example is AppLocket. When it's enabled, it prevents applications from being opened. How does that work? It's simple.

    Every icon on your SpringBoard is an object, remember? Each one has a "launch" method, which does exactly what you think it does.

    When enabled, AppLocket "edits" the implementation of the "launch" method, which results in an annoying pop-up message appearing instead of your app.

    Conclusion:

    This whole thing may be pretty hard to understand. I'm a programmer myself, so it is difficult for me to explain this in non-technical terms. However, even though some of my comparisons are overly simplified, I think I did a decent job. Please tell me what you don't understand, or what could be phrased batter, and I'll make adjustments.

    I hope you gained some knowledge and appreciation!
    Joe Rossignol likes this.
  2. Awesome

    Awesome Banned

    Joined:
    Jun 16, 2009
    Messages:
    1,107
    Likes Received:
    0
    Device:
    iPhone 4 (Black)
    Wow i can actually understand this!

    Please Register or Log in to view images

  3. Bubalooshi

    Bubalooshi Active Member

    Joined:
    May 22, 2008
    Messages:
    2,432
    Likes Received:
    0
    Device:
    iPhone 3GS (Black)
    I can also understand this.

    I've known about MobileSubstrate, but never really knew how it operated.
    This is a nice thing to know, I guess.

    Please Register or Log in to view images



    Thanks!

    ...Did this have anything to do with that PM I sent you a while ago?

    Please Register or Log in to view images



    You did a pretty good job explaining it.
  4. _JKK_

    _JKK_ Super Moderator Staff Member

    Joined:
    Jun 8, 2009
    Messages:
    9,514
    Likes Received:
    1,366
    Device:
    iPhone 5S
    Nice semi-tutorial. I understood it too

    Please Register or Log in to view images



    And yeah, you did do a nice job of explaining it.
  5. tinyman392

    tinyman392 Super Moderator Staff Member

    Joined:
    Jul 10, 2008
    Messages:
    16,015
    Likes Received:
    1,380
    Device:
    iPhone 6
    Great job explaining how it works... Could get a little bit more in depth (might not be possible). This is a great thing for beginners to learn and understand, if they plan to start programming anyway. Good instruction on how individual apps take advantage of replacing "methods"/"functions" (I call them functions now, but I also accept methods). Also a good intro to what an object is (still very vague, but better than my definition, object = "thing".) Overall, this can also be used in the development section of the forum. Good for beginners to understand how programming actually works. This is a lot of typing so I'll stop here.

    *My OPPINION: This should get STICKIED!
  6. ChronicFunk

    ChronicFunk New Member

    Joined:
    Mar 14, 2009
    Messages:
    289
    Likes Received:
    0
    Device:
    iPhone 4 (Black)
    Nice article.

    but...
    I'm sorry, but screw you. How can you just assume that everyone isn't appreciative for what the development community has given all of us? I think you need to re-word that to say 'some' because I know a lot of us here appreciate what has been given to us.
  7. SilentWalker

    SilentWalker Retired Moderator

    Joined:
    Jul 26, 2009
    Messages:
    1,323
    Likes Received:
    10
    Device:
    2G iPod touch
    Wow! Thanks a lot! I was actually wondering this exact same thing - how MobileSubstrate works - just yesterday. And you did a wonderful job of explaining it

    Please Register or Log in to view images



    A couple things I think you can expand on are:
    • Winterboard - Almost every person who jailbreaks, jailbreaks for this reason. Can you explain the object-method-implementation concept with relation to Winterboard?
    • And an extension to the above point - how and why, in somewhat technical terms does Winterboard, and most MobileSubstrate extensions slow your device and increase battery drain? I got rid of Winterboard, and unjailbroke my iPod for this exact reason.

    Thanks again. A very well done article.
  8. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    I was not clear enough. I assume most people are grateful for products/tools of the jailbreak community. Many people do not however, care much about how they work. That is what I sought to address, not the fact that people worked hard to make these things. If that were my goal, I would have focused on the technical side of things.

    WinterBoard really isn't all that detrimental to battery life or memory usage. WinterBoard simply causes the system to load different images into memory every time SprinBoard starts. That's memory that would have been used anyway.

    As for memory-hogging/battery draining MobileSubstrate extensions, that is the responsibility of the creator of the extension. Memory management is critical, because if an extension leaks, memory will be lost until SpringBoard restarts (which is not good).

    An extension like Backgrounder would hurt battery if a lot of apps are left running, but that's just common sense.

    Of course, if users have a ridiculous amount of extensions, they may experience issues, but for the most part, reports are exaggerated/isolated.

    Also, a well-coded MS extension is more power/memory efficient than an app that runs in the background.
  9. flyingguitar

    flyingguitar Active Member

    Joined:
    May 14, 2008
    Messages:
    1,860
    Likes Received:
    11
    Device:
    iPhone 6
    being an objective-C dev myself I can understand this very clearly. and I must say that you did a very good job explaining it (much better then i could've anyway) and I also learned a small thing or 2.

    I also understand that making a .dylib (dynamic library) the format that you need to compile for to make a mobile substrate plug-in can't be compiled like your average iPhone SDK app, so I assuming you would use toolchain? I'm just curious to see how you would go about making a plug-in. thanks
  10. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    xcode won't let you "normally" build a dylib for iPhone, however, SkylarEC posted an xcode template, which sets the dylib flags, renames the binary, and shed's the app bundle for you.

    You can compile on Mac without using xcode (compile with /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin9-gcc-4.2.1 and use a makefile with all the flags and maybe even an install script.

    Talk to me on AIM sometime if you want to learn more.

Share This Page