【问题标题】:Swift: Implementing shortcutItem(3D touch) share item error "Value of type AppDelegate has no member of present"Swift:实现快捷方式项目(3D 触摸)共享项目错误“AppDelegate 类型的值没有当前成员”
【发布时间】: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 :如何在任何视图控制器上实现?
  • 您需要获取对当前顶视图控制器的引用。方式取决于根视图控制器的实现。

标签: ios iphone swift3 xcode8 uiapplicationshortcutitem


【解决方案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)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 2019-02-14
    相关资源
    最近更新 更多