     }
 
     public func dismissWebViewController() {
+        let completion: () -> Void = { [unowned self] in
+            self.delegate?.didDismiss()
+        }
         #if os(iOS) || os(tvOS)
-            self.dismissViewControllerAnimated(true, completion: nil)
+            let animated = true
+            if let navigationController = self.navigationController {
+                navigationController.popViewControllerAnimated(animated)
+            }
+            else if let parentViewController = self.parentViewController {
+                // The presenting view controller is responsible for dismissing the view controller it presented
+                parentViewController.dismissViewControllerAnimated(animated, completion: completion)
+            }
+            else {
+                #if !OAUTH_APP_EXTENSIONS
+                    if let topViewController = UIApplication.topViewController {
+                        topViewController.dismissViewControllerAnimated(animated, completion: completion)
+                    }
+                    else {
+                        // keep old code...
+                        self.dismissViewControllerAnimated(animated, completion: completion)
+                    }
+                #else
+                    // keep old code...
+                    self.dismissViewControllerAnimated(animated, completion: completion)
+                #endif
+                
+            }
         #elseif os(watchOS)
             self.dismissController()
         #elseif os(OSX)

    public typealias OAuthViewController = NSViewController
#endif

public class OAuthWebViewController: OAuthViewController, OAuthSwiftURLHandlerType {

    public func handle(url: NSURL) {
        // do UI in main thread
        if NSThread.isMainThread() {

    public static var userActivityType: String = "org.github.dongri.oauthswift.connect"
    #endif

    public func doHandle(url: NSURL) {
        let completion: () -> Void = { [unowned self] in
            self.delegate?.didPresent()
        }
        #if os(iOS) || os(tvOS)
            let animated = true
            if let navigationController = self.navigationController {
                navigationController.pushViewController(self, animated: animated)
            }
            else if let p = self.parentViewController {
                p.presentViewController(self, animated: animated, completion: completion)
            }
            else {
                #if !OAUTH_APP_EXTENSIONS
                    UIApplication.topViewController?.presentViewController(self, animated: animated, completion: completion)
                #endif
            }
        #elseif os(watchOS)
