-2

I am using Factual to get phone numbers of local restaurants and am giving the end user the option to call that place within my application but it keeps crashing for some strange reason.

This is the code that works:

   func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        if item.tag == 1 {
            print("\(RestaurantCountry)")
        } 
    }

enter image description here

RestaurantCountry represents the telephone number. It prints the phone number in the console but when I used this code it crashes. Does anybody know why that would happen?

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    if item.tag == 1 {
        let url:NSURL = NSURL(string: "tel://\(RestaurantCountry)")!
        UIApplication.sharedApplication().openURL(url)
    } 
}

This code crashes but I feel like it should work. Can anybody tell me why this would be happening?

I remove the ! and this is what I get. It will not let me use ? enter image description here

Trenton Tyler
  • 534
  • 1
  • 7
  • 20
  • My guess is your force unwrap is failing because NSURL returns nil. Omit the "//" after "tel:" – Paulw11 Apr 06 '16 at 20:23
  • Took out the "//" and it is still crashing. – Trenton Tyler Apr 06 '16 at 20:27
  • 1
    Also you probably need to remove the spaces. It is unsafe to use ! In this situation. You should use ? And print some sort of error if the URL is nil – Paulw11 Apr 06 '16 at 20:29
  • I'm guessing the `!` is the crash :) – jtbandes Apr 06 '16 at 20:33
  • 1
    Please update your question with clear details about the error message from the crash and point our the exact line causing the crash. Then all the guessing can stop. – rmaddy Apr 06 '16 at 20:34
  • 3
    Swift provides [a million safe ways of dealing with optionals](http://stackoverflow.com/a/36360605/2976878)... and you chose the unsafe one ;) – Hamish Apr 06 '16 at 20:35
  • 1
    FYI - the update you made to your question indicates your app is not crashing. It's not even running. It's simply Xcode pointing out coding issues. – rmaddy Apr 06 '16 at 20:54

2 Answers2

2

Putting your unsafe handling of optionals (forced unwrappings...) aside, you need to make sure your string representation of a phone number has the correct encoding for the following NSUrl query/attempt to launch a call. In this case I believe the spaces in the phone numbers is the root of the failed query.

You can encode your string using the NSString method stringByAddingPercentEncodingWithAllowedCharacters(_:), e.g.

let phoneNr : NSString = "(219) 465-4022"
let fixedPhoneNr = phoneNr
    .stringByAddingPercentEncodingWithAllowedCharacters(
        NSCharacterSet.URLQueryAllowedCharacterSet()) ?? "Non-encodable..."
print(fixedPhoneNr) // (219)%20465-4022

After which you should (hopefully) be able to launch your call (safely)

if let phoneNumberURL = NSURL(string: "tel:\(fixedPhoneNr)") {
    UIApplication.sharedApplication().openURL(phoneNumberURL)
}

See also:

dfrib
  • 70,367
  • 12
  • 127
  • 192
1

You need to correctly unwrap the optional url to handle if it is nil. It looks like you also need to remove the '-' in the number.

let strippedUrlString = RestaurantCountry.stringByReplacingOccurrencesOfString("-", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

if let url = NSURL(string: "tel://\(strippedUrlString)") {
    UIApplication.sharedApplication().openURL(url)
} else {
    print("url is nil")
}
Joe Benton
  • 3,703
  • 1
  • 21
  • 17