0

他们是否有更好的方式以编程方式退出应用程序?

最近我做了一个无线电应用程序,它有一个用户设置来设置时间,使用 NSTimer 退出应用程序进程(我的意思是睡眠时间)。当时间到达应用程序应该停止其进程并退出。

当时间到达时,我使用这些语句退出应用程序,

  [[UIApplication sharedApplication] suspend];
  [[UIApplication sharedApplication] terminateWithSuccess];

theTimer=[NSTimer scheduledTimerWithTimeInterval:(1.0/1.0) target:self selector:@selector(countTime) userInfo:nil repeats:YES];

counter-=1;
timeLeft.text=[NSString stringWithFormat:@" %d",counter];
if (counter==0.0) {

    [theTimer invalidate];
  [[UIApplication sharedApplication] suspend];
  [[UIApplication sharedApplication] terminateWithSuccess];

使用 [[UIApplication sharedApplication] suspend] 有什么问题吗?[[UIApplication sharedApplication] terminateWithSuccess]; 方法

退出应用程序的任何其他更好的方法,或者至少冻结应用程序进程..我需要帮助吗?

4

3 回答 3

2

Apple 不会批准任何故意暂停或终止自身的应用程序。您只能使用官方文档中列出的方法,其中不包括suspendterminateWithSuccess

于 2012-03-16T07:54:20.647 回答
0

我一直使用exit(0)。我想如果你必须在应用程序退出之前运行代码,你应该在 exit(0) 之前调用它。

Apple不鼓励使用 exit(),因为从用户的角度来看,它看起来像一个崩溃。但他们也说没有 API 调用可以优雅地终止您的应用程序。

如果您正在寻找一种方法来终止您的应用程序而无需用户按下主页,在睡眠时间,我认为他不会将其与崩溃混淆,因为他不会关注您的应用程序并且 exit() 离开没有崩溃日志。

因此,您的选择是:

  • 忘记这一点,并在一段时间后发出哔哔声,提醒他关闭应用程序。(糟糕的!)
  • 使用您的私人电话关闭应用程序并冒被 Apple 拒绝的风险。
  • 使用 exit() 关闭应用程序并坚持使用 POSIX(已接受)调用。
于 2012-03-16T07:55:38.227 回答
0

tanq 用于重播,所以我忘记了以编程方式退出我的应用程序。下一个方法是停止播放我的 MPMoviePlayerController 对象。得到了下一个级别的问题

我的 sleepTime 设置页面和音乐播放页面,两者都是不同的 viewController,所以我无法在时间设置页面中直接访问 MPMoviePlayerController 对象,所以我在音乐播放页面上创建了一个方法

MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:@"http://www.radioparadise.com/musiclinks/rp_64aac.m3u"]];
[player prepareToPlay];
[player play];

- (IBAction)StopPlay:(id)sender 
{   
 [player stop];
}

所以我为音乐播放页面创建了一个对象,我从设置页面调用了这个方法,这是我在 settingsPage 中的代码

@class MusicPlayingPage;
 @interface secondView : UIViewController
{
MusicPlayingPage *audio; 
}

并在 settingsPage 中调用该方法

[audio stopPlay];

控制正确到达流媒体页面,但似乎播放器没有停止播放,我无法访问这些播放器选项中的任何一个 [播放器停止];, [玩家暂停];

musicPlayingPage 中此方法的任何问题

- (IBAction)StopPlay:(id)sender 
{   
 [player stop];
}

抱歉,如果我的问题无法理解。

于 2012-03-17T10:50:58.063 回答