2017-05-13 5 views
0

私は自分のアプリに予定リマインダセクションを作成しましたが、アプリの最初の使用は保存されていないようです。私はリマインダーボタンをクリックすると、ポップアップアラートが表示され、リマインダーポップアップにアクセスしたいというメッセージが表示されます。この人の最初の予定は保存されていないので、私は間違って何をしたのか分からないのですか?私のアプリからリマインダーアプリにデータ(ラベルと日付ピッカー)を送信

import UIKit 
import EventKit 

class FirstViewController: UIViewController { 

    @IBOutlet weak var reminderText: UITextField! 
    @IBOutlet weak var myDatePicker: UIDatePicker! 
    let appDelegate = UIApplication.shared.delegate 
     as! AppDelegate 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

    } 

    @IBAction func setReminder(_ sender: AnyObject) { 

     if reminderText.text == "" { 

      // Create the alert controller 
      let alertController = UIAlertController(title: "Information Needed", message: "Please type in your treatment and select the correct date and time you wish to be reminded about before pressing the Create Appointment Reminder button.", preferredStyle: .alert) 

      // Create the actions 
      let okAction = UIAlertAction(title: "Got It", style: UIAlertActionStyle.default) { 
       UIAlertAction in 
       NSLog("OK Pressed") 
      } 

      // Add the actions 
      alertController.addAction(okAction) 

      // Present the controller 
      self.present(alertController, animated: true, completion: nil) 

     } else { 

      if appDelegate.eventStore == nil { 
       appDelegate.eventStore = EKEventStore() 
       appDelegate.eventStore?.requestAccess(
        to: EKEntityType.reminder, completion: {(granted, error) in 
         if !granted { 
          print("Access to store not granted") 
          print(error!.localizedDescription) 
         } else { 
          print("Access granted") 
         } 
       }) 
      } 

      if (appDelegate.eventStore != nil) { 
       self.createReminder() 
      } 
     } 

     self.reminderText.resignFirstResponder() 

    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 

     textField.resignFirstResponder() 

     return true 

    } 

    func createReminder() { 

     let reminder = EKReminder(eventStore: appDelegate.eventStore!) 

     reminder.title = reminderText.text! + " " + "(Pose Beauty Salon)" 
     reminder.calendar = 
      appDelegate.eventStore!.defaultCalendarForNewReminders() 
     let date = myDatePicker.date 
     let alarm = EKAlarm(absoluteDate: date) 

     reminder.addAlarm(alarm) 

     do { 
      try appDelegate.eventStore?.save(reminder, 
              commit: true) 
     } catch let error { 
      print("Reminder failed with error \(error.localizedDescription)") 
     } 

     // Create the alert controller 
     let alertController = UIAlertController(title: "Reminder Created Successfully", message: "Your \(reminderText.text!) appointment reminder at Pose Beauty Salon has been successfully created in your iPhone Reminders app. Thank You! ", preferredStyle: .alert) 

     // Create the actions 
     let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { 
      UIAlertAction in 
      NSLog("OK Pressed") 
     } 

     // Add the actions 
     alertController.addAction(okAction) 

     // Present the controller 
     self.present(alertController, animated: true, completion: nil) 

     reminderText.text = "" 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     reminderText.endEditing(true) 
    } 
} 
+1

次のようなものを使用することができます。 – Paulw11

+0

こんにちは@ Paulw11私は私のすべての小切手を私のコードでカバーしていたと思った。私はどこに間違っているのか分かりますか? – Elfuthark

+0

'requestAccess'は非同期的に完了しますので、" access granted "を印刷した後、クロージャー内で' createReminder'を呼び出す必要があります。 – Paulw11

答えて

1

あなたのロジックが必要以上にはるかに複雑である:

は、ここに私のコードです。アクセスがすでに許可(または拒否)された後でイベント・ストアへのアクセスを要求すると、ユーザーにプロンプ​​トを出さずにこれらの許可が使用されるという事実を利用できます。

また、AppDelegateのプロパティを他のクラスから初期化することもできません。 AppDelegate上にEventStoreは必要ありません。必要なときにインスタンスを作成するだけで済みます。あなたが正常に完了したアクセスのためのリクエストの後に完了ハンドラでリマインダーを作成する必要があります

} else { 
    let eventStore = EKEventStore() 
    eventStore.requestAccess(
       to: EKEntityType.reminder, completion: {(granted, error) in 
     if !granted { 
      print("Access to store not granted") 
      print(error!.localizedDescription) 
     } else { 
      print("Access granted") 
      self.createReminder(in: eventStore) 
     } 
    }) 
} 


func createReminder(in eventStore: EKEventStore) { 
    .... 
} 
関連する問題