views:

443

answers:

3

Why isn't the TerminateWithSuccess method of the SharedApplication implemented in MonoTouch? How can I invoke it? or even better, how can I programmatically close the application?

+2  A: 

terminateWithSuccess is private API, which is why MonoTouch does not surface it. You should still be able to call it by using a Selector.

        Selector s = new Selector("terminateWithSuccess");
        UIApplication.SharedApplication.PerformSelector(s, UIApplication.SharedApplication, 0)
Matt Greer
Keep in mind calling this method is not "kosher", Apple could change it without warning, and it could even potentially prevent your app from getting approved.
Matt Greer
I read about that in the forums and a lot of people got apps approved that were using the method. What is a kosher way to close the application?
Jonas Stawski
this method is pretty safe, I wouldn't worry about using it too much. But officially, you're not supposed to close the app at all. The user is supposed to close the app when they are done by hitting the physical button on the phone. `terminateWithSuccess` doesn't animate the closing, the app just goes away, which might make the user think your app crashed.
Matt Greer
In regards to application termination, this is what Apple says: "There is no API provided for gracefully terminating an iPhone application. Under the iPhone OS, the user presses the Home button to close applications. Should your application have conditions in which it cannot provide its intended function, the recommended approach is to display an alert for the user that indicates the nature of the problem and possible actions the user could take - turning on WiFi, enabling Location Services, etc. Allow the user to terminate the application at their own discretion."
Brad Larson
The previous is from http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html
Brad Larson
Thank you guys, I am displaying an UIAlert to the user saying they need internet connectivty and closing the app after they press OK. So I guess i'm ok.
Jonas Stawski
+1  A: 

After posting the question i figured it out, but Matt beat me to it. Here's another way:

UIApplication.SharedApplication.PerformSelector(new Selector("terminateWithSuccess"), null, 0f);

Jonas Stawski
Don't forget to read Matt's comment on his answer for important information about this method.
Jonas Stawski
Yeah I guess the NSObject parameter is a bit hazy to me. Most people just pass in null.
Matt Greer
I just re read my way and your way and it is exactly the same except for the second param. I guess I was way too tired and thought it was totaly diff. Either way works.
Jonas Stawski
A: 

FWIW- I received this last week- Looks like the time is UP for this method...

Thank you for submitting your update to xxxx to the App Store. During our review of your application we found it is using private APIs, which is in violation of the iPhone Developer Program License Agreement section 3.3.1; "3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs." While your application has not been rejected, it would be appropriate to resolve this issue in your next update.
The following non-public APIs are included in your application:
terminateWithSuccess
If you have defined methods in your source code with the same names as the above mentioned APIs, we suggest altering your method names so that they no longer collide with Apple's private APIs to avoid your application being flagged with future submissions. Please resolve this issue in your next update to xxx.

pixeled