LoginSignup
14
12

More than 5 years have passed since last update.

ソースコードでアプリをバックグラウンドにいかせる

Last updated at Posted at 2016-04-04

アプリをプログラムで終了させる事は勧められていない(リジェクトされる?)ので、ホーム画面に戻すようにしようと思い調べてみた。
ライフサイクル的にはバックグラウンド→サスペンドなので、下記メソッドも呼ばれているという認識。

  • 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))
}
14
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
12