2016-12-28 18 views
4

「someName」カレンダーに予定を追加します。指定された名前のカレンダーが存在しない場合は、プログラムで作成します。私の問題は、(EKSource型の)localSourceがnullになるとイベントが追加されないということです。私はlocalSourceの値を取得するために3つのチェックを追加しましたが、場合によってはlocalSourceがnilでもあります。だから、私の電話イベントでは追加されますが、私の友人の電話ではそうはなりません。カレンダーに予定を追加できません

私は様々な記事を続き、私はEKSourceは、6つのタイプのものとすることができることを理解:私は理解できないのは何https://developer.apple.com/reference/eventkit/eksourcetype

は例のlocalSourceがnilであるものですか?これは通常の言語ではどういう意味ですか?コードから無関係にするために何かを行うことができますか、デバイス上でユーザーが何かを行う必要がありますか?

- (void)setCalendar { 
    NSArray *calendars = [self.eventStore calendarsForEntityType:nil]; 
    NSString *calendarTitle = someName; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title matches %@", calendarTitle]; 
    NSArray *filtered = [calendars filteredArrayUsingPredicate:predicate]; 
    if ([filtered count]) { 
     self.calendar = [filtered firstObject]; 
    } 
    else { 
     self.calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore]; 
     self.calendar.title = calendarTitle; 
     EKSource *localSource; 
     for (EKSource *source in self.eventStore.sources) 
     { 

      //if iCloud account is setup then add the event in that calendar 
      if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"]) 
      { 
       localSource = source; 
       break; 
      } 
     } 
     if (localSource == nil) 
     { 
      for (EKSource *source in self.eventStore.sources) 
      { 
       //if iCloud is not setup then look for local source 
       if (source.sourceType == EKSourceTypeLocal) 
       { 
        localSource = source; 
        break; 
       } 
      } 

     } 
     if (!localSource) { 
      localSource = [self.eventStore defaultCalendarForNewEvents].source; 
     } 
     self.calendar.source = localSource; 
     NSError *calendarErr = nil; 
     BOOL calendarSuccess = [self.eventStore saveCalendar:self.calendar commit:YES error:&calendarErr]; 
     if (!calendarSuccess) { 
      NSLog(@"Error while updating calendar %@", calendarErr); 
     } 
    } 

}

PS:私は、カレンダーイベントを追加する権限を持っています。

+0

友だちのカレンダーを操作する権限がありますか?あなたの友人はあなたのアプリにそれを許可しますか? – NSDmitry

+0

はい、すべての権限が必要です。 –

+0

あなたのお友達のiPhoneのiOSバージョンは何ですか? –

答えて

4

ローカルソースは、ユーザーのカレンダーの「icloud」、「icloud」、「gmail」などのフィールドを示しています。私のコードでは、ユーザーがicloudアカウントを設定しているのに、icloudアカウントにカレンダーに書き込む権限を与えなかった場合、ローカルソースはnullです。したがって、アプリケーションがカレンダーに書き込む権限を持っていてもローカルソースがnilであっても、カレンダーへのイベントの追加は失敗します。

私のコードは、与えられた2例のカレンダへの追加:

  1. ユーザーがiCloud上に署名していると、カレンダーに書き込むためのiCloudする許可を与えています。その場合、ローカルソースはnilではなく、 'iCloud'に 'someName'という名前でカレンダーが作成されます。
  2. ユーザーがicloudにログインしていません。今、カレンダーは、 '私のiPhoneの'ローカルソースに 'someName'という名前で作成されます。
関連する問題