質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2775閲覧

swift バックグラウンドでタイマーを使い現在時間をコンソールに表示したのですが失敗しました

comet0310

総合スコア59

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/01/06 07:25

編集2017/01/06 09:26

イメージ説明delegateの部分にこのように記述してコンソールに現在時間を表示しようとしたのですがシミュレータをバックグラウンドで作動させても何の反応もありませんでした。
教えてくださると幸いです。
projectのtargetから以下のように設定してあります。
イメージ説明

import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? let userDefaults = UserDefaults.standard func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum) return true } private func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { print("enter") let now = NSDate() let formatter = DateFormatter() formatter.dateFormat = "yyyy/MM/dd HH:mm:ss" let string = formatter.string(from: now as Date) print(string+"ifhdoifgodhgoidhfoihf") completionHandler(UIBackgroundFetchResult.newData) } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Background Fetchが動作しない
ここを参考にして、Background Fetchのシミュレーションを行ってみて下さい。

【追記】
引数の最初に_(アンダーバー)を追加して下さい。

swift

1func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

投稿2017/01/06 07:35

編集2017/01/06 08:36
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

comet0310

2017/01/06 08:13

同様の手順でdebugを使ってシミュレーションしましたが何も反応しませんでした。コード自体が間違ってるのでしょうか
fuzzball

2017/01/06 08:32

Background Fetchに関するコードは間違って無さそうですが‥。 performFetchWithCompletionHandlerの最初に print("enter") を入れてみてもらえますか?
fuzzball

2017/01/06 08:45

回答に追記しました。
comet0310

2017/01/06 08:54

_ を追加してprint("enter")と記述しましたがコンソールでは何も表示されませんでした。
fuzzball

2017/01/06 09:08

アンダーバーの後ろはスペース入れてますか?(質問内のコードを更新して下さい。必ずコピペで)
comet0310

2017/01/06 09:15

スペース入れてます
fuzzball

2017/01/06 09:19

privateを消してみて下さい。なぜ付けたのでしょうか?
fromageblanc

2017/01/06 09:20

もしかしたらですが、backgroundだとデバッガにアタッチできないかもしれません。printをalertかなんかに変えてみてください
comet0310

2017/01/06 09:25

privateを消したらできました!ですが以下のような警告文が出てクリックするとprivateをつけさせられます。
fuzzball

2017/01/06 09:44 編集

Swift3環境が無いので確認できないのですが、これでどうでしょうか?(@escapingを追加) func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問