airsoftFreak airsoftFreak - 5 months ago 20
Objective-C Question

How do i convert this Obj-c codes to swift?

I have no idea how objective-c syntax works, and now Im struggling to convert these codes to swift

NSURL *testURL = [NSURL URLWithString:@"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL]) {
NSString *directionsRequest = @"comgooglemaps-x-callback://" +
@"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" +
@"&x-success=sourceapp://?resume=true&x-source=AirApp";
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
} else {
NSLog(@"Can't use comgooglemaps-x-callback:// on this device.");
}

Answer
let testURL: NSURL = NSURL(string: "comgooglemaps-x-callback://")!
    if UIApplication.sharedApplication().canOpenURL(testURL) {
        let directionsRequest: String = "comgooglemaps-x-callback://" + "?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" + "&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.")
    }