2018-06-25 饿了么iOS隐私政策条款不同意并退出App

2018-06-25  本文已影响0人  HaloMartin

印象里,iOS除了使用Home键,并不能主动退出,今天中午订餐的时候,打开了许久没用的饿了么,意外发现,饿了么有不同意并退出的功能,百度尝试了一下,只是很简单的一段代码而已,效果和数据安全且不论,但是确实可以实现退出App。
退出App的三种方法,exit(1)、abort()、assert(0)
源码在底部:

1.exit(1)

附加了关闭打开文件与返回状态码给执行环境,并调用你用atexit注册的返回函数
iOS终止函数exit
调用exit会让用户感觉程序崩溃了,不会有按Home键返回时的平滑过渡和动画效果;另外,使用exit可能会丢失数据,因为调用exit并不会调用-applicationWillTerminate:方法和UIApplicationDelegate方法

2.abort()

解除进程对SIGABRT信号的阻止,然后向调用进程发送该信号,abort()可以导致进程的异常终止,除非SIGABRT信号被处理,可以通过NSSetUncaughtExceptionHandler来捕捉,从而可以做到从容的退出App,参考资料如下:RunLoop总结:RunLoop的应用场景(五)阻止App崩溃一次

捕捉并处理异常效果图

3.assert(0)

Assert断言

当程序检查到断言条件为false时,触发断言,而后程序崩溃,可在日志中读到相关的过程,在正式发布的Release版中,一般都是会禁用断言功能的;
assert(0)同样可以用捕捉处理abort()的方式进行捕捉,因为assert最终还是会调用abort()产生SIGABRT信号来终止App

总结

OC并不提供正常退出iOSApp的API,上面三种方式虽然可以退出App,但不符合苹果一贯的友好风格,所以也是有可能上架悲剧。
源码:

//
//  AppExitTestViewController.m
//  Modulator
//
//  Created by M君 on 25/06/2018.
//  Copyright © 2018 huihaiweishi. All rights reserved.
//

#import "AppExitTestViewController.h"

typedef NS_ENUM(NSUInteger, AppExitTestTag) {
    AppExitTestTag_Exit = 100,
    AppExitTestTag_Abort,
    AppExitTestTag_Assert,
};

@interface AppExitTestViewController ()


@end

@implementation AppExitTestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    //
    UIButton *exitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    exitBtn.frame = CGRectMake(0, 0, 100, 60);
    exitBtn.layer.borderWidth = 1.0;
    exitBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    exitBtn.tag = AppExitTestTag_Exit;
    exitBtn.backgroundColor = [UIColor lightGrayColor];
    [exitBtn setTitle:@"Exit" forState:UIControlStateNormal];
    [exitBtn addTarget:self action:@selector(onAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:exitBtn];
    //
    UIButton *abortBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    abortBtn.frame = CGRectMake(0, 0, 100, 60);
    abortBtn.layer.borderWidth = 1.0;
    abortBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    abortBtn.tag = AppExitTestTag_Abort;
    abortBtn.backgroundColor = [UIColor lightGrayColor];
    [abortBtn setTitle:@"Abort" forState:UIControlStateNormal];
    [abortBtn addTarget:self action:@selector(onAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:abortBtn];
    //
    UIButton *assertBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    assertBtn.frame = CGRectMake(0, 0, 100, 60);
    assertBtn.layer.borderWidth = 1.0;
    assertBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    assertBtn.tag = AppExitTestTag_Assert;
    assertBtn.backgroundColor = [UIColor lightGrayColor];
    [assertBtn setTitle:@"Assert" forState:UIControlStateNormal];
    [assertBtn addTarget:self action:@selector(onAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:assertBtn];
    //
    abortBtn.center = self.view.center;
    exitBtn.center = CGPointMake(abortBtn.centerX, abortBtn.top - exitBtn.height/2 - 10);
    assertBtn.center = CGPointMake(abortBtn.centerX, abortBtn.bottom + exitBtn.height/2 + 10);
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - action
- (void)onAction:(UIButton *)btn
{
    NSString *title = @"Terminate the App";
    NSString *message = [NSString stringWithFormat:@"Will Terminate the App with Button: %@",btn.titleLabel.text];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        AppExitTestTag tag = (AppExitTestTag)btn.tag;
        switch (tag) {
            case AppExitTestTag_Exit:
                exit(1);
                break;
            case AppExitTestTag_Abort:
                abort();
                break;
            case AppExitTestTag_Assert:
                assert(0);
                break;
            default:
                break;
        }
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
上一篇 下一篇

猜你喜欢

热点阅读