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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
25 deletions
+78
-25
MRFramework/MRFramework/FoundationManagers/Location/MRLocation.swift
+78
-25
No files found.
MRFramework/MRFramework/FoundationManagers/Location/MRLocation.swift
View file @
955c41c1
...
...
@@ -9,6 +9,12 @@
import
Foundation
import
CoreLocation
func
MRLog
(
_
items
:
Any
...
)
{
#if DEBUG
print
(
items
)
#endif
}
// MARK: 定位管理器
public
class
MRLocation
:
NSObject
{
...
...
@@ -49,6 +55,7 @@ public class MRLocation: NSObject {
private
var
_status
:
LocationStatus
=
.
idle
private
var
callbacks
=
[
LocationCallback
]()
private
var
stayCallbacks
=
[
LocationCallback
]()
private
var
authCallback
:
((
_
status
:
CLAuthorizationStatus
)
->
Void
)?
override
init
()
{}
...
...
@@ -73,6 +80,17 @@ extension MRLocation {
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 stay: 是否常驻,若常驻,则这个回调在每次定位后都会调用
...
...
@@ -83,30 +101,42 @@ extension MRLocation {
if
!
isEnable
{
return
}
let
authorizate
=
authorization
?(
CLLocationManager
.
authorizationStatus
())
let
isStartLocating
=
authorizate
??
true
if
isStartLocating
{
/// 存储回调
if
let
finish
=
finish
{
let
callback
=
LocationCallback
(
callback
:
finish
)
if
stay
{
stayCallbacks
.
append
(
callback
)
}
else
{
callbacks
.
append
(
callback
)
let
start
:
(
_
status
:
CLAuthorizationStatus
)
->
Void
=
{
status
in
let
authorizate
=
authorization
?(
status
)
let
isStartLocating
=
authorizate
??
true
if
isStartLocating
{
/// 存储回调
if
let
finish
=
finish
{
let
callback
=
LocationCallback
(
callback
:
finish
)
if
stay
{
self
.
stayCallbacks
.
append
(
callback
)
}
else
{
self
.
callbacks
.
append
(
callback
)
}
}
}
if
_status
!=
.
locating
||
forc
e
{
switch
locationType
{
case
.
system
:
systemLocation
.
stop
UpdatingLocation
()
systemLocation
.
startUpdatingLocation
()
_status
=
.
locating
default
:
break
if
self
.
_status
!=
.
locating
||
force
{
switch
self
.
locationTyp
e
{
case
.
system
:
self
.
systemLocation
.
stopUpdatingLocation
()
self
.
systemLocation
.
start
UpdatingLocation
()
self
.
_status
=
.
locating
default
:
break
}
}
}
}
let
current
=
CLLocationManager
.
authorizationStatus
()
if
current
==
.
notDetermined
||
current
==
.
restricted
{
requestAuthorization
{
(
status
)
in
start
(
status
)
}
}
else
{
start
(
current
)
}
}
/// 停止定位
...
...
@@ -136,11 +166,7 @@ extension MRLocation {
private
func
setupSystemLocation
()
{
systemLocation
.
delegate
=
self
if
Bundle
.
main
.
infoDictionary
?[
"NSLocationWhenInUseUsageDescription"
]
!=
nil
{
systemLocation
.
requestWhenInUseAuthorization
()
}
else
if
Bundle
.
main
.
infoDictionary
?[
"NSLocationAlwaysUsageDescription"
]
!=
nil
{
systemLocation
.
requestAlwaysAuthorization
()
}
requestAuthorization
(
nil
)
systemLocation
.
distanceFilter
=
5
systemLocation
.
desiredAccuracy
=
300
//300米
}
...
...
@@ -159,6 +185,7 @@ extension MRLocation {
// MARK: CLLocationManagerDelegate
extension
MRLocation
:
CLLocationManagerDelegate
{
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didFailWithError
error
:
Error
)
{
MRLog
(
"location manager did fail:
\(
error
)
"
)
invokeCallbacks
(
with
:
nil
,
error
:
error
)
if
stayCallbacks
.
count
==
0
{
stopLocation
()
...
...
@@ -167,6 +194,7 @@ extension MRLocation: CLLocationManagerDelegate {
}
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didUpdateLocations
locations
:
[
CLLocation
])
{
MRLog
(
"location manager did update:
\(
locations
)
"
)
lastLocation
=
locations
.
last
invokeCallbacks
(
with
:
locations
.
last
,
error
:
nil
)
if
stayCallbacks
.
count
==
0
{
...
...
@@ -174,6 +202,11 @@ extension MRLocation: CLLocationManagerDelegate {
_status
=
.
idle
}
}
public
func
locationManager
(
_
manager
:
CLLocationManager
,
didChangeAuthorization
status
:
CLAuthorizationStatus
)
{
MRLog
(
"location manager authorization did change:
\(
status
.
desc
)
"
)
authCallback
?(
status
)
}
}
// MARK: 定位回调
...
...
@@ -184,3 +217,23 @@ public final class LocationCallback: NSObject {
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