屏幕快照 2017-03-14 下午5.34.24.png
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let btn = UIButton(type: .custom)
btn.frame = CGRect(x: 50, y: 100, width: 80, height: 80)
btn.addTarget(self, action: #selector(btnClick(_ :)), for: .touchUpInside)
btn.setTitleColor(UIColor.blue, for: .normal)
btn.setTitle("按钮", for: .normal)
self.view.addSubview(btn)
}
func btnClick(_ btn:UIButton)
{
//准备分享内容,文本图片链接
let items = ["航歌", UIImage(named:"93-dot-red")!,
NSURL(fileURLWithPath:"http://hangge.com")] as [Any]
//新建自定义的分享对象数组
let acts = [FirstActivity(),SecondActivity()]
let activityVC:UIActivityViewController = UIActivityViewController(activityItems: items, applicationActivities: acts)
//要排除的分享按钮,不显示在分享框里
activityVC.excludedActivityTypes = [UIActivityType.mail,UIActivityType.copyToPasteboard,
UIActivityType.print,UIActivityType.assignToContact,UIActivityType.saveToCameraRoll]
self.present(activityVC, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import Foundation
import UIKit
class FirstActivity: UIActivity {
var text:String!
var url:NSURL!
var image:UIImage!
override var activityTitle: String?
{
return "第一"
}
override var activityImage: UIImage?
{
return UIImage(named: "1234")
}
//分享类型,在UIActivityViewController.completionHandler回调里可以用于判断,一般取当前类名
override var activityType: UIActivityType? {
return UIActivityType(rawValue: FirstActivity.self.description())
}
//按钮类型(分享按钮:在第一行,彩色,动作按钮:在第二行,黑白)
override class var activityCategory: UIActivityCategory{
return UIActivityCategory.action
}
//是否显示分享按钮,这里一般根据用户是否授权,或分享内容是否正确等来决定是否要隐藏分享按钮
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
for item in activityItems {
if item is UIImage {
return true
}
if item is NSURL {
return true
}
if item is String {
return true
}
}
return false
}
//解析分享数据时调用,可以进行一定的处理
override func prepare(withActivityItems activityItems: [Any]) {
print("prepareWithActivityItems")
for item in activityItems {
if item is UIImage {
image = item as! UIImage
}
if item is String {
text = item as! String
}
if item is NSURL {
url = item as! NSURL
}
}
}
//执行分享行为
//这里根据自己的应用做相应的处理
//例如你可以分享到另外的app例如微信分享,也可以保存数据到照片或其他地方,甚至分享到网络
override func perform() {
print("performActivity")
//具体的执行代码这边先省略
}
//分享时调用
override var activityViewController: UIViewController?
{
print("activityViewController")
return nil
}
//完成分享后调用
override func activityDidFinish(_ completed: Bool) {
print("activitydidfinish")
}
}
import Foundation
import UIKit
class SecondActivity: UIActivity {
var text:String!
var url:NSURL!
var image:UIImage!
override var activityTitle: String?
{
return "第二"
}
override var activityImage: UIImage?
{
return UIImage(named: "58(1)")
}
//分享类型,在UIActivityViewController.completionHandler回调里可以用于判断,一般取当前类名
override var activityType: UIActivityType? {
return UIActivityType(rawValue: SecondActivity.self.description())
}
//按钮类型(分享按钮:在第一行,彩色,动作按钮:在第二行,黑白)
override class var activityCategory: UIActivityCategory{
return UIActivityCategory.share
}
//是否显示分享按钮,这里一般根据用户是否授权,或分享内容是否正确等来决定是否要隐藏分享按钮
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
for item in activityItems {
if item is UIImage {
return true
}
if item is NSURL {
return true
}
if item is String {
return true
}
}
return false
}
//解析分享数据时调用,可以进行一定的处理
override func prepare(withActivityItems activityItems: [Any]) {
print("prepareWithActivityItems")
for item in activityItems {
if item is UIImage {
image = item as! UIImage
}
if item is String {
text = item as! String
}
if item is NSURL {
url = item as! NSURL
}
}
}
//执行分享行为
//这里根据自己的应用做相应的处理
//例如你可以分享到另外的app例如微信分享,也可以保存数据到照片或其他地方,甚至分享到网络
override func perform() {
print("performActivity")
//具体的执行代码这边先省略
}
//分享时调用
override var activityViewController: UIViewController?
{
print("activityViewController")
return nil
}
//完成分享后调用
override func activityDidFinish(_ completed: Bool) {
print("activitydidfinish")
}
}
网友评论