Lewis Denny
Lewis Denny

Reputation: 669

simple Objective-C script to open a url

im trying to open a url with object c my code is as follows

#include <stdio.h>
int main()
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.ithinksw.com/"]];
return 0;
}

but when i try to compile it i get these errors

hello.c: In function 'main':
hello.c:4: error: expected expression before '[' token
hello.c:4: error: 'NSWorkspace' undeclared (first use in this function)
hello.c:4: error: (Each undeclared identifier is reported only once
hello.c:4: error: for each function it appears in.)
hello.c:4: error: expected ']' before 'openURL'
hello.c:4: error: stray '@' in program

im extremal new to this so any help would be great :)

Upvotes: 1

Views: 4140

Answers (3)

4ndrew
4ndrew

Reputation: 16212

Seems you targetering to iphone, so you have to use [[UIApplication sharedApplication] openURL] instead of NSWorkspace.

#import <Foundation/Foundation.h>

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif

// ...

#if TARGET_OS_IPHONE
    [[UIApplication sharedApplication] openURL:url];
#elif TARGET_OS_MAC
    [[NSWorkspace sharedWorkspace] openURL:url];
#endif

And don't forget to add Foundation framework to include and link section.

Upvotes: 4

Niels Castle
Niels Castle

Reputation: 8069

If your are new to iOS programming I highly recommend using some of Apples templates in xCode as a basis for your development.

NSWorkspace is a MacOS X construct. I don't think there is any iOS equivalent.

Take a look at the CP193P videos on iTunesU to get you started on iOS programming - happy programming!

Upvotes: 1

Ole Begemann
Ole Begemann

Reputation: 135548

You have to

#import <Foundation/Foundation.h>

Also, the file name should be hello.m so that the compiler knows it deals with Objective-C here.

And make sure to link your binary to the Foundation framework.

Upvotes: 2

Related Questions