Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
zhangyongji
/
MRFramework
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
955c41c1
authored
Nov 14, 2019
by
Sarkizz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
调整定位逻辑
parent
2b5183b3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
66 additions
and
13 deletions
+66
-13
MRFramework/MRFramework/FoundationManagers/Location/MRLocation.swift
+66
-13
No files found.
MRFramework/MRFramework/FoundationManagers/Location/MRLocation.swift
View file @
955c41c1
...
@@ -9,6 +9,12 @@
...
@@ -9,6 +9,12 @@
import
Foundation
import
Foundation
import
CoreLocation
import
CoreLocation
func
MRLog
(
_
items
:
Any
...
)
{
#if DEBUG
print
(
items
)
#endif
}
// MARK: 定位管理器
// MARK: 定位管理器
public
class
MRLocation
:
NSObject
{
public
class
MRLocation
:
NSObject
{
...
@@ -49,6 +55,7 @@ public class MRLocation: NSObject {
...
@@ -49,6 +55,7 @@ public class MRLocation: NSObject {
private
var
_status
:
LocationStatus
=
.
idle
private
var
_status
:
LocationStatus
=
.
idle
private
var
callbacks
=
[
LocationCallback
]()
private
var
callbacks
=
[
LocationCallback
]()
private
var
stayCallbacks
=
[
LocationCallback
]()
private
var
stayCallbacks
=
[
LocationCallback
]()
private
var
authCallback
:
((
_
status
:
CLAuthorizationStatus
)
->
Void
)?
override
init
()
{}
override
init
()
{}
...
@@ -73,6 +80,17 @@ extension MRLocation {
...
@@ -73,6 +80,17 @@ extension MRLocation {
return
CLLocationManager
.
locationServicesEnabled
()
return
CLLocationManager
.
locationServicesEnabled
()
}
}
/// 请求获取定位权限
/// - Parameter completion: 回调
public
func
requestAuthorization
(
_
completion
:
((
_
status
:
CLAuthorizationStatus
)
->
Void
)?)
{
authCallback
=
completion
if
Bundle
.
main
.
infoDictionary
?[
"NSLocationWhenInUseUsageDescription"
]
!=
nil
{
systemLocation
.
requestWhenInUseAuthorization
()
}
else
if
Bundle
.
main
.
infoDictionary
?[
"NSLocationAlwaysUsageDescription"
]
!=
nil
{
systemLocation
.
requestAlwaysAuthorization
()
}
}
/// 开始定位
/// 开始定位
/// - Parameter finish: 定位回调
/// - Parameter finish: 定位回调
/// - Parameter stay: 是否常驻,若常驻,则这个回调在每次定位后都会调用
/// - Parameter stay: 是否常驻,若常驻,则这个回调在每次定位后都会调用
...
@@ -83,25 +101,27 @@ extension MRLocation {
...
@@ -83,25 +101,27 @@ extension MRLocation {
if
!
isEnable
{
if
!
isEnable
{
return
return
}
}
let
authorizate
=
authorization
?(
CLLocationManager
.
authorizationStatus
())
let
start
:
(
_
status
:
CLAuthorizationStatus
)
->
Void
=
{
status
in
let
authorizate
=
authorization
?(
status
)
let
isStartLocating
=
authorizate
??
true
let
isStartLocating
=
authorizate
??
true
if
isStartLocating
{
if
isStartLocating
{
/// 存储回调
/// 存储回调
if
let
finish
=
finish
{
if
let
finish
=
finish
{
let
callback
=
LocationCallback
(
callback
:
finish
)
let
callback
=
LocationCallback
(
callback
:
finish
)
if
stay
{
if
stay
{
stayCallbacks
.
append
(
callback
)
self
.
stayCallbacks
.
append
(
callback
)
}
else
{
}
else
{
callbacks
.
append
(
callback
)
self
.
callbacks
.
append
(
callback
)
}
}
}
}
if
_status
!=
.
locating
||
force
{
if
self
.
_status
!=
.
locating
||
force
{
switch
locationType
{
switch
self
.
locationType
{
case
.
system
:
case
.
system
:
systemLocation
.
stopUpdatingLocation
()
self
.
systemLocation
.
stopUpdatingLocation
()
systemLocation
.
startUpdatingLocation
()
self
.
systemLocation
.
startUpdatingLocation
()
_status
=
.
locating
self
.
_status
=
.
locating
default
:
default
:
break
break
}
}
...
@@ -109,6 +129,16 @@ extension MRLocation {
...
@@ -109,6 +129,16 @@ extension MRLocation {
}
}
}
}
let
current
=
CLLocationManager
.
authorizationStatus
()
if
current
==
.
notDetermined
||
current
==
.
restricted
{
requestAuthorization
{
(
status
)
in
start
(
status
)
}
}
else
{
start
(
current
)
}
}
/// 停止定位
/// 停止定位
public
func
stopLocation
()
{
public
func
stopLocation
()
{
if
!
isEnable
{
if
!
isEnable
{
...
@@ -136,11 +166,7 @@ extension MRLocation {
...
@@ -136,11 +166,7 @@ extension MRLocation {
private
func
setupSystemLocation
()
{
private
func
setupSystemLocation
()
{
systemLocation
.
delegate
=
self
systemLocation
.
delegate
=
self
if
Bundle
.
main
.
infoDictionary
?[
"NSLocationWhenInUseUsageDescription"
]
!=
nil
{
requestAuthorization
(
nil
)
systemLocation
.
requestWhenInUseAuthorization
()
}
else
if
Bundle
.
main
.
infoDictionary
?[
"NSLocationAlwaysUsageDescription"
]
!=
nil
{
systemLocation
.
requestAlwaysAuthorization
()
}
systemLocation
.
distanceFilter
=
5
systemLocation
.
distanceFilter
=
5
systemLocation
.
desiredAccuracy
=
300
//300米
systemLocation
.
desiredAccuracy
=
300
//300米
}
}
...
@@ -159,6 +185,7 @@ extension MRLocation {
...
@@ -159,6 +185,7 @@ extension MRLocation {
// MARK: CLLocationManagerDelegate
// MARK: CLLocationManagerDelegate
extension
MRLocation
:
CLLocationManagerDelegate
{
extension
MRLocation
:
CLLocationManagerDelegate
{
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didFailWithError
error
:
Error
)
{
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didFailWithError
error
:
Error
)
{
MRLog
(
"location manager did fail:
\(
error
)
"
)
invokeCallbacks
(
with
:
nil
,
error
:
error
)
invokeCallbacks
(
with
:
nil
,
error
:
error
)
if
stayCallbacks
.
count
==
0
{
if
stayCallbacks
.
count
==
0
{
stopLocation
()
stopLocation
()
...
@@ -167,6 +194,7 @@ extension MRLocation: CLLocationManagerDelegate {
...
@@ -167,6 +194,7 @@ extension MRLocation: CLLocationManagerDelegate {
}
}
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didUpdateLocations
locations
:
[
CLLocation
])
{
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didUpdateLocations
locations
:
[
CLLocation
])
{
MRLog
(
"location manager did update:
\(
locations
)
"
)
lastLocation
=
locations
.
last
lastLocation
=
locations
.
last
invokeCallbacks
(
with
:
locations
.
last
,
error
:
nil
)
invokeCallbacks
(
with
:
locations
.
last
,
error
:
nil
)
if
stayCallbacks
.
count
==
0
{
if
stayCallbacks
.
count
==
0
{
...
@@ -174,6 +202,11 @@ extension MRLocation: CLLocationManagerDelegate {
...
@@ -174,6 +202,11 @@ extension MRLocation: CLLocationManagerDelegate {
_status
=
.
idle
_status
=
.
idle
}
}
}
}
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didChangeAuthorization
status
:
CLAuthorizationStatus
)
{
MRLog
(
"location manager authorization did change:
\(
status
.
desc
)
"
)
authCallback
?(
status
)
}
}
}
// MARK: 定位回调
// MARK: 定位回调
...
@@ -184,3 +217,23 @@ public final class LocationCallback: NSObject {
...
@@ -184,3 +217,23 @@ public final class LocationCallback: NSObject {
self
.
callback
=
callback
self
.
callback
=
callback
}
}
}
}
// MARK: CLAuthorizationStatus
extension
CLAuthorizationStatus
{
var
desc
:
String
{
switch
self
{
case
.
notDetermined
:
return
"notDetermined"
case
.
restricted
:
return
"restricted"
case
.
denied
:
return
"denied"
case
.
authorizedAlways
:
return
"authorizedAlways"
case
.
authorizedWhenInUse
:
return
"authorizedWhenInUse"
@unknown
default
:
return
"unknow"
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment