美文网首页
iOS 系统分享-不同内容

iOS 系统分享-不同内容

作者: 吃货_X | 来源:发表于2021-11-08 10:46 被阅读0次
    ///系统分享类型
    public enum SystemShareType {
        ///分享图片
        case shareImage(UIImage)
        ///分享链接
        case shareUrl(URL)
        ///分享带标题链接
        case shareTitleUrl(String,URL)
        ///分享带标题图片链接
        case shareTitleImageUrl(String,UIImage,URL)
        ///分享多张图片最多九张
        case shareMoreImages([UIImage])
    }
    
    ///系统分享
    public func XlbSystemShare(_ shareType:SystemShareType){
        var arrayItems:[Any] = []
        var excludedActivityTypes:[UIActivity.ActivityType] = []
        switch shareType {
        case let .shareImage(image):
            arrayItems.append(image.jpegData(compressionQuality: 1.0) as Any)
            excludedActivityTypes = [.message,.mail,.copyToPasteboard,.addToReadingList]
            break
        case let .shareUrl(url):
            arrayItems.append(url)
            excludedActivityTypes = [.message,.mail,.addToReadingList,.saveToCameraRoll]
            break
        case let .shareTitleUrl(title, url):
            arrayItems.append(title)
            arrayItems.append(url)
            excludedActivityTypes = [.message,.mail,.addToReadingList,.saveToCameraRoll]
            break
        case let .shareTitleImageUrl(title, image, url):
            excludedActivityTypes = [.message,.mail,.addToReadingList,.saveToCameraRoll]
            arrayItems.append(title)
            arrayItems.append(image.jpegData(compressionQuality: 1.0) as Any)
            arrayItems.append(url)
            break
        case let .shareMoreImages(images):
            excludedActivityTypes = [.message,.mail,.copyToPasteboard,.addToReadingList,.saveToCameraRoll]
            for (index,image) in images.enumerated() {
                if images == nil {
                    continue
                }
                if index < 9 {
                    arrayItems.append(image.jpegData(compressionQuality: 1.0) as Any)
                } else {
                    break
                }
            }
            break
        }
        excludedActivityTypes.append(.assignToContact)
        let activityController = UIActivityViewController(activityItems: arrayItems, applicationActivities: nil)
        activityController.excludedActivityTypes = excludedActivityTypes
        if let window = UIApplication.shared.keyWindow, let rootViewController = window.rootViewController {
            activityController.popoverPresentationController?.sourceView = window
            activityController.popoverPresentationController?.sourceRect = CGRect(origin: CGPoint(x: window.bounds.width / 2.0, y: window.bounds.size.height - 1.0), size: CGSize(width: 1.0, height: 1.0))
            activityController.modalPresentationStyle = .fullScreen
            rootViewController.present(activityController, animated: true, completion: nil)
        }
        
    }
    

    相关文章

      网友评论

          本文标题:iOS 系统分享-不同内容

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