0

I trying to understand the automagic of @UIApplicationMain and how to visualize the start of an an iOS app in terms of Java:

public class UIApplication extends UIResponder implements Runnable {   
    final UIApplicationDelegate appDel;
    public UIApplication(UIApplicationDelegate appDel) {
        this.appDel = appDel;
    }

    public static void main(String[] args) {
        try {
            UIApplication app = new UIApplication(new AppDelegate());

            handThisReferenceToOperatingSystem(app);
            iOSdoesSomethingLikeThis(new Thread(app).start());    
        } catch(Exception e) { e.printStackTrace(); }
    }

    public void run() {
        // chill-out and wait for iOS to invoke methods in UIResponder class.
        // The UIResponder methods invoke my custom methods in AppDelegate.
    }

            public static class AppDelegate implements UIApplicationDelegate {
                public void application(Object UIApplication) { // app specific behaviour
                }
                public void applicationWillResignActive(Object UIApplication) {   // app specific behaviour
                }
                public void applicationDidEnterBackground(Object UIApplication) {  // app specific behaviour
                }
                public void applicationWillEnterForeground(Object UIApplication) {  // app specific behaviour
                }
                public void applicationDidBecomeActive(Object UIApplication) {  // app specific behaviour
                }
                public void applicationWillTerminate(Object UIApplication) {  // app specific behaviour
                }
                // maybe more methods from the UIApplicationDelegate
            }

           public interface UIApplicationDelegate {
               void application(Object UIApplication);
               void applicationWillResignActive(Object UIApplication);
               void applicationDidEnterBackground(Object UIApplication);
               void applicationWillEnterForeground(Object UIApplication);
               void applicationDidBecomeActive(Object UIApplication);
               void applicationWillTerminate(Object UIApplication);
               // maybe some more methods ....
           }
}

public class UIResponder {
    void fingerSwipe() { // default implementation  
    }
    void verticalMotion() { // default implementation
    }
    // more methods iOS might invoke 
}

So basically, applying the @UIApplicationMain attribute to the AppDelegate class makes all the other code go away, right?

Just Someone
  • 253
  • 3
  • 9

1 Answers1

0

Check out this answer: https://stackoverflow.com/a/24516329/335974

The gist is that it generates the main.m file found in Objective-C projects:

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSString *appDelegateClassName = @"AppDelegate";

        return UIApplicationMain(argc, argv, nil, appDelegateClassName);
    }
}

So your Java code looks like in the ballpark of what happens.

Community
  • 1
  • 1
Felipe Cypriano
  • 2,727
  • 22
  • 32
  • When I think abstractly about software and about "what's going on", I think in Java. I can see your reference, but it doesn't "click" because I'm not that familiar with other languages. – Just Someone Aug 10 '16 at 21:59
  • It just creates the `public static void main(String[] args)` for you and pass your `AppDelegate` to the `UIApplicationMain` which is the framework class that starts up the app and calls your delegate at specific points. – Felipe Cypriano Aug 10 '16 at 22:03