IOS 退出程序代码

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:self.exitapplication message:@"" delegate:self cancelButtonTitle:self.exityes otherButtonTitles:self.exitno,nil];
        
  [alert show];


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    if(buttonIndex ==0){
        
        
        [self exitApplication ];
        
    }

}

- (void)exitApplication {
    
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    UIWindow *window = app.window;
    
    [UIView animateWithDuration:1.0f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
    //exit(0);
    
}

用此方法可能会在上传AppStore时被拒

[self performSelector:@selector(notExistCall)];  
abort();

苹果原则上是反对这样做的,他们认为应用的生命周期是由用户来控制的,要退出就按HOME键

exit(0) ? 还有人敢用?? 为什么这么说? 好像在OS还在2.*的时候,苹果就已经禁止了这个方法, 原因是容易让用户产生”产品质量不好”的错觉.
调用个不存在的方法让程序崩溃 比如: [self performSelector:@selector(Ih8ThisButICantStopU)];
raise一个NSException (其实原理是一样的)
abort(); 苹果官方的例子中好多地方用到了这个,所以应该是安全



Q:怎样用代码方式退出IOS程序


       A:没有提供用于正常退出IOS应用的API。


       在IOS中,用户点击Home键来关闭应用。你的应用应该符合以下条件:它不能自行调用方法,而应采取措施与用户交互,表明问题的性质和应用可能会采取的行为,比如打开WIFI,使用定位服务等供用户选择确定使用;


       警告:不要使用exit函数,调用exit会让用户感觉程序崩溃了,不会有按Home键返回时的平滑过渡和动画效果;另外,使用exit可能会丢失数据,因为调用exit并不会调用-applicationWillTerminate:方法和UIApplicationDelegate方法;


如果在开发或者测试中确实需要强行终止程序时,推荐使用abort 函数和assert宏;


iOS代码退出程序一般直接调用exit(0); ,但是视觉上看起来像闪退,于是,我借鉴了下网上别人的代码,写了一个向下切出退出程序的方法,代码如下:


- ( void )exitApplication {
     AppDelegate *app = [UIApplication sharedApplication].delegate;
     UIWindow *window = app.window;
     
     [UIView animateWithDuration: 1 .0f animations:^{
         window.alpha = 0 ;
         window.frame = CGRectMake( 0 , window.bounds.size.width, 0 , 0 );
     } completion:^(BOOL finished) {
         exit( 0 );
     }];
     
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值