gpt4 book ai didi

ios - 如何在 Swift iOS 中通过 QR 扫描仪扫描时在日历中添加事件

转载 作者:行者123 更新时间:2023-11-28 07:25:08 27 4
gpt4 key购买 nike

在发布之前我确实搜索过这个答案但没有找到我需要的东西所以想问问你是否可以帮助我。

我有 iOS Swift 应用程序,它可以扫描二维码,在我的案例中是日历事件,然后将其显示为警报弹出窗口。我需要的是扫描二维码(日历事件),当扫描提示是否将事件添加到日历中时,也将扫描限制为事件二维码。

有效的代码来自 https://medium.com/@gayatri.hedau/qr-code-scanner-with-avcapturesession-ios-swift-5007ee498b49 , 但在那里您可以确认您看到了扫描的信息。

带有事件信息的二维码创建于 https://qrcode.tec-it.com/en/Calendar

我不太了解,但我认为应该调整下面的部分代码,因为这部分显示了警报二维码。

   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)
}

当扫描二维码时,我得到了这个

BEGIN:VEVENT SUMMARY:Student meeting July DESCRIPTION:Attend student meeting LOCATION:Meeting room 21 DTSTART:20190710T140000Z END:VEVENT

此外,当使用来自 AppStore 的类似 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)") }


请注意,此代码处理类型为 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)") }

关于ios - 如何在 Swift iOS 中通过 QR 扫描仪扫描时在日历中添加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56818818/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com