D.W. D.W. - 4 years ago 246
iOS Question

NSURL Error in XCode 8.2.1

BEGINNER ALERT! Please talk to me like I'm a 5-year-old because I am new to this platform and language. I will be grateful and unoffended.

I have the following code in my Xcode project to open a website in the native browser from a button on the app home page:

@IBAction func faceURL(_ sender: Any) {
NSURL *spotiURL = [NSURL URLWithString:@"https://www.facebook.com"];

[[UIApplication sharedApplication] openURL:faceURL options:@{} completionHandler:^(BOOL success) {
if (success){
NSLog(@"Opened url");
}
}];
}


On the line:

NSURL *spotiURL = [NSURL URLWithString:@"https://www.facebook.com"];


it's throwing the following error:


Expected "," separator Consecutive statements on a line must be separated by ";"


It recommends putting a
;
between the colon and
@
symbol before the URL. I know some of this is deprecated with the new Xcode and Swift format, but I can't find a straightforward answer for this particular issue. Thank you in advance for your wisdom.

Answer Source

It looks like you're trying to use Objective C code in a Swift method. If your project is written in Swift then this is (probably) the code you need:

@IBAction func faceURL(_ sender: Any) {

    if let facebookURL = URL(string: "https://www.facebook.com")
    {
        UIApplication.shared.openURL(facebookURL) // the open method you were using doesn't exist in Swift
    }
}

I'd suggest you work through a few tutorials to learn the basics of iOS development.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download