코드를 실행하려고 할 때마다 항상 다음과 같은 오류가 표시됩니다.
치명적 오류 : 선택적 값을 언 래핑하는 동안 예기치 않게 nil이 발견되었습니다.
firstAddress
가치는 450 Serra Mall, Stanford, CA 94305, United States
여기에 코드
@IBAction func locationOneTapped(sender: UIButton) {
let testURL: NSURL = NSURL(string: "comgooglemaps-x-callback://")!
if UIApplication.sharedApplication().canOpenURL(testURL) {
if let address = firstAddress {
let directionsRequest: String = "comgooglemaps-x-callback://" + "?daddr=\(address)" + "&x-success=sourceapp://?resume=true&x-source=AirApp"
let directionsURL: NSURL = NSURL(string: directionsRequest)!
UIApplication.sharedApplication().openURL(directionsURL)
}
}
else {
NSLog("Can't use comgooglemaps-x-callback:// on this device.")
}
유효한 NSURL을 만들려면 NSURL의 문자열을 인코딩해야합니다. 이 답변 을 바탕으로 다음과 같이해야합니다.
let testURL: NSURL = NSURL(string: "comgooglemaps-x-callback://")!
if UIApplication.sharedApplication().canOpenURL(testURL) {
if let address = firstAddress?.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) {
let directionsRequest: String = "comgooglemaps-x-callback://" + "?daddr=\(address)" + "&x-success=sourceapp://?resume=true&x-source=AirApp"
let directionsURL: NSURL = NSURL(string: directionsRequest)!
UIApplication.sharedApplication().openURL(directionsURL)
}
}
else {
NSLog("Can't use comgooglemaps-x-callback:// on this device.")
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다