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

来源:互联网 发布:剑倚天下灵骑进阶数据 编辑:程序博客网 时间:2024/05/17 04:47

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


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



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

几个状态的切换
application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:
applicationWillResignActive:
applicationDidEnterBackground:
applicationWillEnterForeground:
applicationWillTerminate:



之前可以重写

- (void)applicationDidEnterBackground:(UIApplication *)application {
    //home键时执行直接退出程序
    [[UIApplication sharedApplication] terminateWithSuccess];
}

但terminateWithSuccess是一个私有的方法,appstore的审核是通不过的,还有种方法是把terminateWithSuccess换成“exit(0);”,这行代码也是可以实现按home键退出程序的功能,听说能通过审核。

 

exit代码如下:


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);
    
}


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


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


0 0
原创粉丝点击