当前位置: 代码迷 >> 综合 >> iOS Programming - Charpter 6 Chanllege
  详细解决方案

iOS Programming - Charpter 6 Chanllege

热度:117   发布时间:2023-09-21 23:27:06.0

要求:
创建新 UIViewController,添加到 UITabBarController 上,放入一个 WKWebView,让网页显示 https://www.apple.com

  1. 添加 WebKit.framework,新增 WebViewController.swift 文件
    iOS Programming - Charpter 6 Chanllege

  2. 添加 View Controller,删除其视图,并添加 WebKit View,并使其与 TabController 及相应 swift 文件相关联
    iOS Programming - Charpter 6 Chanllege

  3. 添加代码

import UIKit
import WebKitclass WebViewController: UIViewController, WKUIDelegate {
    var webView: WKWebView!override func loadView() {
    let webConfiguration = WKWebViewConfiguration()webView = WKWebView(frame: .zero, configuration: webConfiguration)webView.uiDelegate = selfview = webView}override func viewDidLoad() {
    super.viewDidLoad()let myURL = URL(string:"https://www.apple.com")let myRequest = URLRequest(url: myURL!)webView.load(myRequest)print("WebViewController loaded its view.");}
}
  1. 检测结果
    iOS Programming - Charpter 6 Chanllege

要求:在 MapViewController 上增加 UIButton,点击按钮后让地图移动到用户当前位置
Hint:可能需用委托完成,参考 MKMapViewDelegate 文档

  1. 设置跟踪用户地理位置所需要的参数并确认权限
import UIKit
import MapKit
import CoreLocationclass MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    @IBOutlet var mapView: MKMapView!let locationManager = CLLocationManager()override func viewDidLoad() {
    super.viewDidLoad()locationManager.delegate = selflocationManager.distanceFilter = kCLLocationAccuracyNearestTenMeterslocationManager.desiredAccuracy = kCLLocationAccuracyBestmapView.delegate = selfmapView.showsUserLocation = truemapView.userTrackingMode = .follow}override func viewDidAppear(_ animated: Bool ) {
    if CLLocationManager.authorizationStatus() == .notDetermined {
    locationManager.requestAlwaysAuthorization()}else if CLLocationManager.authorizationStatus() == .denied {
    }else if CLLocationManager.authorizationStatus() == .authorizedAlways {
    locationManager.startUpdatingLocation()}}

弹出授权定位的提示框,需要修改 Info.plist,添加如下两条属性,NSLocationAlwaysAndWhenInUseUsageDescription and NSLocationWhenInUseUsageDescriptionValue 只是作为弹窗的内容,这里可以随意设置
iOS Programming - Charpter 6 Chanllege
待更新。。。

参考资料:
MKMapView
使用Swift构建带有地理定位功能的App
iOS 定位服务与地图


要求
MKMapView 可以显示大头针,是 MKPinAnnotationView 的实例。
在地图上添加三个大头针:出生位置,当前位置,有纪念意义的位置
添加按钮,点击后显示这三个大头针,不断点击切换

待更新…

  相关解决方案