// YetiPoll for iOS. Drop this file into your app. // // YetiPoll.present(from: self, appKey: "yp_live_pk_...", userId: currentUser.id) // // Pass `signature` when your app requires signed wall links: compute it on // your server (HMAC-SHA256 of the user id with your secret key). import UIKit import WebKit public enum YetiPoll { public static func wallURL(appKey: String, userId: String, signature: String? = nil) -> URL { var components = URLComponents(string: "https://yetipoll.com/wall")! var items = [URLQueryItem(name: "app", value: appKey), URLQueryItem(name: "user", value: userId)] if let signature = signature, !signature.isEmpty { items.append(URLQueryItem(name: "sig", value: signature)) } components.queryItems = items return components.url! } public static func present(from presenter: UIViewController, appKey: String, userId: String, signature: String? = nil) { let wall = YetiPollViewController(url: wallURL(appKey: appKey, userId: userId, signature: signature)) presenter.present(UINavigationController(rootViewController: wall), animated: true) } } public final class YetiPollViewController: UIViewController, WKNavigationDelegate, WKUIDelegate { private let url: URL private let webView = WKWebView(frame: .zero) public init(url: URL) { self.url = url super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") } public override func viewDidLoad() { super.viewDidLoad() title = "Surveys" navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(close)) webView.navigationDelegate = self webView.uiDelegate = self webView.frame = view.bounds webView.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.addSubview(webView) webView.load(URLRequest(url: url)) NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: UIApplication.willEnterForegroundNotification, object: nil) } @objc private func close() { dismiss(animated: true) } // Fresh surveys when the user comes back from one. @objc private func refresh() { webView.reload() } // Surveys open in a new window: hand them to Safari, keep the wall here. public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { if let target = navigationAction.request.url { UIApplication.shared.open(target) } return nil } }