Skip to content

Commit

Permalink
Merge pull request #2 from ginlo2/jeff/calender-files
Browse files Browse the repository at this point in the history
handle calendar events
  • Loading branch information
kschramm-ginlo authored May 17, 2024
2 parents 24f5e5e + 180aac3 commit a233b3d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ class ReceiveSharingIntentPlugin : FlutterPlugin, ActivityAware, MethodCallHandl
fun fromMimeType(mimeType: String?): MediaType {
return when {
mimeType?.equals("text/x-vcard", true) == true -> FILE
mimeType?.equals("text/calendar", true) == true -> FILE
mimeType?.equals("text/x-vcalendar", true) == true -> FILE
mimeType?.equals("public.ics", true) == true -> FILE
mimeType?.startsWith("image") == true -> IMAGE
mimeType?.startsWith("video") == true -> VIDEO
mimeType?.startsWith("text") == true -> TEXT
Expand Down
30 changes: 15 additions & 15 deletions example/ios/Share Extension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>SUBQUERY (
extensionItems,
$extensionItem,
SUBQUERY (
$extensionItem.attachments,
$attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.movie" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.audio" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.data" ||
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.vcard"
<string>
SUBQUERY(extensionItems, $extensionItem,
SUBQUERY($extensionItem.attachments, $attachment,
ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.file-url"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.movie"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.audio"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.data"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.vcard"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "text.calendar"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "text/x-vcalendar"
OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.ics"
).@count == $extensionItem.attachments.@count
).@count &gt; 0
).@count > 0
</string>
</dict>
<key>NSExtensionMainStoryboard</key>
Expand Down
48 changes: 39 additions & 9 deletions ios/Classes/RSIShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ open class RSIShareViewController: SLComposeServiceViewController {
return
}
switch type {
case .contact:
if let contactData = data as? Data {
this.handleMedia(forVCard: contactData,
case .contact, .calendar, .calendarText, .vcalendar:
// if shared as raw data, which is mostly the case for contacts
if let data = data as? Data {
this.handleMedia(forData: data,
type: type,
index: index,
content: content,
suggestedName: attachment.suggestedName)
// if shared as file. Calendar events (.ics) are commonly shared as files
} else if let url = data as? URL {
this.handleMedia(forFile: url,
type: type,
index: index,
content: content)
Expand Down Expand Up @@ -152,13 +160,35 @@ open class RSIShareViewController: SLComposeServiceViewController {
}
}

private func handleMedia(forVCard vCardData: Data, type: SharedMediaType, index: Int, content: NSExtensionItem){
let tempPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId)!.appendingPathComponent("TempContact.vcf")
if self.writeTempFileVCard(vCardData, to: tempPath) {
private func handleMedia(forData data: Data, type: SharedMediaType, index: Int, content: NSExtensionItem, suggestedName: String?){
let tempFileName: String
let mimeType: String?

switch type {
case .contact:
tempFileName = "TempContact.vcf"
mimeType = "public.vcard"
case .calendar:
tempFileName = "TempCalendar.ics"
mimeType = "public.ics"
case .calendarText:
tempFileName = "TempCalendarText.ics"
mimeType = "text.calendar"
case .vcalendar:
tempFileName = "TempVCalendar.vcs"
mimeType = "text/x-vcalendar"
default:
tempFileName = "TempFile.tmp"
mimeType = nil
}

let tempPath = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId)!
.appendingPathComponent(suggestedName != nil ? suggestedName! : tempFileName)
if self.writeTempFileData(data, to: tempPath) {
let newPathDecoded = tempPath.absoluteString.removingPercentEncoding!
sharedMedia.append(SharedMediaFile(
path: newPathDecoded,
mimeType: type == .contact ? "text/vcard": nil,
mimeType: mimeType,
type: type
))
}
Expand Down Expand Up @@ -274,12 +304,12 @@ open class RSIShareViewController: SLComposeServiceViewController {
}
}

private func writeTempFileVCard(_ vCardData: Data, to dstURL: URL) -> Bool {
private func writeTempFileData(_ data: Data, to dstURL: URL) -> Bool {
do {
if FileManager.default.fileExists(atPath: dstURL.path) {
try FileManager.default.removeItem(at: dstURL)
}
try vCardData.write(to: dstURL);
try data.write(to: dstURL);
return true;
} catch (let error){
print("Cannot write to temp file: \(error)");
Expand Down
15 changes: 15 additions & 0 deletions ios/Classes/SwiftReceiveSharingIntentPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ public class SharedMediaFile: Codable {
}

public enum SharedMediaType: String, Codable, CaseIterable {
case calendar
case calendarText
case vcalendar
case contact
case image
case video
Expand All @@ -240,6 +243,12 @@ public enum SharedMediaType: String, Codable, CaseIterable {
public var toUTTypeIdentifier: String {
if #available(iOS 14.0, *) {
switch self {
case .calendar:
return UTType.calendarEvent.identifier
case .calendarText:
return "text.calendar"
case .vcalendar:
return "text/x-vcalendar"
case .contact:
return UTType.vCard.identifier
case .image:
Expand All @@ -257,6 +266,12 @@ public enum SharedMediaType: String, Codable, CaseIterable {
}
}
switch self {
case .calendar:
return "public.ics"
case .calendarText:
return "text.calendar"
case .vcalendar:
return "text/x-vcalendar"
case .contact:
return "public.vcard"
case .image:
Expand Down
3 changes: 3 additions & 0 deletions lib/src/data/shared_media_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class SharedMediaFile {
}

enum SharedMediaType {
calendar('calendar'),
calendarText('calendarText'),
vcalendar('vcalendar'),
contact('contact'),
image('image'),
video('video'),
Expand Down

0 comments on commit a233b3d

Please sign in to comment.