gpt4 book ai didi

ios - SFSafariViewController : how to provide custom activities?

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:21 24 4
gpt4 key购买 nike

Safari View Controller 的 WWDC session 提到应用程序可以通过 func safariViewController(controller: SFSafariViewController, activityItemsForURL URL: NSURL, title: String?) -> [UIActivity] 方法提供自定义事件委托(delegate) SFSafariViewControllerDelegate。我已尝试实现此方法,但在我显示 SFSafariViewCntroller 后它未被调用。我还实现了该委托(delegate)的另一个可选方法,func safariViewControllerDidFinish(_: SFSafariViewController),它确实被调用了。我尝试将“@objc”关键字添加到我的方法中(一些其他协议(protocol)需要),但它似乎没有改变任何东西。

我想知道会出什么问题。

谢谢!

最佳答案

这是供您引用的示例代码。在您的主视图中:

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 的页面中获取页面标题和 url,这与 WKWebView 不同,您可以使用 evaluateJavaScript 获取所有内容。所以你需要从其他地方获取分享图片和描述(用于分享给 friend )。

关于ios - SFSafariViewController : how to provide custom activities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32502217/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com