简体   繁体   中英

blank screen appears when click on UILabel in swift

i'm trying to open iPhone call screen with selected number when click on UIlabel which is hyperlink in swift.

a blank screen appears for 1 second and goes. No iPhone call screen appears. Don't know what is wrong in my code.

Code in viewDidLoad

let strPhone = "080 2854 2105"

let attributedPhoneStr = NSMutableAttributedString(string:strPhone, attributes:[NSLinkAttributeName: NSURL(string: "http://aerospace.honeywell.com")!])

lblPhoneContact.attributedText = attributedPhoneStr

lblPhoneContact.userInteractionEnabled = true

let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed"))

lblPhoneContact.addGestureRecognizer(gestureRecognizer)

Function labelPressed

func labelPressed() {      

let url:NSURL = NSURL(string: "tel://08028542105")!

UIApplication.sharedApplication().openURL(url)

}

what is wrong or missing in my code. I tried running on my iPhone device.

for swift use

let url:NSURL = NSURL(string: "telprompt:08028542105")!

instead of

let url:NSURL = NSURL(string: "tel://08028542105")!

You can try to use the GCD's dispatch_async to invoke the "openURL" method:

dispatch_async(dispatch_get_main_queue()) { () -> Void in
    UIApplication.sharedApplication().openURL(url)
}

您应该从uri中省略//,即

let url:NSURL = NSURL(string: "tel:08028542105")!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM