Help Linking Button to Website

I am trying to develop an App for the School I work at. I am trying to set up several buttons that send users to a specific webpage.


Here's my code for one of the buttons... It doesn't link to the webpage. In the simulator it sends me to the home screen, or sometimes it sends me back into xcode... (I think because it just shuts the simulator down? ) Please help.



import UIKit

class ViewController: UIViewController {

@IBAction func Registrar(sender: AnyObject) {

if let url = NSURL(string: "http://www.ajusd.org") {

UIApplication.shared.open(url as URL, options:[:], completionHandler:nil)

}

}

override func viewDidLoad() {

super.viewDidLoad()

/

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}

Replies

Did you try with https ?


Otherwise, you have to declare in info.plist:

App Transport Security Settings // New entry

Allow Arbitrary Loads : YES

Did you try with https ?

That should matter in this case. When you open a URL using UIApplication, the URL opens in the target app (in this case Safari) and thus your ATS settings don’t matter.

@sarahbrooke25, I put your code into a test app here in my office and it works just fine, both on the simulator and on a real device. I’ve not idea what’s going on with your app. I recommend you try reproducing this in a simple test app:

  1. Create a new app from the Single View Application template

  2. Add your method to

    ViewController.swift
  3. Add a button to

    Main.storyboard
    and wire it up to your method
  4. Run on the simulator

Does that change the behaviour?

Share and Enjoy

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

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