Last active
March 24, 2016 20:53
-
-
Save SimplGy/8a1bd768d249ead3ca9d to your computer and use it in GitHub Desktop.
Opens a deep link into an app (Waitress). If that app isn't installed, open the app store for the app instead. Encode a back link into the app open url so the target app can navigate back to this app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// ios-mall-demo | |
// | |
// Created by Alexander Yngling on 26/10/15. | |
// Copyright © 2015 Waitress AB. All rights reserved. | |
// | |
import UIKit | |
class PlaceIdPickerVC: UIViewController { | |
@IBAction func onTapBtn(sender: UIButton) { | |
guard let placeId = sender.titleLabel?.text else { return } | |
openPlaceInWaitress(placeId) | |
} | |
func openPlaceInWaitress(placeId: String) { | |
let backLinkString = "mall://home?id=10&f=1" // replace with whatever back link is needed | |
let safeCharacterSet = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() | |
safeCharacterSet.removeCharactersInString("&=?:/") | |
let backLink = backLinkString.stringByAddingPercentEncodingWithAllowedCharacters(safeCharacterSet as! NSCharacterSet)! | |
let url = NSURL(string:"waitress://www.waitress.com/places/\(placeId)?back=\(backLink)")! | |
let app = UIApplication.sharedApplication() | |
// Open the target URL | |
if (app.canOpenURL(url)) { | |
print("Can open url: \(url)") | |
app.openURL(url); | |
// Open the app store (the target app isn't installed) | |
} else { | |
UIPasteboard(name: "Waitress", create: true)?.string = "\(url)" | |
print("Can't open url, added to pasteboard: \(url)") | |
let appStore = NSURL(string:"itms://itunes.apple.com/app/apple-store/id874437345")! // http://stackoverflow.com/questions/433907/how-to-link-to-apps-on-the-app-store#comment2426466_2337601 | |
print("Opening appstore: \(appStore)") | |
app.openURL(appStore) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment