Open URL in specific TabBar










3















I am implementing a "Tabbet App" and I need a "Tab" to open a URL in Safari (new window not in the same viewcontroller)



Currently for example in the viewcontroller of Tab4 (Tab4ViewControllerView.swif) I have added the following code:



 guard let url = URL(string: "https://google.com") else 
return //be safe


if #available(iOS 10.0, *)
UIApplication.shared.open(url, options: [:], completionHandler: nil)
else

UIApplication.shared.openURL(url)



It works but when I return from Safari to the application, Tab4 is selected and since it does not contain anything it looks white.



I need to click on Tab4 to open Safari but it should not be selected (you must keep selecting the previous tab.)



In the example of the image: I am currently in Tab1 and pressing Tab4 should open Safari but keeping selected Tab1



sample










share|improve this question


























    3















    I am implementing a "Tabbet App" and I need a "Tab" to open a URL in Safari (new window not in the same viewcontroller)



    Currently for example in the viewcontroller of Tab4 (Tab4ViewControllerView.swif) I have added the following code:



     guard let url = URL(string: "https://google.com") else 
    return //be safe


    if #available(iOS 10.0, *)
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    else

    UIApplication.shared.openURL(url)



    It works but when I return from Safari to the application, Tab4 is selected and since it does not contain anything it looks white.



    I need to click on Tab4 to open Safari but it should not be selected (you must keep selecting the previous tab.)



    In the example of the image: I am currently in Tab1 and pressing Tab4 should open Safari but keeping selected Tab1



    sample










    share|improve this question
























      3












      3








      3








      I am implementing a "Tabbet App" and I need a "Tab" to open a URL in Safari (new window not in the same viewcontroller)



      Currently for example in the viewcontroller of Tab4 (Tab4ViewControllerView.swif) I have added the following code:



       guard let url = URL(string: "https://google.com") else 
      return //be safe


      if #available(iOS 10.0, *)
      UIApplication.shared.open(url, options: [:], completionHandler: nil)
      else

      UIApplication.shared.openURL(url)



      It works but when I return from Safari to the application, Tab4 is selected and since it does not contain anything it looks white.



      I need to click on Tab4 to open Safari but it should not be selected (you must keep selecting the previous tab.)



      In the example of the image: I am currently in Tab1 and pressing Tab4 should open Safari but keeping selected Tab1



      sample










      share|improve this question














      I am implementing a "Tabbet App" and I need a "Tab" to open a URL in Safari (new window not in the same viewcontroller)



      Currently for example in the viewcontroller of Tab4 (Tab4ViewControllerView.swif) I have added the following code:



       guard let url = URL(string: "https://google.com") else 
      return //be safe


      if #available(iOS 10.0, *)
      UIApplication.shared.open(url, options: [:], completionHandler: nil)
      else

      UIApplication.shared.openURL(url)



      It works but when I return from Safari to the application, Tab4 is selected and since it does not contain anything it looks white.



      I need to click on Tab4 to open Safari but it should not be selected (you must keep selecting the previous tab.)



      In the example of the image: I am currently in Tab1 and pressing Tab4 should open Safari but keeping selected Tab1



      sample







      ios swift uitabbar






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 16:40









      AlbertBAlbertB

      3218




      3218






















          2 Answers
          2






          active

          oldest

          votes


















          2














          Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id



          restoration id for tab



          TabBarController



          in TabBarViewContoller add:



          import UIKit

          class TabBarViewController: UITabBarController

          override func viewDidLoad()
          super.viewDidLoad()
          delegate = self
          // Do any additional setup after loading the view.



          extension TabBarViewController: UITabBarControllerDelegate
          func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
          if viewController.restorationIdentifier == "safariTabViewController"
          if let url = URL(string: "your_url"), UIApplication.shared.canOpenURL(url)
          UIApplication.shared.openURL(url)

          return false

          return true




          when you returning false in delegate - you make sure that tab will not be switched






          share|improve this answer

























          • Thanks for your response, I am implementing your code. delegate = self is correct? I get error: Use of unresolved identifier 'delegate'

            – AlbertB
            Nov 15 '18 at 17:14











          • You should add this in your UITabBarController. UITabBarController has delegate weak open var delegate: UITabBarControllerDelegate?

            – Andrew Son
            Nov 15 '18 at 17:16











          • Sorry my bad, add not storyboard id but restoration id in your 4 tab view controller

            – Andrew Son
            Nov 15 '18 at 17:23











          • @AlbertB I've updated answer with additional screenshots and code snippets.

            – Andrew Son
            Nov 15 '18 at 17:29












          • I already applied your code, now it works for me, but in the extension with return false Thank you!

            – AlbertB
            Nov 15 '18 at 17:46


















          0














          You should embed a webView in tab4, using WKWebView or UIWebView. This way you won't have to leave your application.



          Something like,



          let webView = WKWebView()



          override func loadView()
          self.view = webView



          if let url = URL(string: "https://www.apple.com")
          let request = URLRequest(url: url)
          webView.load(request)






          share|improve this answer























          • Need to open in a new window on safari, thanks.

            – AlbertB
            Nov 15 '18 at 17:05










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324080%2fopen-url-in-specific-tabbar%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id



          restoration id for tab



          TabBarController



          in TabBarViewContoller add:



          import UIKit

          class TabBarViewController: UITabBarController

          override func viewDidLoad()
          super.viewDidLoad()
          delegate = self
          // Do any additional setup after loading the view.



          extension TabBarViewController: UITabBarControllerDelegate
          func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
          if viewController.restorationIdentifier == "safariTabViewController"
          if let url = URL(string: "your_url"), UIApplication.shared.canOpenURL(url)
          UIApplication.shared.openURL(url)

          return false

          return true




          when you returning false in delegate - you make sure that tab will not be switched






          share|improve this answer

























          • Thanks for your response, I am implementing your code. delegate = self is correct? I get error: Use of unresolved identifier 'delegate'

            – AlbertB
            Nov 15 '18 at 17:14











          • You should add this in your UITabBarController. UITabBarController has delegate weak open var delegate: UITabBarControllerDelegate?

            – Andrew Son
            Nov 15 '18 at 17:16











          • Sorry my bad, add not storyboard id but restoration id in your 4 tab view controller

            – Andrew Son
            Nov 15 '18 at 17:23











          • @AlbertB I've updated answer with additional screenshots and code snippets.

            – Andrew Son
            Nov 15 '18 at 17:29












          • I already applied your code, now it works for me, but in the extension with return false Thank you!

            – AlbertB
            Nov 15 '18 at 17:46















          2














          Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id



          restoration id for tab



          TabBarController



          in TabBarViewContoller add:



          import UIKit

          class TabBarViewController: UITabBarController

          override func viewDidLoad()
          super.viewDidLoad()
          delegate = self
          // Do any additional setup after loading the view.



          extension TabBarViewController: UITabBarControllerDelegate
          func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
          if viewController.restorationIdentifier == "safariTabViewController"
          if let url = URL(string: "your_url"), UIApplication.shared.canOpenURL(url)
          UIApplication.shared.openURL(url)

          return false

          return true




          when you returning false in delegate - you make sure that tab will not be switched






          share|improve this answer

























          • Thanks for your response, I am implementing your code. delegate = self is correct? I get error: Use of unresolved identifier 'delegate'

            – AlbertB
            Nov 15 '18 at 17:14











          • You should add this in your UITabBarController. UITabBarController has delegate weak open var delegate: UITabBarControllerDelegate?

            – Andrew Son
            Nov 15 '18 at 17:16











          • Sorry my bad, add not storyboard id but restoration id in your 4 tab view controller

            – Andrew Son
            Nov 15 '18 at 17:23











          • @AlbertB I've updated answer with additional screenshots and code snippets.

            – Andrew Son
            Nov 15 '18 at 17:29












          • I already applied your code, now it works for me, but in the extension with return false Thank you!

            – AlbertB
            Nov 15 '18 at 17:46













          2












          2








          2







          Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id



          restoration id for tab



          TabBarController



          in TabBarViewContoller add:



          import UIKit

          class TabBarViewController: UITabBarController

          override func viewDidLoad()
          super.viewDidLoad()
          delegate = self
          // Do any additional setup after loading the view.



          extension TabBarViewController: UITabBarControllerDelegate
          func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
          if viewController.restorationIdentifier == "safariTabViewController"
          if let url = URL(string: "your_url"), UIApplication.shared.canOpenURL(url)
          UIApplication.shared.openURL(url)

          return false

          return true




          when you returning false in delegate - you make sure that tab will not be switched






          share|improve this answer















          Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id



          restoration id for tab



          TabBarController



          in TabBarViewContoller add:



          import UIKit

          class TabBarViewController: UITabBarController

          override func viewDidLoad()
          super.viewDidLoad()
          delegate = self
          // Do any additional setup after loading the view.



          extension TabBarViewController: UITabBarControllerDelegate
          func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
          if viewController.restorationIdentifier == "safariTabViewController"
          if let url = URL(string: "your_url"), UIApplication.shared.canOpenURL(url)
          UIApplication.shared.openURL(url)

          return false

          return true




          when you returning false in delegate - you make sure that tab will not be switched







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 17:49

























          answered Nov 15 '18 at 16:52









          Andrew SonAndrew Son

          10915




          10915












          • Thanks for your response, I am implementing your code. delegate = self is correct? I get error: Use of unresolved identifier 'delegate'

            – AlbertB
            Nov 15 '18 at 17:14











          • You should add this in your UITabBarController. UITabBarController has delegate weak open var delegate: UITabBarControllerDelegate?

            – Andrew Son
            Nov 15 '18 at 17:16











          • Sorry my bad, add not storyboard id but restoration id in your 4 tab view controller

            – Andrew Son
            Nov 15 '18 at 17:23











          • @AlbertB I've updated answer with additional screenshots and code snippets.

            – Andrew Son
            Nov 15 '18 at 17:29












          • I already applied your code, now it works for me, but in the extension with return false Thank you!

            – AlbertB
            Nov 15 '18 at 17:46

















          • Thanks for your response, I am implementing your code. delegate = self is correct? I get error: Use of unresolved identifier 'delegate'

            – AlbertB
            Nov 15 '18 at 17:14











          • You should add this in your UITabBarController. UITabBarController has delegate weak open var delegate: UITabBarControllerDelegate?

            – Andrew Son
            Nov 15 '18 at 17:16











          • Sorry my bad, add not storyboard id but restoration id in your 4 tab view controller

            – Andrew Son
            Nov 15 '18 at 17:23











          • @AlbertB I've updated answer with additional screenshots and code snippets.

            – Andrew Son
            Nov 15 '18 at 17:29












          • I already applied your code, now it works for me, but in the extension with return false Thank you!

            – AlbertB
            Nov 15 '18 at 17:46
















          Thanks for your response, I am implementing your code. delegate = self is correct? I get error: Use of unresolved identifier 'delegate'

          – AlbertB
          Nov 15 '18 at 17:14





          Thanks for your response, I am implementing your code. delegate = self is correct? I get error: Use of unresolved identifier 'delegate'

          – AlbertB
          Nov 15 '18 at 17:14













          You should add this in your UITabBarController. UITabBarController has delegate weak open var delegate: UITabBarControllerDelegate?

          – Andrew Son
          Nov 15 '18 at 17:16





          You should add this in your UITabBarController. UITabBarController has delegate weak open var delegate: UITabBarControllerDelegate?

          – Andrew Son
          Nov 15 '18 at 17:16













          Sorry my bad, add not storyboard id but restoration id in your 4 tab view controller

          – Andrew Son
          Nov 15 '18 at 17:23





          Sorry my bad, add not storyboard id but restoration id in your 4 tab view controller

          – Andrew Son
          Nov 15 '18 at 17:23













          @AlbertB I've updated answer with additional screenshots and code snippets.

          – Andrew Son
          Nov 15 '18 at 17:29






          @AlbertB I've updated answer with additional screenshots and code snippets.

          – Andrew Son
          Nov 15 '18 at 17:29














          I already applied your code, now it works for me, but in the extension with return false Thank you!

          – AlbertB
          Nov 15 '18 at 17:46





          I already applied your code, now it works for me, but in the extension with return false Thank you!

          – AlbertB
          Nov 15 '18 at 17:46













          0














          You should embed a webView in tab4, using WKWebView or UIWebView. This way you won't have to leave your application.



          Something like,



          let webView = WKWebView()



          override func loadView()
          self.view = webView



          if let url = URL(string: "https://www.apple.com")
          let request = URLRequest(url: url)
          webView.load(request)






          share|improve this answer























          • Need to open in a new window on safari, thanks.

            – AlbertB
            Nov 15 '18 at 17:05















          0














          You should embed a webView in tab4, using WKWebView or UIWebView. This way you won't have to leave your application.



          Something like,



          let webView = WKWebView()



          override func loadView()
          self.view = webView



          if let url = URL(string: "https://www.apple.com")
          let request = URLRequest(url: url)
          webView.load(request)






          share|improve this answer























          • Need to open in a new window on safari, thanks.

            – AlbertB
            Nov 15 '18 at 17:05













          0












          0








          0







          You should embed a webView in tab4, using WKWebView or UIWebView. This way you won't have to leave your application.



          Something like,



          let webView = WKWebView()



          override func loadView()
          self.view = webView



          if let url = URL(string: "https://www.apple.com")
          let request = URLRequest(url: url)
          webView.load(request)






          share|improve this answer













          You should embed a webView in tab4, using WKWebView or UIWebView. This way you won't have to leave your application.



          Something like,



          let webView = WKWebView()



          override func loadView()
          self.view = webView



          if let url = URL(string: "https://www.apple.com")
          let request = URLRequest(url: url)
          webView.load(request)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 16:49









          WoodstockWoodstock

          12.8k1362100




          12.8k1362100












          • Need to open in a new window on safari, thanks.

            – AlbertB
            Nov 15 '18 at 17:05

















          • Need to open in a new window on safari, thanks.

            – AlbertB
            Nov 15 '18 at 17:05
















          Need to open in a new window on safari, thanks.

          – AlbertB
          Nov 15 '18 at 17:05





          Need to open in a new window on safari, thanks.

          – AlbertB
          Nov 15 '18 at 17:05

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324080%2fopen-url-in-specific-tabbar%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党