アプリをプログラムで終了させる事は勧められていない(リジェクトされる?)ので、ホーム画面に戻すようにしようと思い調べてみた。
ライフサイクル的にはバックグラウンド→サスペンドなので、下記メソッドも呼ばれているという認識。
- applicationWillResignActive
- applicationDidEnterBackground
サンプルコード
objc_sample.m
UIApplication *app = [UIApplication sharedApplication];
if ([app respondsToSelector:@selector(suspend)]) {
[app performSelector:@selector(suspend)];
}
Swift2.0
swift_sample.swift
let app = UIApplication.sharedApplication()
if app.respondsToSelector(#selector(NSURLSessionTask.suspend)) {
app.performSelector(#selector(NSURLSessionTask.suspend))
}
Swift4.0
swift_sample.swift
let app = UIApplication.shared
if app.responds(to: #selector(URLSessionTask.suspend)) {
app.perform(#selector(URLSessionTask.suspend))
}