Swift: Implementing shortcutItem(3D touch) share item error "Value of type AppDelegate has no member of present"

150 views Asked by At

I'm trying to implement share shortcutItem (my implementation):

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)
}

But on this line:

self.present(activityViewController, animated: true, completion: nil)

I'm getting this error:

Value of type AppDelegate has no member of present

enter image description here

Any of you knows why I'm getting this error? or work around this?

I'll really appreciate your help.

1

There are 1 answers

0
Kiester On

Try this,

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)
    }
}