Tôi muốn biết cách thêm sự kiện lịch vào thiết bị, nhưng sử dụng nhanh chóng. Tôi biết có một số ví dụ được thực hiện trong Objective-C, nhưng tại thời điểm này không có gì nhanh chóng. Cảm ơn nhiều.Cách thêm sự kiện vào lịch thiết bị bằng cách sử dụng nhanh
Trả lời
Swift 3.0 phiên bản
let eventStore : EKEventStore = EKEventStore()
// 'EKEntityTypeReminder' or 'EKEntityTypeEvent'
eventStore.requestAccess(to: .event) { (granted, error) in
if (granted) && (error == nil) {
print("granted \(granted)")
print("error \(error)")
let event:EKEvent = EKEvent(eventStore: eventStore)
event.title = "Test Title"
event.startDate = Date()
event.endDate = Date()
event.notes = "This is a note"
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let error as NSError {
print("failed to save event with error : \(error)")
}
print("Saved Event")
}
else{
print("failed to save event with error : \(error) or access not granted")
}
}
tham khảo: https://gist.github.com/mchirico/d072c4e38bda61040f91
cho những người không biết bạn phải nhập: nhập sự kiệnKit để làm cho nó hoạt động –
nó cung cấp lỗi "sử dụng số nhận dạng chưa được giải quyết EKEntityTypeEvent". tui bỏ lỡ điều gì vậy ? – Dashrath
@Dashrath Sử dụng 'EKEntityType.Event' (vài thay đổi trong Swift 2.0) thay vì –
tôi đã có thể điều chỉnh này và loại bỏ các lỗi biên dịch đề cập trong các ý kiến cho các câu trả lời ở trên (và một số khác), như sau:
var eventStore : EKEventStore = EKEventStore()
// 'EKEntityTypeReminder' or 'EKEntityTypeEvent'
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: {
(granted, error) in
if (granted) && (error == nil) {
print("granted \(granted)")
print("error \(error)")
var event:EKEvent = EKEvent(eventStore: eventStore)
event.title = "Test Title"
event.startDate = NSDate()
event.endDate = NSDate()
event.notes = "This is a note"
event.calendar = eventStore.defaultCalendarForNewEvents
eventStore.saveEvent(event, span: EKSpan.ThisEvent, error: nil)
print("Saved Event")
}
})
Tuy nhiên, tôi vẫn gặp lỗi sau tại t cuối cùng về "EKSpan.ThisEvent": Nhãn đối số không đúng trong cuộc gọi (có ': span: error:', expected ': span: commit:').
Tôi đã thử thay đổi "lỗi" thành "cam kết", nhưng nó đã cho tôi một lỗi trình biên dịch nói rằng nó dự kiến một Bool thay vì không. Nó có vẻ như một vấn đề phải làm với bản cập nhật trong cú pháp nhanh chóng.
Chỉnh sửa: Tôi đã kết thúc theo dõi this tutorial và có thể làm cho thiết bị hoạt động.
Trước tiên, hãy yêu cầu quyền truy cập vào lịch và (nếu quyền đó được cấp) gọi hàm để thêm sự kiện.
var savedEventId : String = "" func requestAccessPermission() { let eventStore = EKEventStore() let startDate = NSDate() let endDate = startDate.dateByAddingTimeInterval(60 * 60) // Ends one hour later if (EKEventStore.authorizationStatusForEntityType(.Event) != EKAuthorizationStatus.Authorized) { eventStore.requestAccessToEntityType(.Event, completion: { granted, error in self.createEvent(eventStore, title: "Test Title", startDate: startDate, endDate: endDate) }) } else { createEvent(eventStore, title: "Test Title", startDate: startDate, endDate: endDate) } }
Chức năng đó được gọi là trong đoạn mã trên để thêm sự kiện:
func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) { let event = EKEvent(eventStore: eventStore) event.title = title event.startDate = startDate event.endDate = endDate event.calendar = eventStore.defaultCalendarForNewEvents do { try eventStore.saveEvent(event, span: .ThisEvent) savedEventId = event.eventIdentifier } catch { print("Error Saving") } }
Swift 3.0 tương thích:
func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.notes = description
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let e as NSError {
completion?(false, e)
return
}
completion?(true, nil)
} else {
completion?(false, error as NSError?)
}
})
}
Và cũng nhập EventKit
Vì vậy, bạn có thể dễ dàng gọi phương pháp này từ khắp mọi nơi:
addEventToCalendar(title: "Girlfriend birthday", description: "Remember or die!", startDate: NSDate(), endDate: NSDate())
Nếu bạn thích, bạn có thể đặt phương pháp này bên trong một lớp utiliy và xác định nó như là 'tĩnh'.
Câu trả lời hay, sạch sẽ, làm việc –
Bạn cần thêm "Quyền riêng tư - Lịch sử dụng Mô tả "trong info.plist. –
Bạn cần thêm "Bảo mật - Mô tả sử dụng lịch" vào info.plist. mã sau đây làm việc với phiên bản mới nhất của xcode và nhanh chóng 3.
import EventKit
class EventHelper
{
let appleEventStore = EKEventStore()
var calendars: [EKCalendar]?
func generateEvent() {
let status = EKEventStore.authorizationStatus(for: EKEntityType.event)
switch (status)
{
case EKAuthorizationStatus.notDetermined:
// This happens on first-run
requestAccessToCalendar()
case EKAuthorizationStatus.authorized:
// User has access
print("User has access to calendar")
self.addAppleEvents()
case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:
// We need to help them give us permission
noPermission()
}
}
func noPermission()
{
print("User has to change settings...goto settings to view access")
}
func requestAccessToCalendar() {
appleEventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
DispatchQueue.main.async {
print("User has access to calendar")
self.addAppleEvents()
}
} else {
DispatchQueue.main.async{
self.noPermission()
}
}
})
}
func addAppleEvents()
{
let event:EKEvent = EKEvent(eventStore: appleEventStore)
event.title = "Test Event"
event.startDate = NSDate() as Date
event.endDate = NSDate() as Date
event.notes = "This is a note"
event.calendar = appleEventStore.defaultCalendarForNewEvents
do {
try appleEventStore.save(event, span: .thisEvent)
print("events added with dates:")
} catch let e as NSError {
print(e.description)
return
}
print("Saved Event")
}
}
này thật sự rất chậm trên iOS 11,2 Xcode 9.2, vì vậy tôi sửa đổi câu trả lời Luca Davanzo để sử dụng hàng đợi (chỉ hoạt động nhanh hơn rất nhiều):
func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
DispatchQueue.global(qos: .background).async {() -> Void in
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.notes = description
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let e as NSError {
completion?(false, e)
return
}
completion?(true, nil)
} else {
completion?(false, error as NSError?)
}
})
}
}
- 1. Làm cách nào để thêm sự kiện trên lịch Google bằng API lịch sử dụng Android?
- 2. Thêm sự kiện vào lịch rất chậm
- 3. Thêm sự kiện vào lịch nhìn bằng tập lệnh php
- 4. cách tải danh sách sự kiện lịch trên thiết bị trong thiết bị Android?
- 5. Thêm Sự kiện hàng tuần vào Lịch
- 6. Có cách nào để thêm lời nhắc vào sự kiện lịch mới bằng Mục đích không?
- 7. chèn sự kiện vào lịch sử dụng mục đích
- 8. Cách Thêm sự kiện trong Lịch Google từ Hoạt động?
- 9. Chèn nhiều sự kiện vào lịch Android
- 10. Thêm sự kiện vào lịch trong xcode iOS
- 11. Làm cách nào để tự động thêm sự kiện vào lịch?
- 12. Cách thêm thiết bị mới vào mã nguồn QEMU?
- 13. sự kiện chạm trong linux bằng cách sử dụng Qt
- 14. cách đính kèm sự kiện vào <body> bằng cách sử dụng backbone.js?
- 15. cách thêm sự kiện bàn giao Xorg vào g_main_loop
- 16. Cách thêm hoặc nối trạng thái mớiOutject vào lịch sử
- 17. Cách thêm sự kiện onclick vào phần tử joint.js?
- 18. Lấy cáp USB cắm vào/ra sự kiện bằng cách sử dụng EXTRA_PLUGGED không hoạt động
- 19. nâng cao sự kiện vb6 bằng cách sử dụng interop
- 20. Thêm tiêu đề vào UITableView bằng cách sử dụng nhanh chóng 2.2
- 21. Gắn thêm sự kiện HTML w/click bằng cách sử dụng jQuery
- 22. thêm vào sự kiện window.onload?
- 23. Cách thêm sự kiện trong lịch mặc định của Android 4.0?
- 24. Thêm hovertext sự kiện vào fullcalendar
- 25. thêm sự kiện vào lịch IOS7 với ứng dụng của tôi
- 26. Cách xóa tất cả Lịch sự kiện của ứng dụng của tôi khi ứng dụng bị xóa khỏi thiết bị trong iOS
- 27. Cách duy trì lịch sử công việc bằng cách sử dụng Lịch hẹn Quartz
- 28. Chèn sự kiện tự động vào Fullcalendar bằng cách sử dụng Jquery
- 29. Chế độ xem SQL Server: cách thêm hàng bị thiếu bằng cách sử dụng nội suy
- 30. Textarea chậm trễ sau khi đăng ký sự kiện `keydown` bằng cách sử dụng Javascript
Swift 2.0 Thực hiện được thêm: [HERE ON LINK] (http: // stackoverflow.com/questions/246249/programmatically-add-custom-event-in-the-iphone-calendar/34790334 # 34790334) – Dashrath