【发布时间】:2011-05-23 09:23:37
【问题描述】:
在我的 iPhone 应用程序中,我需要显示指向网站的超链接
如何在 iPhone 编程中为网站添加超链接?
实际上我想使用 facebook API 将链接从我的应用程序传递到 facebook。
那么我应该如何格式化我的文本,使其在 facebook 上用作超链接?
我仍然被这个问题困扰。
【问题讨论】:
标签: iphone objective-c cocoa-touch ios4 hyperlink
在我的 iPhone 应用程序中,我需要显示指向网站的超链接
如何在 iPhone 编程中为网站添加超链接?
实际上我想使用 facebook API 将链接从我的应用程序传递到 facebook。
那么我应该如何格式化我的文本,使其在 facebook 上用作超链接?
我仍然被这个问题困扰。
【问题讨论】:
标签: iphone objective-c cocoa-touch ios4 hyperlink
这取决于你想把这个链接放在哪里。 如果它在 UITextView 中,您只需启用它即可。
textView.text = @"Some text with link in it : http://http://stackoverflow.com";
textView.dataDetectorTypes = UIDataDetectorTypeLink;
更多关于iOS reference library的信息。
或者,如果您想以编程方式打开 Safari:
NSURL *url = [ [ NSURL alloc ] initWithString: @"http://stackoverflow.com" ];
[[UIApplication sharedApplication] openURL:url];
[url release];
如果您想在 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
【讨论】:
在 View Controller 中拖动一个 textView 并选择它的属性检查器:
运行您的项目。您可以在 View Controller 中看到链接。点击链接,将带您进入 safari 并在其上打开一个新标签。
【讨论】:
部分
你能提到你需要在哪里插入hyperlink,这是我在iPhone应用程序中插入link的方法:
NSMutableString *err=[[NSMutableString alloc] init];
[err appendString:@"<html><body style=\"background-color:black\" ><font size=3 face=\"helvetica\" color=\"white\" align=\"center\"><H3>Login Error</H3><p> "];
[err appendString:@"Please try again. If you forgot your password, you can retrieve it at<a href=\"ENTER YOUR LINK URL\"><font color=\"red\"> ENTER LINK TEXT</font></a>.</font></p></body></html>"];
}
【讨论】:
我最喜欢的共享库是 ShareKit。在您的应用程序中进行设置非常容易,并且允许人们通过 Facebook 和其他服务进行分享。它也是可定制的,因此您可以为其设置外观、更改颜色以及控制您希望用户与之共享的服务。
退房,http://www.getsharekit.com/
将其添加到您的应用后,您可以通过创建一个按钮非常轻松地集成它
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(share)];
//add the button to a navigation bar or tool bar.
然后在您的控制器中创建共享方法。
- (void)share
{
// Create the item to share (in this example, a url)
NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];
// Get the ShareKit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the action sheet
[actionSheet showFromToolbar:navigationController.toolbar];
}
【讨论】:
扩展 NSAttributedString{
添加一个新的 swift 文件用于在 ios 中添加链接
static func makeHyperlink(for path: String, in string: String, as substring: String) -> NSAttributedString {
let nsString = NSString(string: string)
let substringRange = nsString.range(of: substring)
let attributedString = NSMutableAttributedString(string: string)
attributedString.addAttribute(.link, value: path, range: substringRange)
return attributedString
}
}
然后在你的文本视图函数中使用这个 makehyperlink 函数。
let attributesString = NSAttributedString.makeHyperlink(for: link, in: paragraph, as: "text to use for link")
【讨论】: