★ UIApplicationオブジェクト取得(Singleton)
var application = UIApplication.shared
★ 自動スリープモードの設定
// アプリ起動中自動スリープしない
application.idleTimerDisabled = true
// アプリ起動中自動スリープする(Default)
application.idleTimerDisabled = false
★ ステータスバーのインジケーターの表示を設定
// 表示する
UIApplication.shared.isNetworkActivityIndicatorVisible = true
// 表示しない
UIApplication.shared.isNetworkActivityIndicatorVisible = false
★ アイコンにバッジを表示する
// ユーザーに許可を求めるダイアログは表示されないが以下の設定を書かないとバッジは表示されない
// ※ 戻り値を使っていないのでワーニングは出る
UIUserNotificationSettings(forTypes: [.Badge], categories:nil)
// バッジを123に設定
UIApplication.shared.applicationIconBadgeNumber = 123
// バッジを非表示にする
UIApplication.shared.applicationIconBadgeNumber = 0
★ RootViewControllerを取得する
// rootViewControllerがUIViewControllerの場合
let rootView = UIApplication.shared.keyWindow?.rootViewController as! ViewController
rootView.view.backgroundColor = UIColor.yellowColor()
// rootViewControllerがUINavigationControllerの場合
let rootNavi = UIApplication.shared.keyWindow?.rootViewController as! UINavigationController
let rootView = rootNavi.viewControllers[0] as! ViewController
rootView.view.backgroundColor = UIColor.yellowColor()
// rootViewControllerがUITabBarControllerの場合
let tabView = UIApplication.shared.keyWindow?.rootViewController as! UITabBarController
let rootView = tabView.viewControllers![0] as! ViewController
rootView.view.backgroundColor = UIColor.yellowColor()
★ TabBarItemにバッジを付ける
let tabView = UIApplication.shared.keyWindow?.rootViewController as! UITabBarController
let rootView = tabView.viewControllers![0] as! ViewController
rootView.tabBarItem.badgeValue = "123"
★ URL Schemeからの起動時に呼ばれるハンドラ
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return true
}
★ Lineアプリに画像を渡して起動する
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func openLineApp(_ sender: UIButton) {
guard let sendImage = UIImage(named: "swift"),
let image = UIImagePNGRepresentation(sendImage) else {
return
}
let pasteBoard = UIPasteboard.general
pasteBoard.setData(image, forPasteboardType: "public.png")
let urlString = "line://msg/image/\(pasteBoard.name as CVarArg)"
if let url = URL(string: urlString), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, completionHandler: nil)
} else {
// - LINEがインストールされていない場合の処理
}
}
}
★ バックグラウンド更新を有効にしているかどうかを取得する
let application = UIApplication.shared
if application.backgroundRefreshStatus == .available {
print("バックグラウンド更新が可能")
} else if application.backgroundRefreshStatus == .denied {
print("バックグラウンド更新はユーザによって禁止されている")
} else if application.backgroundRefreshStatus == .restricted {
print("デバイス設定により無効にされている(ユーザが有効にすることはできない)")
}