Xcode - opening urls in safari browser


(chris1) #1

I am building an app using Hype/Xcode. I currently have a scene which has 4 buttons in it which I wish to open the external Safari browser and load the respective website pages. Is there a simple way to make a button/link open in the Safari browser in iOS? There is an article by Daniel which points to references about how to do this within cordova/phonegap - but nothing about how to do it from within an Xcode project ( Xcode & Phonegap Tips ).


(Mark Hunte) #2

Here is an example Xcode project that uses a Hype project to do this.

The Hype buttons run a javascript which posts messages to the iOS app.

The iOS app is setup to listen for the posted messages and runs a callback handler when it gets them.

The callback handler checks which message was sent and runs a function relating to it.

In this example I used one message post from Hype.
The Hype JS uses the buttons ids to determine what the message posted (message body) will be. ( Different URLS )

The message body is then converted to a swift URL object by the iOS application and opened in Safari.

I have also added a badly constructed URL in one button just to show that the URLS coming from the JS can crash the app if the are not constructed properly.

There is swift code that looks out for this. And also shows how to control the hype document from the App.

hypeXCodeCall2.zip (382.6 KB)


(chris1) #3

Thanks so much, Mark. This is awesome! If you were in Sydney I’d buy you dinner!


(Mark Hunte) #4

On my way… :grin: