Opening a URL in an app

Hello everyone, I would love some help.


I am updating an older app using xcode 9 in high sierra. I got through all the needed changes, except for one and it is making me crazy. I have a few buttons that go to webpages. The code to open them was super simple, but now I get the message "'openURL' was deprecated in iOS 10.0: Please use openURL:options:completionHandler: instead." I have been all over the net looking for examples and trying to edit my code to work, and I can't get anything to work.


All I want to do is open the web pages. These are a few resources for the app user and though I can tell them the url in the help area, I'd like them to be able to tap a link to go there. I previously handled this with 3 buttons wired to open the 3 pages. Now I can't get a page to open and I'm on the verge of using a TSI, but I don't want to burn one just for this. I guess on the upside, the apple support engineers would get a huge laugh. =)


Using apple.com, I would be very grateful if someone would please post the code to open a URL. Better yet, if someone could post both ways, namely how to do it by launching safari and opening it (taking them out of the app) and how to open it within the app. Also, any opinions on which way is best?


Lisa

Accepted Reply

This thread has been deleted

Thank you! I was looking for a way to close.

Replies

This SO thread is a bit dated (swift 3.x), but it uses wkwebview*, with links that open inside your app, andor handoff to safari, so:


https://stackoverflow.com/questions/36231061/wkwebview-open-links-from-certain-domain-in-safari


As for which is best, it mostly depends on your app design, etc. In any case, if your app is just a collection of links, it risks rejection during review.


*if your older app used uiwebview, note it was deprecated back w/iOS 8 in favor of wkwebview


Reference: docs w/code: https://developer.apple.com/documentation/webkit/wkwebview

What language are you using, Swift or Obj-C?


In Swift 4/Xcode 9.x, the API is "open(_: options:completionHandler:)", but the last 2 parameters are optional (defaulting to no options and no handler, respectively). So, if you have a URL, you should be able to write:


     UIApplication.shared.open (url)


to replace your old code.


In Obj-C, the code is similar, except that there are no automatic defaults, so:


     [UIApplication.shared openURL: url, options: @{ }, completionHandler: nil);


Note that the Swift 4 name drops the "URL" suffix in the conversion from the Obj-C where the API is declared.


If that doesn't answer your question, can you show the line of code that you were using?

Quincy, thanks for taking the time to reply to me.


I have seen this code and information in the apple documentation and on posts online. For some reason, I cannot seem to use the code properly. I don't know why I am struggling with this. The application I built is much more complicated than this code, yet I just can't get it right.


When I posted, I indicated I was hoping someone would use the url for apple.com and post an example. I wanted to just copy, paste and overwrite with my own url and move on. At this point, I am taking out the buttons.

Quincey, I don't expect you to read my mind and I am proficient at copy and paste. I'm 50/50 on putting a smiley emoji here.


I'm using swift with a little objective C. The deprecated openURL was in swift. As I keep mentioning, I was looking for a complete code example with a URL. So with yours, something like this: UIApplication.shared.open ("http://www.apple.com") or UIApplication.shared.open (http://www.apple.com) or whatever actually works without an error.


As I mentioned in my previous post, I've removed the need for it from this app. I'm sure I'll eventually figure it out, but for now it's not a big deal. It's been bugging me and I want to move on.


Have a great day!

Yes, I was joking about that part, so here's the smiley. 🙂


>> something like this: UIApplication.shared.open ("http://www.apple.com")


Sorry, I assumed you already had the necessary URL. In the simplest case, when you have a pre-determined, absolute URL string such as the one above, you can construct the URL straightforwardly in Swift:


     let url = URL (string: "http://www.apple.com")!
     UIApplication.shared.open (url)


The longer, safer way to construct the URL is to use URLComponents:


     let urlComponents = URLComponents (string: "http://www.apple.com")!
     UIApplication.shared.open (urlComponents.url!)


(It's safer because the syntax of the string is checked properly. The first technique accepts any random string you give it.)


Do you get an error if you use one of these techniques? If so, what error?

Thanks, Quincey.


I tried both, which I think may have even been the same as some of my prior attempts this week. No warnings or errors, everything builds great. The button can be tapped, but nothing happens. I ran it on both the simulator and my iphone, just in case the simulator was being weird. This is part of what confounds me so much.


I've wondered if there is some kind of new build item setting that I have to set to open a URL in the app. I've searched on this, but haven't found anything. I've also tried both http and https, but neither works.

Hmm, did you try using the "canOpenURL" method to see what that says about it? If no joy there, maybe Quinn will jump in when he's around later and offer some suggestions about what problems to look for. (I take it that we've established that the problem isn't the code itself, but rather what the code does or doesn't do.)

Thx Quincey. I really appreciate your taking the time to help me. I wanted to try what you suggested, since you took the time to suggest it. I've hidden the buttons and commented out the code. I'm done with this for now. Consider my request for help closed.


Lisa

Lisa:


The normal way to 'close' a thread is by clicking where you see 'Correct Answer' - you can even do that with own of your own comments 😉


Ken

Post not yet marked as solved Up vote reply of KMT Down vote reply of KMT

Hi Quincy. I know this is an ancient thread, but I'm trying to do somerthing similar and my initial call to open a URL works for the intial URL, but 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 and its like making a call to an external function and handing over control and not returning to the calling function. Do you know of an alternate method to call multiple URLs in succession?