要求:
创建新UIViewController
,添加到UITabBarController
上,放入一个WKWebView
,让网页显示https://www.apple.com
-
添加
WebKit.framework
,新增WebViewController.swift
文件
-
添加
View Controller
,删除其视图,并添加WebKit View
,并使其与TabController
及相应swift
文件相关联
-
添加代码
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.");}
}
- 检测结果
要求:在
MapViewController
上增加UIButton
,点击按钮后让地图移动到用户当前位置
Hint:可能需用委托完成,参考MKMapViewDelegate
文档
- 设置跟踪用户地理位置所需要的参数并确认权限
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 NSLocationWhenInUseUsageDescription
,Value
只是作为弹窗的内容,这里可以随意设置
待更新。。。
参考资料:
MKMapView
使用Swift构建带有地理定位功能的App
iOS 定位服务与地图
要求
MKMapView
可以显示大头针,是MKPinAnnotationView
的实例。
在地图上添加三个大头针:出生位置,当前位置,有纪念意义的位置
添加按钮,点击后显示这三个大头针,不断点击切换
待更新…