@@ -15,7 +15,8 @@ public enum Karte {
15
15
///
16
16
/// - Parameter app: a navigation app supported by Karte
17
17
/// - Returns: `true` if the app is installed
18
- /// - Warning: For this to return `true` in any case, the necessary url schemes have to be included in your app's Info.plist.
18
+ /// - Warning: For this to return `true` in any case, the necessary url schemes have to be
19
+ /// included in your app's Info.plist.
19
20
/// Please see Karte's README for additional details.
20
21
public static func isInstalled( _ app: App ) -> Bool {
21
22
guard app != . appleMaps else { return true } // FIXME: See issue #3
@@ -27,7 +28,8 @@ public enum Karte {
27
28
///
28
29
/// - Parameters:
29
30
/// - app: the app to be launched
30
- /// - origin: an optional origin location, defaults to the user's locatino if left empty in most apps
31
+ /// - origin: an optional origin location, defaults to the user's locatino if left empty in
32
+ /// most apps
31
33
/// - destination: the location to route to
32
34
public static func launch( app: App ,
33
35
origin: LocationRepresentable ? = nil ,
@@ -39,10 +41,12 @@ public enum Karte {
39
41
///
40
42
/// - Parameters:
41
43
/// - app: the app to be launched
42
- /// - origin: an optional origin location, defaults to the user's location if left empty in most apps
44
+ /// - origin: an optional origin location, defaults to the user's location if left empty in
45
+ /// most apps
43
46
/// - destination: the location to route to
44
47
/// - mode: mode of transport to use
45
- /// - Throws: `Karte.Error.unsupportedMode` if the chosen mode is not supported by the target app
48
+ /// - Throws: `Karte.Error.unsupportedMode` if the chosen mode is not supported by the target
49
+ /// app
46
50
public static func launch( app: App ,
47
51
origin: LocationRepresentable ? = nil ,
48
52
destination: LocationRepresentable ,
@@ -56,13 +60,19 @@ public enum Karte {
56
60
mode: Mode ? ) throws {
57
61
guard app != . appleMaps else {
58
62
guard app. supports ( mode: mode) else { throw Error . unsupportedMode }
59
- // If mode (as in the launchOptions below) stays nil, Apple Maps won't go directly to the route, but show search boxes with prefilled content instead.
60
- let modeKey = ( mode? . identifier ( for: . appleMaps) as? [ String : String ] ) ?? [ MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDefault]
61
- MKMapItem . openMaps ( with: [ origin, destination] . compactMap { $0? . mapItem } , launchOptions: modeKey)
63
+ let mapItems = [ origin, destination] . compactMap { $0? . mapItem }
64
+ // If mode (as in the launchOptions below) stays nil, Apple Maps won't go directly to
65
+ // the route, but show search boxes with prefilled content instead.
66
+ let modeKey = ( mode? . identifier ( for: . appleMaps) as? [ String : String ] )
67
+ ?? [ MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDefault]
68
+ MKMapItem . openMaps ( with: mapItems, launchOptions: modeKey)
62
69
return
63
70
}
64
71
65
- guard let queryString = app. queryString ( origin: origin, destination: destination, mode: mode) else {
72
+ guard let queryString = app. queryString ( origin: origin,
73
+ destination: destination,
74
+ mode: mode)
75
+ else {
66
76
throw Error . unsupportedMode
67
77
}
68
78
@@ -74,13 +84,17 @@ public enum Karte {
74
84
UIApplication . shared. open ( url, completionHandler: nil )
75
85
}
76
86
77
- /// Return a `UIAlertController` with all supported apps the device has installed to offer an option for which app to start.
78
- /// Use this instead of `Karte.presentPicker()` if you want to control the presentation of the alert view controller manually.
87
+ /// Return a `UIAlertController` with all supported apps the device has installed to offer an
88
+ /// option for which app to start.
89
+ /// Use this instead of `Karte.presentPicker()` if you want to control the presentation of the
90
+ /// alert view controller manually.
79
91
///
80
92
/// - Parameters:
81
- /// - origin: an optional origin location, defaults to the user's location if left empty in most apps
93
+ /// - origin: an optional origin location, defaults to the user's location if left empty in
94
+ /// most apps
82
95
/// - destination: the location to route to
83
- /// - mode: an optional mode of transport to use, results in only those apps being shown that support this mode
96
+ /// - mode: an optional mode of transport to use, results in only those apps being shown that
97
+ /// support this mode
84
98
/// - title: an optional title for the `UIAlertController`
85
99
/// - message: an optional message for the `UIAlertController`
86
100
/// - cancel: label for the cancel button, defaults to "Cancel"
@@ -92,15 +106,19 @@ public enum Karte {
92
106
title: String ? = nil ,
93
107
message: String ? = nil ,
94
108
cancel: String = " Cancel " ,
95
- style: UIAlertControllerStyle = . actionSheet) -> UIAlertController {
109
+ style: UIAlertControllerStyle = . actionSheet)
110
+ -> UIAlertController {
96
111
let alert = UIAlertController ( title: title, message: message, preferredStyle: style)
97
112
98
113
App . all
99
114
. filter ( self . isInstalled)
100
115
. filter { $0. supports ( mode: mode) } // defaults to true if mode is nil
101
116
. map { app in
102
117
return UIAlertAction ( title: app. name, style: . default, handler: { _ in
103
- try ? self . _launch ( app: app, origin: origin, destination: destination, mode: mode)
118
+ try ? self . _launch ( app: app,
119
+ origin: origin,
120
+ destination: destination,
121
+ mode: mode)
104
122
} )
105
123
}
106
124
. forEach { alert. addAction ( $0) }
@@ -110,12 +128,15 @@ public enum Karte {
110
128
return alert
111
129
}
112
130
113
- /// Present a `UIAlertController` with all supported apps the device has installed to offer an option for which app to start.
131
+ /// Present a `UIAlertController` with all supported apps the device has installed to offer an
132
+ /// option for which app to start.
114
133
///
115
134
/// - Parameters:
116
- /// - origin: an optional origin location, defaults to the user's location if left empty in most apps
135
+ /// - origin: an optional origin location, defaults to the user's location if left empty in
136
+ /// most apps
117
137
/// - destination: the location to route to
118
- /// - mode: an optional mode of transport to use, results in only those apps being shown that support this mode
138
+ /// - mode: an optional mode of transport to use, results in only those apps being shown that
139
+ /// support this mode
119
140
/// - viewcontroller: a `UIViewController` to present the `UIAlertController` on
120
141
/// - title: an optional title for the `UIAlertController`
121
142
/// - message: an optional message for the `UIAlertController`
@@ -130,7 +151,13 @@ public enum Karte {
130
151
cancel: String = " Cancel " ,
131
152
style: UIAlertControllerStyle = . actionSheet) {
132
153
133
- let alert = createPicker ( origin: origin, destination: destination, mode: mode, title: title, message: message, cancel: cancel, style: style)
154
+ let alert = createPicker ( origin: origin,
155
+ destination: destination,
156
+ mode: mode,
157
+ title: title,
158
+ message: message,
159
+ cancel: cancel,
160
+ style: style)
134
161
135
162
OperationQueue . main. addOperation {
136
163
viewcontroller. present ( alert, animated: true , completion: nil )
0 commit comments