Commit 67d6c023 by Sarkizz

添加定位功能

parent f4ab9d2b
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
A70210542320FE8B009F8BC6 /* MRCheckBoxImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70210512320FE8B009F8BC6 /* MRCheckBoxImage.swift */; }; A70210542320FE8B009F8BC6 /* MRCheckBoxImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70210512320FE8B009F8BC6 /* MRCheckBoxImage.swift */; };
A7021067232742D7009F8BC6 /* UIImage+util.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7021066232742D7009F8BC6 /* UIImage+util.swift */; }; A7021067232742D7009F8BC6 /* UIImage+util.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7021066232742D7009F8BC6 /* UIImage+util.swift */; };
A702106A23277B2B009F8BC6 /* DataURLParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = A702106923277B2B009F8BC6 /* DataURLParser.swift */; }; A702106A23277B2B009F8BC6 /* DataURLParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = A702106923277B2B009F8BC6 /* DataURLParser.swift */; };
A7295040235E949B006A5D6A /* MRLocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A729503F235E949B006A5D6A /* MRLocation.swift */; };
A73E5C8F23136605000C379B /* UIColor+util.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73E5C8E23136605000C379B /* UIColor+util.swift */; }; A73E5C8F23136605000C379B /* UIColor+util.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73E5C8E23136605000C379B /* UIColor+util.swift */; };
A73E5C922313767C000C379B /* UIButton+util.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73E5C912313767C000C379B /* UIButton+util.swift */; }; A73E5C922313767C000C379B /* UIButton+util.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73E5C912313767C000C379B /* UIButton+util.swift */; };
A73E5C95231376D6000C379B /* NamespaceWrappable.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73E5C94231376D6000C379B /* NamespaceWrappable.swift */; }; A73E5C95231376D6000C379B /* NamespaceWrappable.swift in Sources */ = {isa = PBXBuildFile; fileRef = A73E5C94231376D6000C379B /* NamespaceWrappable.swift */; };
...@@ -80,6 +81,7 @@ ...@@ -80,6 +81,7 @@
A70210512320FE8B009F8BC6 /* MRCheckBoxImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MRCheckBoxImage.swift; sourceTree = "<group>"; }; A70210512320FE8B009F8BC6 /* MRCheckBoxImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MRCheckBoxImage.swift; sourceTree = "<group>"; };
A7021066232742D7009F8BC6 /* UIImage+util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+util.swift"; sourceTree = "<group>"; }; A7021066232742D7009F8BC6 /* UIImage+util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+util.swift"; sourceTree = "<group>"; };
A702106923277B2B009F8BC6 /* DataURLParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataURLParser.swift; sourceTree = "<group>"; }; A702106923277B2B009F8BC6 /* DataURLParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataURLParser.swift; sourceTree = "<group>"; };
A729503F235E949B006A5D6A /* MRLocation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MRLocation.swift; sourceTree = "<group>"; };
A73E5C8E23136605000C379B /* UIColor+util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+util.swift"; sourceTree = "<group>"; }; A73E5C8E23136605000C379B /* UIColor+util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+util.swift"; sourceTree = "<group>"; };
A73E5C912313767C000C379B /* UIButton+util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIButton+util.swift"; sourceTree = "<group>"; }; A73E5C912313767C000C379B /* UIButton+util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIButton+util.swift"; sourceTree = "<group>"; };
A73E5C94231376D6000C379B /* NamespaceWrappable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NamespaceWrappable.swift; sourceTree = "<group>"; }; A73E5C94231376D6000C379B /* NamespaceWrappable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NamespaceWrappable.swift; sourceTree = "<group>"; };
...@@ -204,6 +206,14 @@ ...@@ -204,6 +206,14 @@
path = Parser; path = Parser;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
A729503E235E948F006A5D6A /* Location */ = {
isa = PBXGroup;
children = (
A729503F235E949B006A5D6A /* MRLocation.swift */,
);
path = Location;
sourceTree = "<group>";
};
A73E5C93231376C9000C379B /* Namespace */ = { A73E5C93231376C9000C379B /* Namespace */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
...@@ -324,6 +334,7 @@ ...@@ -324,6 +334,7 @@
A7B8E156230E300200999DF2 /* FoundationManagers */ = { A7B8E156230E300200999DF2 /* FoundationManagers */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
A729503E235E948F006A5D6A /* Location */,
A702106823277B16009F8BC6 /* Parser */, A702106823277B16009F8BC6 /* Parser */,
A73E5C9B23137F32000C379B /* HotUpdate */, A73E5C9B23137F32000C379B /* HotUpdate */,
A73E5C93231376C9000C379B /* Namespace */, A73E5C93231376C9000C379B /* Namespace */,
...@@ -566,6 +577,7 @@ ...@@ -566,6 +577,7 @@
A7B8E161230E319700999DF2 /* SAPhotoPicker.swift in Sources */, A7B8E161230E319700999DF2 /* SAPhotoPicker.swift in Sources */,
A7021017231CC60C009F8BC6 /* MRScanningLineView.swift in Sources */, A7021017231CC60C009F8BC6 /* MRScanningLineView.swift in Sources */,
A702101B231CC60C009F8BC6 /* ScanningAVManager.swift in Sources */, A702101B231CC60C009F8BC6 /* ScanningAVManager.swift in Sources */,
A7295040235E949B006A5D6A /* MRLocation.swift in Sources */,
A73E5C922313767C000C379B /* UIButton+util.swift in Sources */, A73E5C922313767C000C379B /* UIButton+util.swift in Sources */,
A70210532320FE8B009F8BC6 /* MRAlbumCell.swift in Sources */, A70210532320FE8B009F8BC6 /* MRAlbumCell.swift in Sources */,
A7B8E142230B9F9C00999DF2 /* MRFiles.swift in Sources */, A7B8E142230B9F9C00999DF2 /* MRFiles.swift in Sources */,
......
//
// MRLocation.swift
// MRFramework
//
// Created by Sarkizz on 2019/10/22.
// Copyright © 2019 sarkizz. All rights reserved.
//
import Foundation
import CoreLocation
// MARK: 定位管理器
public class MRLocation: NSObject {
/// 系统定位完成回调
public typealias MRLocationSystemFinish = (_ location: CLLocation?, _ error: Error?) -> Void
/// 定位授权回调。在每次调用startLocation的时候都会调用一次,返回false则不定位
public typealias MRlocationAuthorization = (_ status: CLAuthorizationStatus) -> Bool
/// 定位类型
public enum LocationType {
case system
}
/// 定位状态
public enum LocationStatus {
case idle
case locating
}
public static let shared = MRLocation()
/// 当前定位状态
public var status: LocationStatus {
get {
return _status
}
}
/// 根据状态判断是否定位的回调
public var authorization: MRlocationAuthorization?
/// 最后一次获取的定位
public var lastLocation: CLLocation?
private lazy var systemLocation: CLLocationManager = {
let manager = CLLocationManager()
return manager
}()
private var locationType: LocationType = .system {
didSet {
locationTypeDidChange()
}
}
private var _status: LocationStatus = .idle
private var callbacks = [LocationCallback]()
public convenience init(type: LocationType = .system) {
self.init()
locationType = type
}
/// 开始定位
/// - Parameter finish: 定位回调
/// - Parameter stay: 是否常驻,若常驻,则这个回调在每次定位后都会调用
/// - Parameter force: 是否强制定位。强制定位会无视当前状态直接调用定位函数
public func startLocation(finish: MRLocationSystemFinish? = nil,
stay: Bool = false, force: Bool = false) {
let isStartLocating = authorization?(CLLocationManager.authorizationStatus()) ?? true
if isStartLocating {
/// 存储回调
if let finish = finish {
let c = LocationCallback(callback: finish, isStay: stay)
callbacks.append(c)
}
if _status != .locating || force {
switch locationType {
case .system:
systemLocation.startUpdatingLocation()
_status = .locating
}
}
}
}
/// 停止定位
public func stopLocation() {
switch locationType {
case .system:
systemLocation.stopUpdatingLocation()
}
}
}
// MARK: Private
extension MRLocation {
private func locationTypeDidChange() {
switch locationType {
case .system:
setupSystemLocation()
}
}
private func setupSystemLocation() {
systemLocation.delegate = self
systemLocation.requestWhenInUseAuthorization()
}
private func invokeCallbacks(with location: CLLocation?, error: Error?) {
callbacks = callbacks.filter({ c in
c.callback(location, error)
return c.isStay
})
}
}
// MARK: CLLocationManagerDelegate
extension MRLocation: CLLocationManagerDelegate {
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
invokeCallbacks(with: nil, error: error)
_status = .idle
}
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
lastLocation = locations.last
invokeCallbacks(with: locations.last, error: nil)
_status = .idle
}
}
// MARK: 定位回调
public final class LocationCallback: NSObject {
var callback: MRLocation.MRLocationSystemFinish
var isStay: Bool
public init(callback: @escaping MRLocation.MRLocationSystemFinish, isStay: Bool) {
self.callback = callback
self.isStay = isStay
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment