发布之前,我确实搜索了此答案,但没有找到我需要的答案,所以想问一下您是否能帮助我。
我有一个iOS Swift应用程序,它扫描我的日历事件中的QR码,然后将其显示为警报弹出窗口。 我需要扫描QR Code(日历事件),并且在扫描提示是否将Event添加到日历中时,还需要将扫描限制为事件QR Code。
有效的代码来自https://medium.com/@gayatri.hedau/qr-code-scanner-with-avcapturesession-ios-swift-5007ee498b49,但是您可以在此处确认已查看扫描的信息。
带有事件信息的QR码是在https://qrcode.tec-it.com/en/Calendar上创建的
我不太了解,但是我认为应该调整下面的代码部分,因为该部分显示了警报QR码。
func receivedCode(qrcode: String) {
print(qrcode)
let alertController = UIAlertController(title: "Do you want to add event into your calendar?", message: qrcode, preferredStyle: .alert)
let action1 = UIAlertAction(title: "Ok", style: .default) { (action:UIAlertAction) in
self.dismiss(animated: true)
}
alertController.addAction(action1)
self.present(alertController, animated: true, completion: nil)
}
在扫描QR码时,我得到了
BEGIN:VEVENT 摘要:7月学生会 描述:参加学生会议 地点:21号会议室 DTSTART:20190710T140000Z END:VEVENT
此外,当使用来自AppStore的类似QR Code应用扫描仪进行扫描时,它也表明是日历事件。
答案 0 :(得分:0)
我找到了通过处理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)") }
请注意,此代码处理在https://www.qrcode-monkey.com/#event上创建的类型为 vEvent 的QR码。
但是,如果您在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)") }