How to create shared instance of my app? "appDel is an AppDelegate shared instance of your application"
-
What do you need to know exactly? – Deepak Danduprolu May 28 '11 at 13:55
-
I cant understand this one: //appDel is an AppDelegate shared instance of your application [appDel.window addSubview: [wikitudeAR start]]; – user622203 May 28 '11 at 14:20
-
in this post: http://stackoverflow.com/questions/5128634/how-to-implement-wikitude-api-in-iphone-application/5146600#5146600 – user622203 May 28 '11 at 14:26
-
He is mentioning that `appDel` is the application delegate of the shared instance of the `UIApplication` which is obtained by doing `[[UIApplication sharedApplication] delegate]`. – Deepak Danduprolu May 28 '11 at 15:16
-
so this should be ok: [[[UIApplication sharedApplication] delegate].window addSubview: [wikitudeAR start]]; because is not! – user622203 May 28 '11 at 15:35
-
Basically `[[UIApplication sharedApplication] delegate]` returns a `id
` object which doesn't have a window object. You need to cast it to whatever your application delegate class is. – Deepak Danduprolu May 28 '11 at 15:36
2 Answers
How to create shared instance of my app?
The application instance is created in the UIApplicationMain
function. You can access this object by invoking the sharedApplication
class method.
"appDel is an AppDelegate shared instance of your application"
[[UIApplication sharedApplication] delegate]
will give you a pointer to your application delegate.

- 28,492
- 6
- 64
- 71
You don't have to create it. The shared instance (singleton) is always there, assuming that you're using the standard main.m
implementation that is calling NSApplicationMain()
. If not, a call to [NSApplication sharedApplication]
will also cause it to be created.
To access the shared instance of NSApplication, use either
[NSApplication sharedApplication];
or simply use the global variable
NSApp
To access the app delegate's shared instance, use
[[NSApplication sharedApplication] delegate];
or simply
[NSApp delegate]
In the text you quoted, I would assume appDel
to be a variable that is assigned the return value of [NSApp delegate]
in order to save a few characters typing when it is used very often.

- 6,071
- 1
- 25
- 30
-
actually is this one and I don't get it appDel is an AppDelegate shared instance of your application [appDel.window addSubview: [wikitudeAR start]]; – user622203 May 28 '11 at 14:02
-
when i type this: [[NSApplication sharedApplication] delegate]; xcode asking me to replace nsaplication with uiapplication – user622203 May 28 '11 at 14:08
-
I'm sorry, I didn't notice the iPhone tag. On an iPhone, it's of course `UIApplication` instead of `NSApplication`, and there's no comparable shorthand for `NSApp`, so you'll always have to write `[UIApplication sharedApplication]`. – puzzle May 31 '11 at 21:13
-
`appDel` in your example would probably be the object returned by `[[UIApplication sharedApplication] delegate]`. – puzzle May 31 '11 at 21:17