I have latitude and longitude information for source and destination, I want to send this information to yandex maps and yandex maps should show directions. How can I do this in Swift 3?
You should use Yandex's URL-Scheme (yandexmaps://build_route_on_map/?params)
Example in swift 3:
let url = URL(string: "yandexmaps://build_route_on_map/?lat_from=XXXXX&lon_from=XXXXX&lat_to=XXXXX&lon_to=XXXXX")!
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
Remember to check if the app can be opened.
if UIApplication.shared.canOpenURL(url) {
// Open the URL here
}
Related
It's quite easy to open a URL in SwiftUI, e.g. as I've mentioned in this answer, however, I'm interested in opening the following strings in the web browser (e.g. Safari) with the default search engine:
samplestring
8883notaurl
Is there an option to open the query in a default search engine, instead of just composing a URL like this:
https://www.google.com/search?q=query
And then opening it as a regular URL?
You can use x-web-search:// scheme with x-web-search://?[SearchValue].
Sample code:
let urlStr = "x-web-search://?[SearchValue]"
if let url = URL(string: urlStr) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:]) { didEnd in
print("Did End: \(didEnd)")
}
} else {
print("Can't open URL")
}
} else {
print("Isn't URL")
}
I am new in swift and I am facing problem to show address in google maps. I am only able to show latitude and longitude.
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
UIApplication.shared.openURL(NSURL(string: "comgooglemaps://?saddr=&daddr=\(Float(self.lat)!),\(Float(self.lon)!)&directionsmode=driving")! as URL)
}
This code showing address as shown in the picture.
You can use the query parameter q with comgooglemaps scheme like this
let address = "" //Replace with address to open
//Replace line breaks and white spaces in the address with queryable values
let queryableAddress = address.replacingOccurrences(of: "\n", with: " ").replacingOccurrences(of: " ", with: "%20")
if let url = URL(string: "comgooglemaps://?q=\(queryableAddress)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Following is the code
let firstAddress = "your, local address, here"
let testURL: NSURL = NSURL(string: "comgooglemaps-x-callback://")!
if UIApplication.shared.canOpenURL(testURL as URL) {
if let address = firstAddress.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
let directionsRequest: String = "comgooglemaps-x-callback://" + "?daddr=\(address)" + "&x-success=sourceapp://?resume=true&directionsmode=driving"
let directionsURL: NSURL = NSURL(string: directionsRequest)!
let optionsKeyDictionary = [UIApplication.OpenExternalURLOptionsKey(rawValue: "universalLinksOnly"): NSNumber(value: true)]
UIApplication.shared.open(directionsURL as URL, options: optionsKeyDictionary, completionHandler: nil)
}
}
else {
print("Can't use comgooglemaps-x-callback:// on this device.")
}
Also you need to add following in your Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps-x-callback</string>
</array>
It will open google map if installed in your device and added url scheme in your project otherwise open default apple map with provided latitude and longitude
func openMapWithLatLng(dLat: Double, dLng: Double) {
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string:"comgooglemaps://?daddr=\(dLat),\(dLng)&directionsmode=driving&zoom=14&views=traffic")!, options: [:]
, completionHandler: nil)
} else {
UIApplication.shared.openURL(URL(string:"comgooglemaps://?daddr=\(dLat),\(dLng)&directionsmode=driving&zoom=14&views=traffic")!)
}
}else {
let url = "http://maps.apple.com/maps?daddr=\(dLat),\(dLng)"
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: url)!, options: [:]
, completionHandler: nil)
} else {
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: url)!, options: [:]
, completionHandler: nil)
} else {
// Fallback on earlier versions
}
}
}
}
Add below scheme in your Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
I would like to open my app's settings page inside the Settings app with Swift 4 in iOS 11. Just like the picture shows:
The following codes doesn't work, it will only open the Settings app:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
The app shows in the above picture is able to do this. So I wonder if there is any way to custom the URL Scheme to make it happen?
Oops, it seems it works in iOS 11.4.1:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Just an update because UIApplicationOpenSettingsURLString changed.
guard let url = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:])
}
You can open your app settings screen using it's bundle id, for example for CAMERA permission, you can use
if let bundleId = /* your app's bundle identifier */
let url = URL(string: "\(UIApplication.openSettingsURLString)&path=CAMERA/\(bundleId)"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Reference: https://stackoverflow.com/a/61383270/4439983
It seems that Apple has moved a lot of the app configurations to the App path with iOS 11, how to open the app path programmatically in Settings? I tried "App-Prefs:root=\(Bundle.main.bundleIdentifier!)" but this doesn't seem to work.
Please note that my question is specific to: How to open the app path in settings: NOT how to open the settings
Here is the code you're looking for, I guess:
if let url = URL(string: UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
And in addition, the updated version for swift 5 :
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Swift 4.2, iOS 12
Opening just the settings is possible with the function below:
extension UIApplication {
...
#discardableResult
static func openAppSettings() -> Bool {
guard
let settingsURL = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(settingsURL)
else {
return false
}
UIApplication.shared.open(settingsURL)
return true
}
}
Usage: UIApplication.openAppSettings()
But be careful to NOT use "non-public URL scheme", such as: prefs:root= or App-Prefs:root, because otherwise your app will be rejected. This happened to me recently since I was trying to have a deeplink to the wifi section in the settings.
And if you want to make it work for both, older and newer iOS-versions, then do:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
openURL has been deprecated since iOS 10, so I would advise you to use:
if let url = URL(string:UIApplicationOpenSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: { success in
log.debug("Open app settings success: \(success)")
})
}
}
UIApplicationOpenSettingsURLString has been renamed to UIApplication.openSettingsURLString.
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
SWift 5
In some case we can not open App's setting after trying all the above. To solve this problem 100% just make sure these two step are followed
Step 1.
Right click on the project file -> Add New File -> Add Settings.Bundle in project and edit according to your requirements.
Step 2. Now add some code in your buttons action.
if let url = URL(string: UIApplication.openSettingsURLString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
Note: Using "prefs:Root" is forbidden by apple and your app will be rejected. So, avoid using this api.
let proURL = "fb://profile/\(userId)"
if let url = URL(string: proURL), UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: {
(success) in
})
} else {
UIApplication.shared.openURL(url)
}
}
Here I take user profile Id and based on that I will open profile in app, but it gives me error :
the page you requested was not found