Wednesday, November 11, 2026
HomeiOS Developmentios - Sharing an ICS file together with some textual content just...

ios – Sharing an ICS file together with some textual content just isn’t working

[ad_1]

I am creating an ICS file and I am attempting to share it together with some textual content utilizing UIActivityViewController. Whereas sharing solely ics file getting shared however not the textual content. Right here is code and screenshots, please let me know, is there something mistaken.

Word: Whereas sharing by way of mail, it’s behaving as anticipated. Situation is with WhatsApp, messages and so on.

In case you examine messages app screenshot, textual content is showing there.

    import UIKit

struct EventInformation {
    let title: String
    let description: String
    let registration: String
    let hyperlink: String
    let timeZone: TimeZone
    let startDateTime: Date
    let endDateTime: Date
    let location: String

}

protocol ICSFileGenerationDelegate {
    init(viewController: UIViewController)
    func shareICSFile(from eventInformation: EventInformation)
}

class ICSFileGenerator: NSObject, ICSFileGenerationDelegate {

    non-public weak var viewController: UIViewController?
    
    required init(viewController: UIViewController) {
        self.viewController = viewController
    }

    non-public func generateICSFile(occasion: EventInformation, completion: ((URL?) -> Void)) {
            
        let iCalText: NSMutableString = ""
        iCalText.append("BEGIN:VCALENDAR")
        iCalText.append("rnBEGIN:VEVENT")
    
        iCalText.append("rnSUMMARY:(occasion.title.replacingOccurrences(of: ",", with: ","))")
        iCalText.append("rnDESCRIPTION:(occasion.description)nn(occasion.registration)")
    
        if let eventUrl = URL(string: occasion.hyperlink) {
            iCalText.append("rnURL;VALUE=URI:(eventUrl)")
        }
    
        let timeZoneIdentifier = occasion.timeZone.identifier
    
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyyMMdd'T'HHmmss'Z'"
        let startDateTimeText = dateFormatter.string(from: occasion.startDateTime)
        let endDateTimeText = dateFormatter.string(from: occasion.endDateTime)
    
        iCalText.append("rnDTSTART;TZID=(timeZoneIdentifier):(startDateTimeText)")
        iCalText.append("rnDTEND;TZID=(timeZoneIdentifier):(endDateTimeText)")

        iCalText.append("rnLOCATION:(occasion.location.replacingOccurrences(of: ",", with: ","))")
    
        iCalText.append("rnEND:VEVENT")
        iCalText.append("rnEND:VCALENDAR")
    
        guard let documentDirectoryUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
        else {
            completion(nil)
            return
        }
            
        do {
            let icsFileLocation = documentDirectoryUrl.appendingPathComponent("Occasion.ics")
            strive iCalText.write(to: icsFileLocation, atomically: true, encoding: String.Encoding.utf8.rawValue)
            completion(icsFileLocation)
            print(icsFileLocation)
        } catch {
            completion(nil)
        }
    
    }

    func shareICSFile(from eventInformation: EventInformation) {
        DispatchQueue.international(qos: .userInitiated).async { [weak self] in
            guard let self = self else { return }
            self.generateICSFile(occasion: eventInformation) { [weak self] icsFileUrl in
                guard let self = self, let viewController = self.viewController, let icsFileUrl = icsFileUrl else {
                    return
                }
                let eventDetailsText = "(eventInformation.title) nn (eventInformation.hyperlink)"
                let sharingItems: [Any] = [eventDetailsText, icsFileUrl]
                let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: [])
                DispatchQueue.essential.async {
                     activityViewController.popoverPresentationController?.sourceView = viewController.view
                    viewController.current(activityViewController, animated: true, completion: nil)
                }
            }
        }
    }

}

Sharing from messages app
Sharing from Mail app

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments