Safari的WWDC会话提到,应用程序可以通过委托func safariViewController(controller: SFSafariViewController, activityItemsForURL URL: NSURL, title: String?) -> [UIActivity]的SFSafariViewControllerDelegate方法提供自定义活动。我尝试过实现这个方法,但是在我呈现SFSafariViewCntroller之后不会调用它。我还实现了该委托的另一个可选方法,func safariViewControllerDidFinish(_: SFSafariViewController),它确实会被调用。我试图将"@objc“关键字添加到我的方法中(其他一些协议需要),但它似乎没有改变任何东西。
我在想什么会出问题。
谢谢!
发布于 2015-09-24 09:34:05
下面是供您参考的示例代码。你的主要观点是:
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上的WeChat共享存在缺陷,因为WeChat不符合Safari的共享标准。您可以单击WeChat共享图标,WeChat将能够共享。但是,您只能在Safari的页面中获得页面标题和url,与WKWebView不同的是,在evaluateJavaScript中可以获得所有内容。因此,您需要从其他地方获得共享图像和描述(用于与朋友分享)。
https://stackoverflow.com/questions/32502217
复制相似问题