LoginSignup
1
3

More than 3 years have passed since last update.

[iOS] [Xcode] WKWebViewで位置情報を取得するコンテンツをloadする

Posted at

前提環境

  • Xcode 11.3
  • Swift 5.1

info.plistの設定

ユーザーから位置情報取得の許諾を得るための設定
スクリーンショット 2020-03-21 11.19.33.png

許諾アラートを日本語にする設定
スクリーンショット 2020-03-21 10.57.42.png
スクリーンショット 2020-03-21 10.58.11.png

謎の挙動

許諾アラートで「OK」をtapして、WKWebViewをいったん閉じて、再度WKWebViewを開いてもまた許諾アラートが表示されます。
2回目の「OK」で記憶されて、3回目は表示されなくなるようです。
この挙動の原因と回避方法は謎です。。。どなたかご存知でしたら教えてください。

注意点

Xcode 11.3で新規作成したプロジェクトでは、コンテンツが位置情報を取得しようとした際にコンソールに以下のログが出力されてクラッシュします。

*** Terminating app due to uncaught exception 'NSObjectNotAvailableException',
reason: 'UIAlertView is deprecated and unavailable for UIScene based applications,
please use UIAlertController!'

許諾アラートが表示されるタイミングで、Xcode 11で新規にプロジェクトを作成した時のデフォルトである「scene baseアプリケーション」の場合にクラッシュしてしまうようです。

許諾アラートはアプリ側で実装している訳ではないので、どうやらSDKのバグっぽい雰囲気です。
Xcode 11.3現在では、window baseアプリケーションにするしかなさそうです。

1.SceneDelegate.swiftをファイルごと削除

2.AppDelegate.swiftを修正

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow? // この行を追加

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

// ここから下を削除
//    // MARK: UISceneSession Lifecycle
//
//    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
//        // Called when a new scene session is being created.
//        // Use this method to select a configuration to create the new scene with.
//        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
//    }
//
//    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
//        // Called when the user discards a scene session.
//        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
//        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
//    }

}

3.info.plistから"Application Scene Manifestを削除
スクリーンショット 2020-03-21 10.43.12.png

この件については、Xcodeがバージョンアップされたら再確認しようと思います。

1
3
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
1
3