SFSafariViewController:如何提供自定义活动?

时间:2015-09-10 12:41:42

标签: ios swift ios9

Safari View Controller的WWDC会话提到应用程序可以通过委托func safariViewController(controller: SFSafariViewController, activityItemsForURL URL: NSURL, title: String?) -> [UIActivity]的方法SFSafariViewControllerDelegate提供自定义活动。我试图实现这个方法,但是在我提出SFSafariViewCntroller之后,没有被称为。我还实现了该委托的另一个可选方法func safariViewControllerDidFinish(_: SFSafariViewController),它被调用。我试图在我的方法中添加“@objc”关键字(某些其他协议所需),但它似乎没有改变任何东西。

我想知道可能出现什么问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

以下是您参考的示例代码。在您的主视图中:

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

您可以在操作表的操作行中看到两个新图标。您也可以将其更改为显示在共享行中,如代码中所述。

最后需要注意的是,微博在Safari View上共享存在陷阱,因为微信并不符合Safari的共享标准。您可以点击微信共享图标,微信即可分享。但是你只能在Safari View的页面中获取页面标题和网址,这与使用evaluateJavaScript获取所有内容的WKWebView不同。因此,您需要从其他地方获取共享图像和描述(以便与朋友分享)。