SFSafariViewController: how to provide custom actions?

A WWDC Safari View Controller session states that applications can provide custom actions using the func safariViewController(controller: SFSafariViewController, activityItemsForURL URL: NSURL, title: String?) -> [UIActivity]delegate method SFSafariViewControllerDelegate. I tried to implement this method, but it is not called after submission SFSafariViewCntroller. I also implemented another optional method of this delegate func safariViewControllerDidFinish(_: SFSafariViewController), which calls the call. I tried to add the keyword "@objc" to my method (required by some other protocols), but it doesn't seem to change anything.

I wonder what could go wrong.

Thanks!

+4
source share
1 answer

. :

func safariViewController(controler: SFSafariViewController, activityItemsForURL: NSURL, title: String?) -> [UIActivity] {
    //global variable for the url to be shared
    webPageUrl = activityItemsForURL.absoluteString
    //global variable for the title to be shared
    webPageTitle = title!
    let wcActivity = WeChatActivity()
    let wcMoment = WeChatMoment()
    return [wcActivity, wcMoment]
}

1

import UIKit

class WeChatActivity : UIActivity{

    override init() {
        self.text = ""

    }

    var text:String?

    override func activityType()-> String {
        return "WeChat"
    }

    override func activityImage()-> UIImage?
    {
        return UIImage(named: "WeChat")!
    }

    override func activityTitle() -> String
    {
        return "微信好友"
    }


    override class func activityCategory() -> UIActivityCategory{
        return UIActivityCategory.Action
        //you can change to .Share and it'll appear in the share line
    }

    func getURLFromMessage(message:String)-> NSURL
    {
        var url = "whatsapp://"

        if (message != "")
        {
            url = "\(url)send?text=\(message)"
        }

        return NSURL(string: url)!
    }


    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
        return true;
    }

    override func performActivity() {
        shareToWeChat("ftcweixin://?url=\(webPageUrl)&title=\(webPageTitle)&description=\(webPageDescription)&img=\(webPageImageIcon)&to=chat")
    }

}

2:

import UIKit

class WeChatMoment : UIActivity{

    override init() {
        self.text = ""
    }

    var text:String?


    override func activityType()-> String {
        return "WeChatMoment"
    }

    override func activityImage()-> UIImage?
    {
        return UIImage(named: "Moment")!
    }

    override func activityTitle() -> String
    {
        return "微信朋友圈"
    }


    override class func activityCategory() -> UIActivityCategory{
        return UIActivityCategory.Action
    }

    func getURLFromMessage(message:String)-> NSURL
    {
        var url = "whatsapp://"

        if (message != "")
        {
            url = "\(url)send?text=\(message)"
        }

        return NSURL(string: url)!
    }


    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
        return true;
    }

    override func performActivity() {
        shareToWeChat("ftcweixin://?url=\(webPageUrl)&title=\(webPageTitle)&description=\(webPageDescription)&img=\(webPageImageIcon)&to=moment")
    }
}

. , , .

, WeChat Safari View , WeChat Safari. WeChat Share, WeChat . URL- Safari, WKWebView, , JavaScript. , ( ) .

0

Source: https://habr.com/ru/post/1606781/


All Articles