1

현지 알림으로 작업 중이며 특정 viewController을 표시하려고하지만이 포럼에서 찾은 내용을 시도했으며 표시된보기와 비정상적인 동작이 나타납니다.알림 작업에서 특정보기 컨트롤러 제공

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

    print("didReceive Method called") 

    if response.actionIdentifier == "actionOne" { 
     DispatchQueue.main.async(execute: { 
      self.notificationAction1() 
     }) 
    } else if response.actionIdentifier == "actionTwo" { 
     DispatchQueue.main.async(execute: { 
      self.notificationAction2() 
     }) 

    } else if response.actionIdentifier == "actionThree" { 

    } 
    completionHandler() 
} 

func notificationAction1() { 
    redirectToVC() 
} 

func redirectToVC() { 
    let toVC = VersesViewController() 
    if self.window != nil && self.window?.rootViewController != nil { 
     let rootVC = self.window?.rootViewController! 
     if rootVC is UINavigationController { 
      (rootVC as! UINavigationController).pushViewController(toVC, animated: true) 
     } else { 
      rootVC?.present(toVC, animated: true, completion: { 
       //Do something 
      }) 
     } 
    } 
} 

코드 (특히 redirectToVC() 방법)에 어떤 문제가 여기 this picture here: 그리고에 AppDelegate.swift의 소스 코드는? 도움이 될 것입니다.

+0

현재 작동합니까? – Mannopson

+0

소스 코드 위에있는 그림을 보면,보기 컨트롤러는 비어있는 상태입니다. –

답변

1

방금이 답변을 발견했습니다. 기본적으로 AppDelegate에서보기 컨트롤러를 제공합니다. 당신이 현재 화면의 특정 뷰 컨트롤러를 제공 할 때마다 탐색을 통해 밀거나 제시 될 수있는 컨트롤러가 볼 경우 Opening view controller from app delegate using swift

+0

완벽하게 작동합니까? – Mannopson

+0

또한 rootViewController를 이와 같이 설정했지만 문제는 내 기본 rootView가 실행 파일 애니메이션보기입니다. 애니메이션을 보여주고 다른 뷰로 리다이렉트하여 rootView로 설정합니다. 하지만 내 rootView를'didReceive response :'로 리셋 할 때 특정 ViewController를 한 눈에 보여준 다음 이전 rootViewController로 돌아갑니다. 이전 rootViewController로 되돌아 가지 못하게하려면 어떻게해야합니까? –

0

func redirectToVC() { 
    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    let initialViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "versesVC") as UIViewController 
    self.window = UIWindow(frame: UIScreen.main.bounds) 
    self.window?.rootViewController = initialViewController 
    self.window?.makeKeyAndVisible() 
} 

감사합니다, 그것은 문제가 아니에요, 두 경우에 아래 사용할 수 있습니다 문제를 해결하기위한 코드.

    var appdelgateObj = UIApplication.shared.delegate as! AppDelegate 
        if let destinationVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: “ToPresentVC”) as? ToPresentVC { 
         if let window = appdelgateObj.window , let rootViewController = window.rootViewController { 
          var currentController = rootViewController 
          while let presentedController = currentController.presentedViewController { 
           currentController = presentedController 
          } 
          currentController.present(destinationVC, animated: true, completion: nil) 
         } 
        }