0.3 C
New York
Sunday, February 23, 2025

ios – Share textual content and picture each on the similar time on social media apps like fb, instagram, whatsapp and on linkedin in swift


I am making an attempt to share title and picture as url on social media apps, however getting no success, I am utilizing the beneath code for a similar, however solely picture is sharing textual content isn’t sending.. please let me know if i am doing something improper right here.. Thanks prematurely!

import LinkPresentation

class ShareItem: NSObject, UIActivityItemSource {
non-public let url: URL
non-public let title: String

var activityTitle: String? {
    return title
}


init(url: URL, title: String? = "") {
    self.url = url
    self.title = title ?? ""
}

func activityViewControllerPlaceholderItem(_: UIActivityViewController) -> Any {
    title
}

func activityViewController(_: UIActivityViewController, itemForActivityType _: UIActivity.ActivityType?) -> Any? {
    url
}

func activityViewController(_: UIActivityViewController, subjectForActivityType _: UIActivity.ActivityType?) -> String {
    title
}

func activityViewController(_: UIActivityViewController, dataTypeIdentifierForActivityType _: UIActivity.ActivityType?) -> String {
    (strive? url.resourceValues(forKeys: [.typeIdentifierKey]).typeIdentifier) ?? ""
}

func activityViewController(_: UIActivityViewController, thumbnailImageForActivityType _: UIActivity.ActivityType?, suggestedSize _: CGSize) -> UIImage? {
    UIImage(contentsOfFile: url.absoluteString)
}

func activityViewControllerLinkMetadata(_: UIActivityViewController) -> LPLinkMetadata? {
    let metadata = LPLinkMetadata()
    
    // Set the title and different metadata
    metadata.title = title
    metadata.originalURL = url
    metadata.url = url
    metadata.iconProvider = NSItemProvider.init(contentsOf: url)
    // Make sure the picture supplier is legitimate
    if let picture = UIImage(contentsOfFile: url.path) {
        metadata.imageProvider = NSItemProvider(object: picture)
    }
    return metadata
}


static func saveImageForSharing(picture: UIImage, completion: @escaping (URL?) -> Void) {
    let tempDirectory = FileManager.default.temporaryDirectory
    let fileName = UUID().uuidString + ".png"
    let fileURL = tempDirectory.appendingPathComponent(fileName)
    if let imageData = picture.pngData() {
        do {
            strive imageData.write(to: fileURL)
            completion(fileURL)
        } catch {
            print("Error saving picture: (error)")
            completion(nil)
        }
    } else {
        print("Error changing picture to PNG information")
        completion(nil)
    }
}

}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles