1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Basic structure for Nit apps on iOS
22 #import <UIKit/UIKit.h>
24 // Our interface to the iOS system
25 @interface AppDelegate: UIResponder <UIApplicationDelegate>
28 @property (strong, nonatomic) UIWindow *window;
34 // Global reference to the App from app.nit
37 // Our own C argc and argv
39 char **app_nit_ios_argv;
41 @implementation AppDelegate
43 - (BOOL)application:(UIApplication *)application
44 willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
46 // Set aside `application
` to be used from Nit
47 App_ui_application__assign(app_nit_ios_app, application);
48 App_app_delegate__assign(app_nit_ios_app, self);
50 // Complete the callback
51 return App_will_finish_launching_with_options(app_nit_ios_app);
54 - (BOOL)application:(UIApplication *)application
55 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
57 return App_did_finish_launching_with_options(app_nit_ios_app);
60 - (void)applicationWillResignActive:(UIApplication *)application {
61 App_will_resign_active(app_nit_ios_app);
64 - (void)applicationDidEnterBackground:(UIApplication *)application {
65 App_did_enter_background(app_nit_ios_app);
68 - (void)applicationWillEnterForeground:(UIApplication *)application {
69 App_will_enter_foreground(app_nit_ios_app);
72 - (void)applicationDidBecomeActive:(UIApplication *)application {
73 App_did_become_active(app_nit_ios_app);
76 - (void)applicationWillTerminate:(UIApplication *)application {
77 App_will_terminate(app_nit_ios_app);
83 # Application interface to the iOS system
84 extern class AppDelegate in "ObjC" `{ AppDelegate * `}
87 # Graphical application to which events are sent
88 extern class UIApplication in "ObjC" `{ UIApplication * `}
93 # Main graphical application
94 var ui_application
: UIApplication
96 # Application interface to the iOS system
97 var app_delegate
: AppDelegate
99 # Copy back to C the command line arguments
101 # Nit extracts the first arguments from the `args` sequence,
102 # so we need to add it back. That's why Nit's `args` is smaller than in C.
103 private fun register_args
(program_name
: CString, argc
: Int,
104 argv
: Sequence[String]) import Sequence[String].[], String.to_cstring
in "ObjC" `{
105 app_nit_ios_argc = (int)(argc+1);
107 // TODO copy or pin the strings when needed
108 app_nit_ios_argv = malloc(argc * sizeof(char*));
109 app_nit_ios_argv[0] = program_name;
110 for (int i = 0; i < argc; i ++) {
111 String arg = Sequence_of_String__index(argv, i);
112 app_nit_ios_argv[i+1] = String_to_cstring(arg);
116 # Register `self` globally in C so it can be retrieved from iOS callbacks
117 private fun register_globally
in "ObjC" `{
119 app_nit_ios_app = self;
122 # Entry point to the iOS framework
123 private fun ui_application_main
: Bool import did_finish_launching_with_options
,
124 will_finish_launching_with_options
,
125 will_resign_active
, did_enter_background
, will_enter_foreground
,
126 did_become_active
, will_terminate
, ui_application
=, app_delegate
= in "ObjC" `{
129 return UIApplicationMain(app_nit_ios_argc, app_nit_ios_argv,
130 nil, NSStringFromClass([AppDelegate class]));
134 # The application is about to launch
136 # Redef this method to set the very first custom code to be executed.
137 fun will_finish_launching_with_options
: Bool do return true
139 # The application just launched but is not yet displayed to the user
141 # Redef this method to customize the behavior.
142 fun did_finish_launching_with_options
: Bool
149 # The application is about to move from active to inactive state
151 # This can occur for certain types of temporary interruptions
152 # (such as an incoming phone call or SMS message) or when the
153 # user quits the application and it begins the transition to
154 # the background state.
156 # Redef this method to pause ongoing tasks, disable timers, and
157 # throttle down OpenGL ES frame rates. Games should use this
159 fun will_resign_active
do on_pause
161 # The application just left foreground it can be suspended at any time
163 # Redef this method to release shared resources, save user data,
164 # invalidate timers, and store application state to restore your
165 # application to its current state in case it is terminated later.
167 # If your application supports background execution, this method
168 # is called instead of `will_terminate` when the user quits.
169 fun did_enter_background
175 # The application will enter the foreground
177 # Called as part of the transition from the background to the
180 # Redef to undo changes made on entering the background.
181 fun will_enter_foreground
do on_restart
183 # The application just became active
185 # Redef to restart any tasks that were paused (or not yet started) while
186 # the application was inactive. If the application was previously
187 # in the background, optionally refresh the user interface.
188 fun did_become_active
do on_resume
190 # The application is about to terminate (from a state other than suspended)
192 # Redef to save data if appropriate.
195 # Usually a forced termination by the system
202 app
.register_args
(program_name
.to_cstring
, args
.length
, args
)
203 app
.register_globally
205 var ret
= app
.ui_application_main
206 exit
if ret
then 0 else 1