简体   繁体   English

在 xcode11 中为 ios13 获取错误的用户当前位置

[英]Getting wrong User’s Current Location in xcode11 for ios13

I'm building ios app without using storyboard and I need to get user current location for my app.我正在构建 ios 应用程序而不使用 storyboard,我需要为我的应用程序获取用户当前位置。

When I run stimulator and click 'Allow While Using App' or 'Allow Once', it detects wrong location.当我运行刺激器并单击“使用应用程序时允许”或“允许一次”时,它会检测到错误的位置。 There is no error shown.没有显示错误。

Here is my code:这是我的代码:

  • AppDelegate.swift 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
    }
}
  • ViewController.swift ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
    
    var webView: WKWebView!
    
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)

        // [1] Since we're using auto layout, this ignores the frame and
        // considers only the auto-layout constraints
        webView.translatesAutoresizingMaskIntoConstraints = false

        let myURL = URL(string:"https://www.yourapp.com")
        let myRequest = URLRequest(url: myURL!)

        // [2] Adds the webview as a subview of the view
        view.addSubview(webView)
        webView.load(myRequest)

        // [3] Pins the webview to the safe area layout guides.
        // From the documentation: When the view is visible onscreen, this guide
        // reflects the portion of the view that is not covered by navigation bars,
        // tab bars, toolbars, and other ancestor views.
        // https://developer.apple.com/documentation/uikit/uiview/2891102-safearealayoutguide
        NSLayoutConstraint.activate([
            webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        ])
    }
}

Beside that, I remove SceneDelegate file and add privacy - location WhenInUse and AlwaysAndWhenInUse in info plist.除此之外,我删除了 SceneDelegate 文件并在 info plist 中添加了隐私位置 WhenInUse 和 AlwaysAndWhenInUse。

In simulator you can set "your device" location in Debug→Location, it will not use your MacBook/actual location在模拟器中,您可以在调试→位置中设置“您的设备”位置,它不会使用您的 MacBook/实际位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM