2016-12-15 4 views
0

나는 아이폰 OS의 알림을 사용하여 권한을 필요로하고 그것을 위해 내가이 일을 해요 :기능 실행 (액세스 권한에 대한 요청 후) 지연 IOS

switch EKEventStore.authorizationStatus(for: .reminder) { 
case .authorized:      
    print("Access granted") 
    //everything's normal here 
    //executing my function here 

case .denied: 
    print("Access denied") 
case .notDetermined:  
    print("not defined yet") 

    //No determined so asking for permission 
    self.eventStore.requestAccess(to: .reminder) { (granted, error) -> Void in 
     if granted == true { 
      print("permission granted") 

      //executing my function here after getting permissions but this piece of code executes after a long delay 
      //this piece of codes are executing after a while say 5-10 seconds 

     }else if error != nil{  
      print("ther's an error : \(error)") 
     }    
    } 

default: 
    print("Case Default") 
} 

으로 앱은 알림 및 사용자의 권한을하라는 메시지를 표시 할 때 위의 설명 권한을 부여합니다. 내 다음 기능이 실행되었지만 잠시 후 (5-10 초)

누구가 설명 할 수 있습니까?

답변

1

requestAccess의 완료가 주 스레드에서 호출되지 않습니다. permissions granted 코드를 내부에 배치하십시오. 비동기 교환 :

DispatchQueue.main.async { 
    print("permission granted") 
} 
+0

응답을 주셔서 감사합니다. 노력에 감사드립니다. –

1

요청 권한은 순전히 비동기식 프로세스이므로 코드에서 제어 할 수없는 바로 그 기능을 즉시 실행할 수 없습니다. 응용 프로그램 코드는 권한을 요청할 수 있으며 요청 된 권한을 실제로 받으면 지연되는 OS를 기반으로 권한이 부여 될 때 위임자 콜백 핸들러를 가져옵니다.

기본 UI 스레드에서 실행되지 않는 스레드/블록의 권한을 요청할 수 있으며 해당 코드의 실행에 보이지 않는 지연이있을 수 있습니다. 권한 요청을 시작하는 코드를 확인해야합니다.

+0

안녕하십니까, 감사합니다. –