Skip to content

Commit

Permalink
REF: Use new deeplinking interface to open restaurant in external apps
Browse files Browse the repository at this point in the history
  • Loading branch information
josefdolezal committed Oct 28, 2017
1 parent 0b5108c commit 1931bd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
10 changes: 5 additions & 5 deletions LunchGuy/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>yelp4</string>
<string>comgooglemaps</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand Down Expand Up @@ -41,6 +36,11 @@
</dict>
</array>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>yelp4</string>
<string>comgooglemaps</string>
</array>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
Expand Down
40 changes: 16 additions & 24 deletions LunchGuy/ViewControllers/MenuTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,24 @@ class MenuTableViewController: UITableViewController {
func actionButtonHanlder() {
let controller = UIAlertController(title: "Choose an app where you want to open selected restaurant.", message: nil, preferredStyle: .actionSheet)

let urlEscapedRestaurant = restaurant.name.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""

let actions = [
UIAlertAction(title: "Apple Maps", style: .default, handler: { _ in
let url = URL(string: "http://maps.apple.com/?q=\(urlEscapedRestaurant)")!
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}),
UIAlertAction(title: "Google Maps", style: .default, handler: { _ in
let url = URL(string: "comgooglemaps://?q=\(urlEscapedRestaurant)")!
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}),
UIAlertAction(title: "Yelp", style: .default, handler: { _ in
let url = URL(string: "yelp4:///search?terms=\(urlEscapedRestaurant)")!

if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.openURL(url)
}
}),
UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let externalApps: [ExternalApp] = [
AppleMapsApp.search(place: restaurant.name),
GoogleMapsApp.search(place: restaurant.name),
YelpApp.search(place: restaurant.name)
]

actions.forEach { controller.addAction($0) }
// Add only apps which are actually installed
externalApps.flatMap { app -> UIAlertAction? in
guard UIApplication.shared.canOpenURL(app.urlScheme) else { return nil }

return UIAlertAction(title: app.displayName, style: .default) { _ in
UIApplication.shared.openURL(app.deepLink)
}
}
.forEach { controller.addAction($0) }

// Add cancel action
controller.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

present(controller, animated: true, completion: nil)
}
Expand Down

0 comments on commit 1931bd4

Please sign in to comment.