简体   繁体   中英

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

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

在此处输入图片说明

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

I'll really appreciate your help.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM