2017-05-13 1 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) { 
    .... 
}