如何在iPhone应用程序中添加超链接?

Par*_*att 7 iphone cocoa-touch objective-c hyperlink ios4

在我的iPhone应用程序中,我需要显示网站的超链接

如何在iPhone编程中为网站添加超链接?

其实我想通过Facebook API将我的应用程序链接传递到Facebook.

那么我应该如何格式化我的文本,使其在Facebook上作为超链接?

我仍然坚持这个问题.

Jul*_*ien 12

这取决于您想要放置此链接的位置.如果它在UITextView中,您只需启用它.

textView.text = @"Some text with link in it : http://http://stackoverflow.com";
textView.dataDetectorTypes = UIDataDetectorTypeLink;
Run Code Online (Sandbox Code Playgroud)

有关iOS参考库的更多信息.

或者,如果要以编程方式打开Safari:

NSURL *url = [ [ NSURL alloc ] initWithString: @"http://stackoverflow.com" ];
[[UIApplication sharedApplication] openURL:url];
[url release];
Run Code Online (Sandbox Code Playgroud)

如果您想在Facebook上分享,您需要告诉Safari打开一个URL,该URL将显示允许用户共享的Facebook页面.这是一个例子:

NSString *urlString = @"http://stackoverflow.com";
//The url you want to share

NSString *title = "The Title of the Page";
//The title you want to be displayed on Facebook

NSString *shareUrlString = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?u=%@&t=%@", urlString , title];
//Create the URL string which will tell Facebook you want to share that specific page 

NSURL *url = [ [ NSURL alloc ] initWithString:shareUrlString ];
//Create the URL object 

[[UIApplication sharedApplication] openURL:url];
//Launch Safari with the URL you created

[url release];
//Release the object if you don't need it
Run Code Online (Sandbox Code Playgroud)