【问题标题】:How to add hyperlink in iPhone app?如何在 iPhone 应用程序中添加超链接?
【发布时间】:2011-05-23 09:23:37
【问题描述】:

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

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

实际上我想使用 facebook API 将链接从我的应用程序传递到 facebook。

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

我仍然被这个问题困扰。

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios4 hyperlink


    【解决方案1】:

    这取决于你想把这个链接放在哪里。 如果它在 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
    

    【讨论】:

    • 实际上我想使用 facebook API 将链接从我的应用程序传递到 facebook。那么我应该如何格式化我的文本,使其在 facebook 上用作超链接?
    • 好吧,那不一样了。所以你有一个链接,你希望用户能够在他们的 Facebook 个人资料上分享?
    • 不是在回答你的问题吗?
    • @Julien:对不起,我忘了勾选你的答案作为答案。真的很抱歉:)
    • @PARTH 没问题 :) 3 个月的时间来确定答案是否正确:p
    【解决方案2】:

    在 View Controller 中拖动一个 textView 并选择它的属性检查器:

    1. 将您的文本设置为“我的网站:www.mywebsitelink.com”并确保 该文本类型应该是纯文本。
    2. 将您的行为勾选为可选择而不是可编辑。
    3. 勾选检测为链接。

    运行您的项目。您可以在 View Controller 中看到链接。点击链接,将带您进入 safari 并在其上打开一个新标签。

    【讨论】:

      【解决方案3】:

      部分

      你能提到你需要在哪里插入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>"];
          }
      

      【讨论】:

        【解决方案4】:

        我最喜欢的共享库是 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];
        }
        

        【讨论】:

          【解决方案5】:

          扩展 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")

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-22
            • 2013-11-07
            • 2015-06-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多