简体   繁体   English

简单的Objective-C脚本打开一个URL

[英]simple Objective-C script to open a url

im trying to open a url with object c my code is as follows 我试图用对象c打开一个URL,我的代码如下

#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 :) 即时通讯这是个新手,所以任何帮助都会很棒:)

Seems you targetering to iphone, so you have to use [[UIApplication sharedApplication] openURL] instead of NSWorkspace. 似乎您定位到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

And don't forget to add Foundation framework to include and link section. 并且不要忘记添加Foundation框架以包含和链接部分。

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. 另外,文件名应为hello.m以便编译器在此处知道它处理的是Objective-C。

And make sure to link your binary to the Foundation framework. 并确保将您的二进制文件链接到Foundation框架。

If your are new to iOS programming I highly recommend using some of Apples templates in xCode as a basis for your development. 如果您是iOS编程的新手,我强烈建议您在xCode中使用一些Apple模板作为您开发的基础。

NSWorkspace is a MacOS X construct. NSWorkspace是MacOS X的构造。 I don't think there is any iOS equivalent. 我认为没有任何iOS等效产品。

Take a look at the CP193P videos on iTunesU to get you started on iOS programming - happy programming! 观看iTunesU上的CP193P视频,开始使用iOS编程-编程愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM