Converting code from Objective-C to Swift

117 views Asked by At

I'm developing a new app in Swift and I want to hook up Instagram to my app. I found the iPhone hook up for Instagram but it's in objective C.

Can anyone help me in converting it to swift?

Here is the code:

NSURL *instagramURL = [NSURL URLWithString:@"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
    [[UIApplication sharedApplication] openURL:instagramURL];
}
2

There are 2 answers

0
Ashish Kakkad On

You have to do in swift like as follows

    var instagramURL : NSURL = NSURL(string: "instagram://location?id=1")!
    if (UIApplication.sharedApplication().canOpenURL(instagramURL)) {
        UIApplication.sharedApplication().openURL(instagramURL)
    }
0
Md. Ibrahim Hassan On

Swift 4.2

let instagramURL = URL(string: "instagram://location?id=1")
if let anURL = instagramURL {
    if UIApplication.shared.canOpenURL(anURL) {
         UIApplication.shared.openURL(anURL)
    }
}

Converted to Swift using Swiftify.me