flutter add_2_calendar更新到Xcode iOS 17后无法工作

k4ymrczo  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(180)

在我的flutter iOS应用程序中,我使用包add_2_calendar向用户日历添加一些事件。更新到iOS 17后,我注意到此功能不再工作。在我的Info.plist中,我有以下内容

<key>NSCalendarsUsageDescription</key>
    <string>$(PRODUCT_NAME) user your calendar</string>
    <key>NSContactsUsageDescription</key>
    <string>$(PRODUCT_NAME) user your calendar</string>

字符串
我尝试将NSCalendarsWriteOnlyAccessUsageDescription作为弃用的NSCalendarsUsageDescription包括在内,但没有任何变化

<key>NSCalendarsWriteOnlyAccessUsageDescription</key>
    <string>Add event to calendar</string>


在终端上,我刚刚收到消息XPC connection was invalidated
我如何在下面的flutter代码中使用它:

final Event calendarEvent = Event(
    title: '$homeTeamName vs $awayTeamName',
    description: '$leagueName ($round)\nReferee: $referee',
    location: stadium,
    startDate: matchTimestamp,
    endDate: matchTimestamp.add(const Duration(hours: 2)),
    iosParams: const IOSParams(
      reminder: Duration(hours: 1),
      url: 'https://hadjimamas.github.io/',
    ),
    androidParams: const AndroidParams(emailInvites: []),
  );

..some code

InkWell(
          onTap: () {
            Add2Calendar.addEvent2Cal(calendarEvent);
          },


封装文档add_2_calendar

ohfgkhjo

ohfgkhjo1#

在下面的链接中,您可以找到与此问题Quick Fix相关的快速修复程序
实际编辑add_2_calendar/ios/Classes/SwiftAdd2CalendarPlugin.swift如下:

import Flutter
import UIKit
import EventKit
import EventKitUI
import Foundation

extension Date {
  init(milliseconds:Double) {
      self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
  }
}

var statusBarStyle = UIApplication.shared.statusBarStyle

public class SwiftAdd2CalendarPlugin: NSObject, FlutterPlugin {

public static func register(with registrar: FlutterPluginRegistrar) {
  let channel = FlutterMethodChannel(name: "add_2_calendar", binaryMessenger: registrar.messenger())
  let instance = SwiftAdd2CalendarPlugin()
  registrar.addMethodCallDelegate(instance, channel: channel)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
  if call.method == "add2Cal" {
    let args = call.arguments as! [String:Any]
   
      
    addEventToCalendar(from: args,completion:{ (success) -> Void in
          if success {
              result(true)
          } else {
              result(false)
          }
      })
  }
}

private func addEventToCalendar(from args: [String:Any], completion: ((_ success: Bool) -> Void)? = nil) {
    
    
    let title = args["title"] as! String
    let description = args["desc"] is NSNull ? nil: args["desc"] as! String
    let location = args["location"] is NSNull ? nil: args["location"] as! String
    let timeZone = args["timeZone"] is NSNull ? nil: TimeZone(identifier: args["timeZone"] as! String)
    let startDate = Date(milliseconds: (args["startDate"] as! Double))
    let endDate = Date(milliseconds: (args["endDate"] as! Double))
    let alarmInterval = args["alarmInterval"] as? Double
    let allDay = args["allDay"] as! Bool
    let url = args["url"] as? String
    
    let eventStore = EKEventStore()
    let event = createEvent(eventStore: eventStore, alarmInterval: alarmInterval, title: title, description: description, location: location, timeZone: timeZone, startDate: startDate, endDate: endDate, allDay: allDay, url: url, args: args)

    presentCalendarModalToAddEvent(event, eventStore: eventStore, completion: completion)
}

private func createEvent(eventStore: EKEventStore, alarmInterval: Double?, title: String, description: String?, location: String?, timeZone: TimeZone?, startDate: Date?, endDate: Date?, allDay: Bool, url: String?, args: [String:Any]) -> EKEvent {
    let event = EKEvent(eventStore: eventStore)
    if let alarm = alarmInterval{
        event.addAlarm(EKAlarm(relativeOffset: alarm*(-1)))
    }
    event.title = title
    event.startDate = startDate
    event.endDate = endDate
    if (timeZone != nil) {
        event.timeZone = timeZone
    }
    if (location != nil) {
        event.location = location
    }
    if (description != nil) {
        event.notes = description
    }
    if let url = url{
        event.url = URL(string: url);
    }
    event.isAllDay = allDay
    
    if let recurrence = args["recurrence"] as? [String:Any]{
        let interval = recurrence["interval"] as! Int
        let frequency = recurrence["frequency"] as! Int
        let end = recurrence["endDate"] as? Double// Date(milliseconds: (args["startDate"] as! Double))
        let ocurrences = recurrence["ocurrences"] as? Int
        
        let recurrenceRule = EKRecurrenceRule.init(
            recurrenceWith: EKRecurrenceFrequency(rawValue: frequency)!,
            interval: interval,
            end: ocurrences != nil ? EKRecurrenceEnd.init(occurrenceCount: ocurrences!) : end != nil ? EKRecurrenceEnd.init(end: Date(milliseconds: end!)) : nil
        )
        event.recurrenceRules = [recurrenceRule]
    }
    
    return event
}

private func getAuthorizationStatus() -> EKAuthorizationStatus {
    return EKEventStore.authorizationStatus(for: EKEntityType.event)
}

// Show event kit ui to add event to calendar

func presentCalendarModalToAddEvent(_ event: EKEvent, eventStore: EKEventStore, completion: ((_ success: Bool) -> Void)? = nil) {
    if #available(iOS 17, *) {
        OperationQueue.main.addOperation {
            self.presentEventCalendarDetailModal(event: event, eventStore: eventStore)
        }
    } else {
        let authStatus = getAuthorizationStatus()
        switch authStatus {
        case .authorized:
            OperationQueue.main.addOperation {
                self.presentEventCalendarDetailModal(event: event, eventStore: eventStore)
            }
            completion?(true)
        case .notDetermined:
            //Auth is not determined
            //We should request access to the calendar
            eventStore.requestAccess(to: .event, completion: { [weak self] (granted, error) in
                if granted {
                    OperationQueue.main.addOperation {
                        self?.presentEventCalendarDetailModal(event: event, eventStore: eventStore)
                    }
                    completion?(true)
                } else {
                    // Auth denied
                    completion?(false)
                }
            })
        case .denied, .restricted:
            // Auth denied or restricted
            completion?(false)
        default:
            completion?(false)
        }
    }
}

// Present edit event calendar modal

func presentEventCalendarDetailModal(event: EKEvent, eventStore: EKEventStore) {
    let eventModalVC = EKEventEditViewController()
    eventModalVC.event = event
    eventModalVC.eventStore = eventStore
    eventModalVC.editViewDelegate = self
    
    if #available(iOS 13, *) {
        eventModalVC.modalPresentationStyle = .fullScreen
    }
    
    if let root = UIApplication.shared.keyWindow?.rootViewController {
        root.present(eventModalVC, animated: true, completion: {
            statusBarStyle = UIApplication.shared.statusBarStyle
            UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
        })
    }
}
}

extension SwiftAdd2CalendarPlugin: EKEventEditViewDelegate {

public func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
    controller.dismiss(animated: true, completion: {
        UIApplication.shared.statusBarStyle = statusBarStyle
    })
}
}

字符串

相关问题