我试图用对象c打开一个URL,我的代码如下
#include <stdio.h>
int main()
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.ithinksw.com/"]];
return 0;
}
但是当我尝试编译它时,出现这些错误
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
即时通讯这是个新手,所以任何帮助都会很棒的:)
最佳答案
似乎您定位到iPhone,因此必须使用[[UIApplication sharedApplication] openURL]而不是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
并且不要忘记添加Foundation框架以包含和链接部分。
关于objective-c - 简单的Objective-C脚本打开一个URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8679423/