您的位置:首页 > 移动开发 > Swift

iOS3D-Touch开发之Home Screen Quick Actions(Swift)

2016-03-04 17:50 447 查看
func creatShortcutItem() {
let item1 = UIApplicationShortcutItem(type: "Shortcut1", localizedTitle: "搜索", localizedSubtitle: "此处可以搜索", icon:UIApplicationShortcutIcon(type: UIApplicationShortcutIconType.Location), userInfo: nil)
let item2 = UIApplicationShortcutItem(type: "Shortcut2", localizedTitle: "主页", localizedSubtitle: "此处是主页", icon:UIApplicationShortcutIcon(templateImageName: "iconfont-hometouch"), userInfo: nil)
UIApplication.sharedApplication().shortcutItems = [item1,item2]
}


值得注意的是,应用在后台开启,会调用以下方法

func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
switch shortcutItem.type {
case "Shortcut1" :
let tab = RootTabViewController()
self.window?.rootViewController = tab
self.window?.makeKeyAndVisible()
let vcc = HPViewController()
vcc.hidesBottomBarWhenPushed = true
(tab.selectedViewController as! UINavigationController).pushViewController(vcc, animated: true)
case "Shortcut2" :
let tab = RootTabViewController()
self.window?.rootViewController = tab
self.window?.makeKeyAndVisible()
tab.selectedIndex = 1
default:
print("nothing")
}
}


应用在后台未开启,会调用以下方法

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if #available(iOS 9.0, *) {
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
switch shortcutItem.type {
case "Shortcut1":
if isLogined() {
}else {
self.window?.rootViewController = LoginViewController()
self.window?.makeKeyAndVisible()
SVProgressHUD.showErrorWithStatus("请您先登录")
}
break
case "Shortcut2" :
if isLogined() {
let time: NSTimeInterval = 0.5
let delay = dispatch_time(DISPATCH_TIME_NOW,
Int64(time * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
let tab = RootTabViewController()
self.window?.rootViewController = tab
self.window?.makeKeyAndVisible()
let vcc = EditViewController()
vcc.hidesBottomBarWhenPushed = true
(tab.selectedViewController as! UINavigationController).pushViewController(vcc, animated: true)
}
}else {
self.window?.rootViewController = LoginViewController()
self.window?.makeKeyAndVisible()
SVProgressHUD.showErrorWithStatus("请您先登录")
}
break
default:
break
}

return false
}
} else {
// Fallback on earlier versions
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  3D-Touch Swift ios