系统分享UIActivity

iOS自带的分享控件可以完成绝大部分的分享需求。
它可以不需要集成微信、QQ等第三方SDK就可以实现分享(需要在弹出视图里面点击更多打开开关)到微信好友、朋友圈等,功能很强大,也很方便。

一、系统分享两个步骤

  1. UIActivity配置分享的内容和链接、操作(自定义)
  2. UIActivityViewController弹出分享视图

1. UIActivity配置分享的内容和链接等

自定义的GXActivity

class GXActivity: UIActivity {
    private let kActivityType =  "kActivityType"
    
    private var text: String?
    private var img: UIImage?
    private var url: URL?
    
    private var shareContexts: [Any]?
    // 设置title  和 image会在弹出视图里面出现一个自定义的应用,
    // title为应用名称  image为应用图标  shareContext是存放分享的内容的
    convenience init(title: String?, image: UIImage?, url: URL?, shareContext: [Any]) {
        self.init()
        self.text = title
        self.img = image
        if url != nil {
            self.url = url
        }
        self.shareContexts = 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 img
    }
    
    // default returns nil. subclass must override and must return non-nil value
    override var activityTitle: String? {
        return text
    }
    
    var activityUrl: URL? {
        return url
    }
    
    // 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: kActivityType)
    }
    
    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 GXSystemShare: NSObject {
    public var parentVC: UIViewController?
    
    public static let manager = GXSystemShare()
    private override init() {
        super.init()
    }
    /// 这里我设置了配置链接是可选的
    func shrare(shareVC: UIViewController, orText: String, trText: String, shareUrl: URL? = nil) {
        parentVC = shareVC
        var activityItems = [Any]()
        if shareUrl == nil {
            activityItems = [orText, trText]
        } else {
            activityItems = [orText, trText, shareUrl as Any]
        }
        let activity = GXActivity(title: "Demo AP", image: UIImage(named: "iPhoneApp.png"), url: shareUrl, shareContext: activityItems)
        let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: [activity])
        // 表示去掉不想显示的按钮  即第一点里面所包含的type 放到这里
        activityVC.excludedActivityTypes = []
        
//        activityVC.isModalInPopover = true
        shareVC.present(activityVC, animated: true, completion: nil)
    }
}

你可能感兴趣的:(iOS开发,cocoa)