Swift:实现快捷方式项目(3D 触摸)共享项目错误“AppDelegate 类型的值没有当前成员”
Posted
技术标签:
【中文标题】Swift:实现快捷方式项目(3D 触摸)共享项目错误“AppDelegate 类型的值没有当前成员”【英文标题】:Swift: Implementing shortcutItem(3D touch) share item error "Value of type AppDelegate has no member of present" 【发布时间】:2017-08-02 20:15:25 【问题描述】:我正在尝试实现 share shortcutItem(我的实现):
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void)
if shortcutItem.type == "share"
self.shareItem()
func shareItem()
let visitedlink = "http://google.com"
let myWebsite = NSURL(string: visitedlink)
let img: UIImage = UIImage(named:"Logo")!
guard let url = myWebsite else
print("nothing found")
return
let shareItems:Array = [img,url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo, UIActivityType.message, UIActivityType.mail]
self.present(activityViewController, animated: true, completion: nil)
但是在这一行:
self.present(activityViewController, animated: true, completion: nil)
我收到此错误:
Value of type AppDelegate has no member of present
你们中有人知道我为什么会收到这个错误吗?或解决这个问题?
非常感谢您的帮助。
【问题讨论】:
错误信息很清楚。AppDelegate
类不是 UIViewController
的子类。它没有present
方法。
@vadian :如何在任何视图控制器上实现?
您需要获取对当前顶视图控制器的引用。方式取决于根视图控制器的实现。
【参考方案1】:
试试这个,
public extension UIViewController
func shareItem()
let visitedlink = "http://google.com"
let myWebsite = NSURL(string: visitedlink)
let img: UIImage = UIImage(named:"Logo")!
guard let url = myWebsite else
print("nothing found")
return
let shareItems:Array = [img,url]
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo, UIActivityType.message, UIActivityType.mail]
self.present(activityViewController, animated: true, completion: nil)
【讨论】:
以上是关于Swift:实现快捷方式项目(3D 触摸)共享项目错误“AppDelegate 类型的值没有当前成员”的主要内容,如果未能解决你的问题,请参考以下文章
IOS 实现3D Touch在tableView的简单应用(swift版)