使用 Expo createEventAsync IOS 将约会添加到日历

埃梅考科利

SDK 版本:38 平台(Android/iOS):

我很难让此代码返回日历 ID,我很感激任何人的帮助,因为关于新日历 API 更改的信息似乎很少。

async obtainCalendarPermission() {
    let permission = await Permissions.getAsync(Permissions.CALENDAR)
     if (permission.status !== 'granted') {
       permission = await Permissions.askAsync(Permissions.CALENDAR)
       return
     }
    if (permission.status !== 'granted') {
      permission = await Permissions.askAsync(Permissions.REMINDERS)
      return
     
      if (permission.status !== 'granted') {
        Alert.alert('Permission not granted to calendar')
      }
    }
    return permission
  }

async function getDefaultCalendarSource() {
  const calendars = await Calendar.getCalendarsAsync()
  const defaultCalendars = calendars.filter(
    (each) => each.source.name === 'Default',
  )
  return defaultCalendars[0].source
}

async addReservationToCalendar(date){
    await this.obtainCalendarPermission()
    const permission = await Permissions.askAsync(Permissions.REMINDERS)
    if (permission.status !== 'granted') 
    var calendars = Calendar.getCalendarsAsync().then(calendars => console.log(calendars))
      
    const defaultCalendarSource = Platform.OS === 'ios' ? await getDefaultCalendarSource(): { isLocalAccount: true, name: 'Expo Calendar' };
  console.log(defaultCalendarSource ,+'emeka')

    let dateMs = Date.parse(date)
    let startDate = new Date(dateMs)
    let endDate = new Date(dateMs + 2 * 60 * 60 * 1000)

    const calendarId = await Calendar.createEventAsync(
      defaultCalendarSource.id,
      {
        title: 'Con Fusion Table Reservation',
        startDate: startDate,
        endDate: endDate,
        timeZone: 'Asia/Hong_Kong',
        location:
          '121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong',
      },
    )}
埃梅考科利

我已经解决了。我很难获得日历 ID,因为它是新 createEventAsync() API 的要求。这是为我解决问题的完整代码,我将其粘贴在这里,以便有人可以使用或改进它。

它生成一个唯一的日历 ID 并将预订保存到 IOS 上的日历中,我没有在 android 上测试它。

async function getDefaultCalendarSource() {
  await Calendar.getCalendarsAsync().then((id) => console.log(id))
}

 async obtainCalendarPermission() {
    let permission = await Permissions.getAsync(Permissions.CALENDAR)
    if (permission.status !== 'granted') {
      permission = await Permissions.askAsync(Permissions.CALENDAR)
      return
    }
    if (permission.status !== 'granted') {
      permission = await Permissions.askAsync(Permissions.REMINDERS)
      return

      if (permission.status !== 'granted') {
        Alert.alert('Permission not granted to calendar')
      }
    }
    return permission
  }
async addReservationToCalendar(date) {
    await this.obtainCalendarPermission()
    var dateMs = Date.parse(date)
    var startDate = new Date(dateMs)
    var endDate = new Date(dateMs + 2 * 60 * 60 * 1000)

    getDefaultCalendarSource()
    const newCalendar = await Calendar.createCalendarAsync({
      title: 'Test Reservation',
      color: '#512DA8',
      entityType: Calendar.EntityTypes.EVENT,
      sourceId: getDefaultCalendarSource.id,
      source: getDefaultCalendarSource,
      name: 'Restauran Reservation',
      ownerAccount: 'personal',
      accessLevel: Calendar.CalendarAccessLevel.OWNER,
    })

      .then((id) => {
        Calendar.createEventAsync(id, {
          title: 'Table Reservation',
          startDate: startDate,
          endDate: endDate,
          timeZone: 'Asia/Hong_Kong',
          location:
            '121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong',
        }).catch((err) => console.log(err))
        // console.log(`calendar ID is: ${id}`)
      })
      .catch((err) => console.log(err))
  }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Objective C在iOS 9.3中将动画添加到启动屏幕

使用Swift将徽章添加到iOS 8中的应用程序图标

如何使用Swift iOS将动作添加到UIAlertView按钮

将位置添加到EKEvent IOS日历

如何使用照片框架将照片添加到IOS 8中的照片库

使用droid项目将ios项目添加到xamarin表单解决方案

以编程方式将日历事件添加到iOS或Android上的Outlook

使用php将事件添加到Google日历

React-Native错误使用expo将视频添加到app.js作为背景

如何将照片添加到iOS模拟器以测试使用巨大的照片库?

使用Swift将身份验证凭据添加到iOS上的钥匙串

将TypeScript添加到React-Native Expo项目

可以将google日历添加到iOS应用程序吗?

Calendar.createEventAsync在仅Android Expo上崩溃

使用React Native和Expo SQLite将大数据存储在iOS和Android移动设备上

使用Swift包管理器将Google admob添加到iOS项目

ios将事件添加到Google日历

通过Json iOS Titan将事件添加到日历

使用Ionic框架将事件添加到设备日历

使用GNOME日历将事件添加到Google日历

使用Apple Watch将事件添加到日历中

使用MIDINotes将声音字体添加到MusicTrack吗?CoreMIDI iOS

在iOS中的特定位置使用javascript函数将文本添加到uiwebview

如何使用FullCalendar将事件添加到Google日历?

如何使用SegmentControl iOS将数据添加到上次选择的默认索引

使用 Swift 将 String:Int 字典中的项目添加到 iOS 上的 UITableView

Swift iOS - 使用 SegmentedControl 将 ChildViewController 添加到 CollectionView 部分

在 MS Access 中使用 VBA 将约会添加到别人共享的 Outlook 日历

无法在 iOS 上使用 Expo 保存视频