Validate if the data is null

2

How can I validate if a URL in my database is NULL and if so, show an alert but if it has a URL, open it

func Facebook(sender: AnyObject) {
    if let url = NSURL(string: face){
        let alert = UIAlertView(title: "Aviso!!!", message: "Mmmm, no     tengo url!", delegate: nil,
                                cancelButtonTitle: "OK")
        alert.show()
        UIApplication.sharedApplication().openURL(url)
    }
}

This way I get the data

values = ((try! NSJSONSerialization.JSONObjectWithData(data!,  options: .AllowFragments)) as? NSArray)!<br/>

face = values[indexPath.row]["facebook"] as! String
    
asked by Brandon Josué Cerezo 17.08.2016 в 20:51
source

2 answers

1

your code would look like this:

>         func Facebook(sender: AnyObject) {
          var url= NSURL(string: face)
>         if url != nil{ 
>             UIApplication.sharedApplication().openURL(url)
>         }else{
            let alert = UIAlertView(title: "Aviso!!!", message: "Mmmm, no tengo url!", delegate: nil, cancelButtonTitle: "OK")
            alert.show()

>     }

answered by 17.08.2016 в 23:32
1

With this you can call a url, if you can not create the NSURL it does not run and it launches a message

func CallFacebook(face: String) {
    if let url = NSURL(string: face){
        UIApplication.sharedApplication().openURL(url)
    }else{
        let alert = UIAlertView(title: "Aviso!!!", message: "Mmmm, no tengo url!", delegate: nil,
                                cancelButtonTitle: "OK")
        alert.show()
    }
}
    
answered by 18.08.2016 в 05:16