iOS自带的分享控件其实是一个很nice的。它可以不需要集成微信、QQ等第三方SDK就可以实现分享(需要在弹出视图里面点击更多打开开关)到微信好友、朋友圈等,功能很强大,也很方便。还有更多的分享不需要我们去设置。
两个步骤即可完成
1. UIActivity配置分享的内容和链接、操作(自定义)
2. UIActivityViewController弹出分享视图
一、具体的支持的分享类型
extension UIActivity.ActivityType {
@available(iOS 6.0, *)
public static let postToFacebook: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let postToTwitter: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let postToWeibo: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let message: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let mail: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let print: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let copyToPasteboard: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let assignToContact: UIActivity.ActivityType
@available(iOS 6.0, *)
public static let saveToCameraRoll: UIActivity.ActivityType
@available(iOS 7.0, *)
public static let addToReadingList: UIActivity.ActivityType
@available(iOS 7.0, *)
public static let postToFlickr: UIActivity.ActivityType
@available(iOS 7.0, *)
public static let postToVimeo: UIActivity.ActivityType
@available(iOS 7.0, *)
public static let postToTencentWeibo: UIActivity.ActivityType
@available(iOS 7.0, *)
public static let airDrop: UIActivity.ActivityType
@available(iOS 9.0, *)
public static let openInIBooks: UIActivity.ActivityType
@available(iOS 11.0, *)
public static let markupAsPDF: UIActivity.ActivityType
}
二、UIActivity配置分享的内容和链接等
1.如果不需要自定义的分享,直接设置UIActivity(这里不在细说,下面的用法里就有)
2.自定义的LGActivity
class LGActivity: UIActivity {
let LGActivityType = "LGActivityType"
var lgTitle: String?
var lgImage: UIImage?
var lgUrl: URL?
var lgShareContexts: [Any]?
//设置title 和 image会在弹出视图里面出现一个自定义的应用,title为应用名称 image为应用图标 shareContext是存放分享的内容的
convenience init(title: String, image: UIImage, url: URL?, shareContext:[Any]) {
self.init()
lgTitle = title
lgImage = image
if url != nil {
lgUrl = url
}
lgShareContexts = shareContext
}
override init() {
super.init()
}
// default is UIActivityCategoryAction.
override class var activityCategory: UIActivity.Category {
return .share
}
// default returns nil. subclass must override and must return non-nil value
override var activityImage: UIImage? {
return lgImage
}
// default returns nil. subclass must override and must return non-nil value
override var activityTitle: String? {
return lgTitle
}
var activityUrl: URL? {
return lgUrl
}
// default returns nil. subclass may override to return custom activity type that is reported to completion handler
override var activityType: UIActivity.ActivityType? {
return UIActivity.ActivityType(rawValue: LGActivityType)
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
if activityItems.count > 0 {
return true;
}
return false;
}
//准备分享所进行的方法,通常在这个方法里面,把item中的东西保存下来,items就是要传输的数据
override func prepare(withActivityItems activityItems: [Any]) {
print("activityItems --- \(activityItems)")
}
override func perform() {
///在这里写你自定义的应用 分享点击以后需要做的操作,比如点击需要打开链接、复制等等
self.activityDidFinish(true)
}
}
三、UIActivityViewController弹出视图
class LGSystemShare: NSObject {
var parentVc: UIViewController?
static let manager = LGSystemShare()
private override init() {
super.init()
}
///这里我设置了配置链接是可选的
func lgShrare(shreVC: UIViewController, orText: String, trText: String, shareUrl: URL?) {
parentVc = shreVC
var activityItems = [Any]()
if shareUrl == nil {
activityItems = [orText, trText]
} else {
activityItems = [orText, trText, shareUrl as Any]
}
let activity = LGActivity(title: lg_appName, image: UIImage(named: "iPhoneApp.png")!, url: shareUrl, shareContext: activityItems)
let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: [activity])
//表示去掉不想显示的按钮 即第一点里面所包含的type 放到这里
activityVC.excludedActivityTypes = []
activityVC.isModalInPopover = true
shreVC.present(activityVC, animated: true, completion: nil)
}
}
到此,基本的系统分享已完成,需要更复杂的分享,各位可以继续深入研究!!!