8.7 C
Canberra
Saturday, July 26, 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 attempting to share title and picture as url on social media apps, however getting no success, I am utilizing the under code for a similar, however solely picture is sharing textual content shouldn’t be sending.. please let me know if i am doing something incorrect 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 knowledge")
        completion(nil)
    }
}

}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles