美文网首页
IOS UIActivityViewController分享内容

IOS UIActivityViewController分享内容

作者: zhangml0522 | 来源:发表于2018-11-13 10:59 被阅读0次

    基本分享

            //设定分享内容
            let textShare = "text"
            let imageShare = UIImage(named: "aaa")
            let urlShare = URL(string: "http://www.baidu.com")
            let activityItems = [textShare,imageShare as Any,urlShare as Any]
            //弹出分享框
            let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities:nil)
            present(activityViewController, animated: true, completion: nil)
            //分享结束后的回调
            activityViewController.completionWithItemsHandler = {(_ activityType: UIActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ activityError: Error?) -> Void in
                print(completed ? "成功" : "失败")
            }
            /**
             设置排除的服务
             @available(iOS 6.0, *)
             public static let postToFacebook: UIActivityType
             
             @available(iOS 6.0, *)
             public static let postToTwitter: UIActivityType
             
             @available(iOS 6.0, *)
             public static let postToWeibo: UIActivityType // SinaWeibo
             
             @available(iOS 6.0, *)
             public static let message: UIActivityType
             
             @available(iOS 6.0, *)
             public static let mail: UIActivityType
             
             @available(iOS 6.0, *)
             public static let print: UIActivityType
             
             @available(iOS 6.0, *)
             public static let copyToPasteboard: UIActivityType
             
             @available(iOS 6.0, *)
             public static let assignToContact: UIActivityType
             
             @available(iOS 6.0, *)
             public static let saveToCameraRoll: UIActivityType
             
             @available(iOS 7.0, *)
             public static let addToReadingList: UIActivityType
             
             @available(iOS 7.0, *)
             public static let postToFlickr: UIActivityType
             
             @available(iOS 7.0, *)
             public static let postToVimeo: UIActivityType
             
             @available(iOS 7.0, *)
             public static let postToTencentWeibo: UIActivityType
             
             @available(iOS 7.0, *)
             public static let airDrop: UIActivityType
             
             @available(iOS 9.0, *)
             public static let openInIBooks: UIActivityType
             
             @available(iOS 11.0, *)
             public static let markupAsPDF: UIActivityType
            **/
            activityViewController.excludedActivityTypes = [.postToWeibo,.postToTwitter]
    

    UIActivityViewController分享到不同服务都有一定的局限性,比如说facebook无法分享文本且图片和url不能同时分享

    自定义分享

    新建一个类继承UIActivity并重写以下方法

    import UIKit
    
    class CustomActicity: UIActivity {
        
        //分享的标题
        override var activityTitle: String?{
            return "标题"
        }
        
        //分享的LOGO
        override var activityImage: UIImage?{
            return UIImage.init(named: "aaa")
        }
    
        //提供的服务类型的标识符
        override var activityType: UIActivityType?{
            return UIActivityType.init(CustomActicity.description())
        }
        
        //分享类型 .action .share
        override class var activityCategory: UIActivityCategory {
            return .share
        }
        
        ///操作
        override func prepare(withActivityItems activityItems: [Any]) {
            print(activityItems)
        }
        ///是否允许进行分享
        override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
            return true
        }
        
    }
    

    使用:

     let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: [CustomActicity()])
    
    share.png
    action.png

    相关文章

      网友评论

          本文标题:IOS UIActivityViewController分享内容

          本文链接:https://www.haomeiwen.com/subject/cgukfqtx.html