Opening multiple URLs in succession

Hi all. I'm trying to call multiple URLS in succession. I understand that using a function call like

UIApplication.shared.open (url) will perform the intial call but its like making a call to an external function and handing over control and not returning to the calling function. Can anyone tell me of an alternate method to call multiple URLs in succession?

It's hard to know what you're asking. You can issue a series of UIApplication.shared.open calls and that will "open" all those URLs (whatever opening means in this context) all at the same time. For example, if you open a series of https URLs this way, they'll all open in separate browser tabs.


If you want to defer issuing a second UIApplication.shared.open call until the first one has finished being handled (whatever that means in this context), then you need to use an asynchronous code pattern. This means you use the completion handler block that you can pass as a parameter to the UIApplication.shared.open call to initiate the next call in the series.


This is not an difficult pattern, exactly, but it's a bit hard to wrap your head around until you're used to thinking through the order of execution.


Does any of that relate to the question you're asking?

Hi Quncy. Sorry I was a little vague. Here is a snippet of my code:


func counter()
{
seconds -= 1
label.text = String(seconds) + " Seconds"
if (seconds == 0)
{
let url1 = URL(string: "http://www.wix.com")!
let url2 = URL(string: "http://www.activistpost.com")!
let url3 = URL(string: "http://www.time.com")!
let url4 = URL(string: "http://www.steemit.com")!
let url5 = URL(string: "http://www.youtube.com")!
let url6 = URL(string: "http://www.cptts.net/61m.jpg")!
if #available(iOS 10.0, *) {
UIApplication.shared.open (url1)
UIApplication.shared.open (url2)
UIApplication.shared.open (url3)
UIApplication.shared.open (url4)
UIApplication.shared.open (url5)
UIApplication.shared.open (url6)
} else {
// Fallback on earlier versions
}


The first call to wix.com opens up the website and it stays there since there is no way to return. I had also planned to insert sleep statements but I'm told that this is incorrect in this section of code as well.



As you can tell I'm not an expert yet. :-|

>> it stays there since there is no way to return


I still don't understand. What stays where? Return from what?


In terms of code, your open calls don't "go" anywhere, so there's no return involved. Each call initiates opening of a web page, but that happens asynchronously in a different app (Safari, I guess, since this is http). I would expect there'd be 6 tabs open, though perhaps the behavior is that only one tab is created, so one of the 6 pages "wins".


So, are you trying to display six pages in turn, with a pause on each page? In that case, you'll need a timer to cause the open calls to be issued at the correct time for each one.

When I say "return" I'm using C++ terminolgy. Sorry. If you call an external function there is no way to return to the calling function in C++ unless you have a return statement. I'm told that it works the same here. Let's put that aside for now, however. I've tried with "sleep (5)" statements in between but since it stays at the wix.com page and doesn't change it made no difference. The bottom line is that when executed there is a single wix.com page open and it does not proceed to the next pages.

I tried your code and I got a similar result — only one of the web pages was displayed, though it was random which one. I'm not sure precisely why, but it looks like the "open" method merely queues a request for its URL to be delivered (to Safari in this case, since it's http), and that request isn't handled until the run loop of your main thread has a chance to run. That's why "sleep(5)" doesn't help, since the thread doesn't cycle the run loop while it's sleeping.


However, I think it's a bit more complicated than that. It looks like there's contention when those requests arrive, too. From what I could tell, the requets are processes sequentially on arrival, too, and a new request (while the current one is still loading its web page, or waiting to load) is discarded, and this happens in a single Safari tab, so that there's still only one "winner".


So, the question becomes, what are you really trying to do. If you're trying to get the effect of some kind of web page "slide show", this doesn't seem like the way to do it. It might be better to use WebKit (WKWebView) to display multiple web views in-app.

I tried your code and I got a similar result — only one of the web pages was displayed, though it was random which one.

Beyond the implementation issues you’ve seen there are conceptual problems with this. Opening a URL brings the target app to the foreground, which typically moves your app to the background, and you can’t open URLs when you’re in the background. So even if this were all 100% synchronous it still wouldn’t work.

So, the question becomes, what are you really trying to do.

Yep. An explanation of the overall goal would really help here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, Eskimo.

What I'm actually trying to do is roll out an automated wifi stress test across multiple devices. Each device goes through the page loads in succession. There are other apps out there that will perform similar functions but I wanted something customizable.

Do you have to load the URLs in Safari itself?

SFSafariViewController
is virtually identical to Safari on the performance front, and that puts you in direct control.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Opening multiple URLs in succession
 
 
Q