I am trying to open google map, but the following code is not working in ios 13.
let addressForMap = address.replacingOccurrences(of: " ", with: "+", options: .literal, range: nil)
if let url = URL(string: "comgooglemaps://?q=\(addressForMap)¢er=\(lat),\(long)") {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open: \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open: \(success)")
}
}
In plist, I have added it:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
Your url might be return nil, so they not open in browser or google map
Please try url
if let url = URL(string: "comgooglemaps://?saddr=&daddr=\(latitude),\(longitude)&directionsmode=driving") {
UIApplication.shared.open(url, options: [:])
}
Your try this url also
"https://maps.google.com/?q=#\(lat),\(lon)"
Related
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'm trying to ask for phone call programmatically but I'm not able to construct URL from my nine-digit phone number. When I try it with for example 999999999 phone number, it works, it asks for call
#IBAction func callButtonPressed(_ sender: UIButton) {
askForCall(to: "999999999")
}
func askForCall(to number: String) {
guard let url = URL(string: "tel:\(number)"), UIApplication.shared.canOpenURL(url) else { return }
UIApplication.shared.open(url)
}
but when I use real phone number 736XXXXXX it shows nothing.
Note: when I try it without canOpenUrl it doesn’t work so I guess problem is with constructing URL from my real number
Any ideas?
You should type "tel://" + number and not tel:\(number)
EDIT 2
Try something like this
func call(phoneNumber: String) {
if let url = URL(string: phoneNumber) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(phoneNumber): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(phoneNumber): \(success)")
}
}
}
let number = "736XXXXXX"
let phoneNumber = "tel://\(number)"
call(phoneNumber: phoneNumber)
Try with that number to see if it's a bigger problem than the simple code :)
You need to add the scheme 'tel' into your info.plist
<key>LSApplicationQueriesSchemes</key>
<string>tel</string>
Then normal use:
guard let url = URL(string: "tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(url) else {return}
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
Goodluck
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.
I am trying to place a phone call with this code...
let phone = detail.value(forKey: "Phone") as? String
guard let number = URL(string: "telprompt://" (phone)) else { return }
UIApplication.shared.open(number, options: [:], completionHandler: nil)
I attempted to vary my code basically off this answer swift how to make phone call iOS 10?, but I am having difficulty creating a working/error-free function.
Originally my code went like
guard let number = URL(string: "telprompt://"\(phone))...
however, Xcode directed a space between the end quote and open paranthensis for the phone variable while simultaneously deleting "\". Unfortunately, now I am left with the error in the title. A tweak in my code would be appreciated :D
Update 1:
I have updated my code to
#IBAction func call(_ sender: Any)
{
let phone = detail.value(forKey: "Phone") as? NSURL
func makeCallToNumber(number: String){
if let url = URL(string: "TEL://\(phone)"){
UIApplication.shared.open(url , options: [:], completionHandler: nil)
}
else{
print("Error")
}
}
}
yet the code is still not bringing up the dialer.
update 2:
I have switched my code to
let phone = detail.value(forKey: "Phone") as? String
if let url = URL(string: "telprompt:\(String(describing: phone))") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
and while I have no errors, no call is being made and this appears in my console. . Unsure what it means.
Working example:
func callNumber(phoneNumber: String) {
if let phoneCallURL = NSURL(string: "tel://\(phoneNumber.phoneToString())") {
if UIApplication.shared.canOpenURL(phoneCallURL as URL) {
UIApplication.shared.openURL(phoneCallURL as URL)
}
}
}
//removes "(", ")", "-", " " etc. and adds "+" for region code format
extension String {
func phoneToString() -> String {
var value = "+"
for character in self.characters {
if Int(String(character)) != nil {
value = value + String(character)
}
}
return value
}
}
phoneToString would format "+000 (000) 000" into "+000000000"
Try this easy code:
func makeCallToNumber(number: String){
if let url = URL(string: "TEL://\(number)"){
UIApplication.shared.open(url , options: [:], completionHandler: nil)
}
else{
print("Error")
}
}
Using guard and telprompt
func makeCallToNumber2(number: String){
guard let url = URL(string: "telprompt://\(number)") else {
print("Error")
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Regards