Introduction
- With iOS 9, new iPhone models add a third dimension to the user interface.
- A user can now press your Home screen icon to immediately access functionality provided by your app.
- Within your app, a user can now press views to see previews of additional content and gain accelerated access to features.
- Apple’s own apps mostly use 3D Touch for getting a little preview of the next thing you want to do. So if you’re on the home screen, hard press on an icon to get a few shortcuts directly into a section of the app.
- If you want to your app more interactive to add 3D touch in your application
- Two major 3D touch features need to add your app.
- Peek and Pop
- Quick Actions
1. Implement Peek and Pop :
A. Storyboard -> Using Segue : (On cell Selection)
– create simple list screen as per your requirement. I already created below screen that contain tableview with simple cell
-make sure your segue type is cell selection
1. ListVC.swift
import UIKit class ListVC: UIViewController { @IBOutlet weak var tblList: UITableView! var arrCompany = [Company]() override func viewDidLoad() { super.viewDidLoad() arrCompany.append( Company(name:"Apple", address:"Cupertino, California, U.S.", url:"https://en.wikipedia.org/wiki/Apple_Inc", logo:#imageLiteral(resourceName: "apple")) ) arrCompany.append( Company(name:"Google", address:"Googleplex, Mountain View, California, U.S", url:"https://en.wikipedia.org/wiki/Google", logo:#imageLiteral(resourceName: "google")) ) arrCompany.append( Company(name:"Microsoft", address:"Albuquerque, New Mexico, United States", url:"https://en.wikipedia.org/wiki/Microsoft", logo:#imageLiteral(resourceName: "microsoft")) ) arrCompany.append( Company(name:"Samsung", address:"Seocho District, Seoul, South Korea", url:"https://en.wikipedia.org/wiki/Samsung", logo:#imageLiteral(resourceName: "samsung")) ) arrCompany.append( Company(name:"Amazon", address:"Seattle, Washington, U.S", url:"https://en.wikipedia.org/wiki/Amazon.com", logo:#imageLiteral(resourceName: "amazon")) ) arrCompany.append( Company(name:"Facebook", address:"Menlo Park, California, U.S.", url:"https://en.wikipedia.org/wiki/Facebook", logo:#imageLiteral(resourceName: "facebook")) ) arrCompany.append( Company(name:"Flipkart", address:"Bangalore, Karnataka, India", url:"https://en.wikipedia.org/wiki/Flipkart", logo:#imageLiteral(resourceName: "flipkart")) ) arrCompany.append( Company(name:"Yahoo", address:"Sunnyvale, California, U.S.", url:"https://en.wikipedia.org/wiki/Yahoo!", logo:#imageLiteral(resourceName: "yahoo")) ) } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let cell = sender as! UITableViewCell let indexPath = tblList.indexPath(for: cell)! if segue.identifier == "segueDetails" { if let detailsVC = segue.destination as? DetailVC { detailsVC.url = arrCompany[indexPath.row].url } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } extension ListVC : UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrCompany.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier:"ListCell") return cell! } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if let listCell = cell as? ListCell { listCell.lblName.text = arrCompany[indexPath.row].name listCell.lblAddress.text = arrCompany[indexPath.row].address listCell.imgLogo.image = arrCompany[indexPath.row].logo } } }
2. ListCell.swift
// Cell Class class ListCell : UITableViewCell { @IBOutlet weak var imgLogo: UIImageView! @IBOutlet weak var lblName: UILabel! @IBOutlet weak var lblAddress: UILabel! override func awakeFromNib() { super.awakeFromNib() } } // Cell Model Class class Company { var name:String var address:String var logo : UIImage var url:String init(name:String,address:String,url:String,logo:UIImage) { self.name = name self.address = address self.logo = logo self.url = url } }
3. DetailVC.swift
import UIKit class DetailVC: UIViewController { var url : String? @IBOutlet weak var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() let urlReq = URL(string: url!); let requestObj = URLRequest(url: urlReq!) webView.loadRequest(requestObj) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func addMenuItems(menu:String ...) -> [UIPreviewActionItem] { var arrPreview = [UIPreviewActionItem]() for m in menu { let likeAction = UIPreviewAction(title:m, style: .default) { (action, viewController) -> Void in print(action.title) } arrPreview.append(likeAction) } return arrPreview } // Add Action of preview override var previewActionItems: [UIPreviewActionItem] { return self.addMenuItems(menu: "Open","Bookmark") } }
2.Implement Quick Action:
– Create a UIApplicationShortcutItems array. This is where we define what the actions are, the title, subtitle, and shortcut keys, icon, dictionary. Note that you can only have a max of 4 quick actions off the icon in your app.
A. Dynamic Quick Action
type: A required string delivered to your app when the user invokes the corresponding quick action.
localizedTitle: A required string displayed to the user on the Home screen as the name of the quick action.
localizedSubtitle: An optional string that is displayed to the user on the Home screen, immediately below the corresponding title string. icon: An optional string specifying the type of an icon from the system-provided API. Full list of the icons https://developer.apple.com/reference/uikit/uiapplicationshortcuticontype. We can also set a custom icon. userInfo: An optional, app-defined dictionary.
1. AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { UIApplication.shared.shortcutItems = getAppShortcut() return true } extension AppDelegate { func getAppShortcut() -> [UIApplicationShortcutItem] { return [ UIApplicationShortcutItem(type:"id_create", localizedTitle:"Create", localizedSubtitle:"create new item", icon:UIApplicationShortcutIcon(type: .add), userInfo:nil) , UIApplicationShortcutItem(type:"id_like", localizedTitle:"Like", localizedSubtitle:"view your favorites", icon:UIApplicationShortcutIcon(type: .favorite), userInfo:nil) , UIApplicationShortcutItem(type:"id_search", localizedTitle:"Search", localizedSubtitle: nil, icon:UIApplicationShortcutIcon(type: .search), userInfo:nil) , UIApplicationShortcutItem(type:"id_search1", localizedTitle:"Custom Icon", localizedSubtitle: nil, icon:UIApplicationShortcutIcon(templateImageName:"apple_logo"), // Custom Icon userInfo:nil), ] } }
– When user tap on any quick action below method will call. You can navigate specific screen as per your requirement.
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { }
Quick Action Output:
A. Static Quick Action (Info.plist)
– home screen quick actions using the UIApplicationShortcutItems array in the app Info.plist file
– add this key into you info.plist file
<key>UIApplicationShortcutItems</key> <array> <dict> <key>UIApplicationShortcutItemIconType</key> <string>UIApplicationShortcutIconTypeSearch</string> <key>UIApplicationShortcutItemSubtitle</key> <string>shortcutSubtitle1</string> <key>UIApplicationShortcutItemTitle</key> <string>shortcutTitle1</string> <key>UIApplicationShortcutItemType</key> <string>$(PRODUCT_BUNDLE_IDENTIFIER).First</string> <key>UIApplicationShortcutItemUserInfo</key> <dict> <key>firstShorcutKey1</key> <string>firstShortcutKeyValue1</string> </dict> </dict> </array>