Open URL in specific TabBar
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
ios swift uitabbar
add a comment |
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
ios swift uitabbar
add a comment |
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
ios swift uitabbar
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
ios swift uitabbar
ios swift uitabbar
asked Nov 15 '18 at 16:40
AlbertBAlbertB
3218
3218
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id
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
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 delegateweak 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 withreturn false
Thank you!
– AlbertB
Nov 15 '18 at 17:46
|
show 2 more comments
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)
Need to open in a new window on safari, thanks.
– AlbertB
Nov 15 '18 at 17:05
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id
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
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 delegateweak 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 withreturn false
Thank you!
– AlbertB
Nov 15 '18 at 17:46
|
show 2 more comments
Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id
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
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 delegateweak 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 withreturn false
Thank you!
– AlbertB
Nov 15 '18 at 17:46
|
show 2 more comments
Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id
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
Add to your UITabBarController extension with UITabBarControllerDelegate, also set to your Tab 4 restoration Id
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
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 delegateweak 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 withreturn false
Thank you!
– AlbertB
Nov 15 '18 at 17:46
|
show 2 more comments
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 delegateweak 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 withreturn 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
|
show 2 more comments
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)
Need to open in a new window on safari, thanks.
– AlbertB
Nov 15 '18 at 17:05
add a comment |
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)
Need to open in a new window on safari, thanks.
– AlbertB
Nov 15 '18 at 17:05
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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