実現したいこと
AppDelegate.swiftにて特定の通信結果を終了してからイニシャル画面に遷移するという挙動を実現したいです。
発生しているエラーメッセージ
そのためにDispatchGroupを使って以下の実装を試みております。通信処理前でenterして処理後でleaveし、notifyを使ってイニシャル画面を設定するという実装をしているのですが以下のようなメッセージが表示されてしまいます。
Application windows are expected to have a root view controller at the end of application launch
おそらくnotifyが機能していないからだとは思うのですがなぜ機能しないのでしょうか?enterやleaveの場所をいろいろかえてみたりもしたのですがうまくいかない状態です。(ちなみに通信処理さえしなければ問題なくイニシャルページは表示されます。)
皆様のお力添えいただけますと幸いです。
何卒よろしくお願いいたします。
該当のソースコード
swift
1let dispatchGroup = DispatchGroup() 2 3func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 4 5 self.window = UIWindow(frame: UIScreen.main.bounds) 6 let storyboard = UIStoryboard(name: "Main", bundle: nil) 7 8 //購入済みのユーザーかRealmからデータを取得して確認 9 let fetchedSubscriptions = self.realm.objects(Subscription.self).sorted(byKeyPath: "updatedTime", ascending: false) 10 11 //SubscriptionがvalidがあればそのままTodayViewControllerへ遷移 12 if fetchedSubscriptions.first?.valid == true { 13 14 //もしrealmから最新のsubscription情報を取得してexpireDateが空ならレシート検証を行ってexpireDateを更新 15 if fetchedSubscriptions.first?.expireDate == 0 { 16 17 self.receiptValidation(url: "https://sandbox.itunes.apple.com/verifyReceipt") 18 19 } 20 21 //receiptValidationの通信が終了したらMain画面を表示 22 dispatchGroup.notify(queue: .main) { 23 24 let initialViewController = storyboard.instantiateViewController(withIdentifier: "MainPageView") 25 self.window?.rootViewController = initialViewController 26 self.window?.makeKeyAndVisible() 27 28 } 29 30 } else { 31 32 //そうでない場合はWelcomePageを表示 33 34 let initialViewController = storyboard.instantiateViewController(withIdentifier: "Welcome") 35 self.window?.rootViewController = initialViewController 36 self.window?.makeKeyAndVisible() 37 38 } 39 40 return true 41}
swift
1func receiptValidation(url: String) { 2 3 dispatchGroup.enter() 4 5 //(中略) 6 7 let session = URLSession.shared 8 let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in 9 10 //(中略) 11 12 self.dispatchGroup.leave() 13 14 }) 15 task.resume() 16}
補足情報(言語/FW/ツール等のバージョンなど)
Swift 5.0.1
Xcode 10.3
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。