如何在 iPhone 应用程序中添加超链接? [英] How to add hyperlink in iPhone app?

查看:85
本文介绍了如何在 iPhone 应用程序中添加超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

实际上,我想使用 facebook API 将我的应用程序中的链接传递到 facebook.

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

我仍然被这个问题困扰.

解决方案

这取决于你想把这个链接放在哪里.如果它在 UITextView 中,你只需要启用它.

textView.text = @"一些带有链接的文本:http://http://stackoverflow.com";textView.dataDetectorTypes = UIDataDetectorTypeLink;

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

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

NSURL *url = [ [NSURL alloc ] initWithString: @"http://stackoverflow.com" ];[[UIApplication sharedApplication] openURL:url];[网址发布];

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

NSString *urlString = @"http://stackoverflow.com";//你要分享的网址NSString *title = "页面标题";//你想在Facebook上显示的标题NSString *shareUrlString = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?u=%@&t=%@", urlString , title];//创建 URL 字符串,它会告诉 Facebook 您想分享该特定页面NSURL *url = [ [ NSURL alloc ] initWithString:shareUrlString ];//创建URL对象[[UIApplication sharedApplication] openURL:url];//使用您创建的 URL 启动 Safari[网址发布];//如果不需要就释放对象

In my iPhone app, I require to show the hyperlink to a website

How can I add hyperlink for a website in iPhone programming?.

Actually i want to pass the link from my app onto facebook using facebook API.

So how should I format my text such that it works as hyperlink on facebook?

I am still stuck with this issue.

解决方案

It depends on where you want to put this link. If it is in a UITextView, you just have to enable it.

textView.text = @"Some text with link in it : http://http://stackoverflow.com";
textView.dataDetectorTypes = UIDataDetectorTypeLink;

More info on iOS reference library.

Or, if you want to open Safari programmatically:

NSURL *url = [ [ NSURL alloc ] initWithString: @"http://stackoverflow.com" ];
[[UIApplication sharedApplication] openURL:url];
[url release];

If you want to share on Facebook, you need to tell Safari to open a URL which will display a Facebook page that allows the user to share. Here is an example:

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

这篇关于如何在 iPhone 应用程序中添加超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆