Я нашел решение, манипулируя строковым значением, возвращаемым QR-кодом:
var aStr = codeString.replacingOccurrences(of: "BEGIN:VEVENT", with: "")
aStr = aStr.replacingOccurrences(of: "\n", with: "")
aStr = aStr.replacingOccurrences(of: "SUMMARY:", with: "")
aStr = aStr.replacingOccurrences(of: "LOCATION:", with: "_")
aStr = aStr.replacingOccurrences(of: "DTSTART:", with: "_")
aStr = aStr.replacingOccurrences(of: "DTEND:", with: "_")
aStr = aStr.replacingOccurrences(of: "END:VEVENT", with: "")
print("\nEVENT STRING: \(aStr)\n")
// Make an array of aStr items
let evArr = aStr.components(separatedBy: "_")
let startDateStr = "\(evArr[2])"
let endDateStr = "\(evArr[3])"
// Get dates from Strings
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.timeZone = TimeZone.autoupdatingCurrent
df.dateFormat = "yyyyMMdd'T'HHmmss"
let startDate = df.date(from: startDateStr)
let endDate = df.date(from: endDateStr)
// Store Event in Calendar
let eventStore = EKEventStore()
eventStore.requestAccess(to: EKEntityType.event) { (granted, error) in
if granted && error == nil {
print("ACCESS GRANTED \(granted)")
print("ERROR: \(String(describing: error))")
let event = EKEvent(eventStore: eventStore)
event.title = "\(evArr[0])"
event.startDate = startDate!
event.endDate = endDate!
event.notes = "EVENT: \(evArr[0])\nLOCATION: \(evArr[1])"
// Save Event...
event.calendar = eventStore.defaultCalendarForNewEvents
do { try eventStore.save(event, span: .thisEvent)
} catch let error as NSError { print("Failed to save Event: \(error)") }
print("Event saved in your Calendar app")
// error
} else { print("Failed to save Event: \(error!.localizedDescription)") }
Обратите внимание, что этот код обрабатывает QR-код типа vEvent , созданный https://www.qrcode -monkey.com / # event .
Но если вы создадите событие на https://www.qrstuff.com,, то это будет событие типа vCalendar , поэтому вам потребуется код ниже, чтобы сохранить его в приложении Календарь iOS:
var aStr = codeString.replacingOccurrences(of: "BEGIN:VCALENDAR", with: "")
aStr = aStr.replacingOccurrences(of: "VERSION:2.0", with: "")
aStr = aStr.replacingOccurrences(of: "BEGIN:VEVENT", with: "")
aStr = aStr.replacingOccurrences(of: "\n", with: "")
aStr = aStr.replacingOccurrences(of: "\r", with: "")
aStr = aStr.replacingOccurrences(of: "SUMMARY;CHARSET=utf-8:", with: "")
aStr = aStr.replacingOccurrences(of: "LOCATION;CHARSET=utf-8:", with: "_")
aStr = aStr.replacingOccurrences(of: "DTSTART:", with: "_")
aStr = aStr.replacingOccurrences(of: "DTEND:", with: "_")
aStr = aStr.replacingOccurrences(of: "END:VEVENT", with: "")
aStr = aStr.replacingOccurrences(of: "END:VCALENDAR", with: "")
// Make an array of aStr items
let evArr = aStr.components(separatedBy: "_")
let startDateStr = "\(evArr[2])"
let endDateStr = "\(evArr[3])"
// Get dates from Strings
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.timeZone = TimeZone.autoupdatingCurrent
df.dateFormat = "yyyyMMdd'T'HHmmssZ"
let startDate = df.date(from: startDateStr)
let endDate = df.date(from: endDateStr)
// Store Event in Calendar
let eventStore = EKEventStore()
eventStore.requestAccess(to: EKEntityType.event) { (granted, error) in
if granted && error == nil {
print("ACCESS GRANTED \(granted)")
print("ERROR: \(String(describing: error))")
let event = EKEvent(eventStore: eventStore)
event.title = "\(evArr[0])"
event.startDate = startDate!
event.endDate = endDate!
event.notes = "EVENT: \(evArr[0])\nLOCATION: \(evArr[1])"
// Save Event...
event.calendar = eventStore.defaultCalendarForNewEvents
do { try eventStore.save(event, span: .thisEvent)
} catch let error as NSError { print("Failed to save Event: \(error)") }
print("This Event has been saved in your Calendar app")
// error
} else { print("Failed to save Event: \(error!.localizedDescription)") }