如何将NSString格式化为URL?

时间:2014-10-13 13:17:03

标签: ios objective-c nsstring

我使用以下字符串来格式化我的应用程序中的Facebook帖子中的URL。

NSString *stringAppURLFormatted = [NSString stringWithFormat:@"<a href= 'https://www.facebook.com'> New Game Name </a>"];

输出

<a href= 'https://www.facebook.com'> New Game Name </a>

虽然我期待输出

New Game Name

如何纠正?

已更新

基本上,我使用下面的代码,将NSString添加到NSMutableDictionary。我不太确定如何应用已被广泛推荐用于此方法的UIActivityViewController方法。

-(void)postPictureWithText:(UIImage*)picture withTitle:(NSString*) title andDescription (NSString*)description
{
    NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
    //NSDictionary* paramsImmutable = params.copy;

    NSString *stringAppURLFormatted = [NSString stringWithFormat:@"<a href= 'https://www.facebook.com'> New Game Name </a>"];
    [params setObject:stringAppURLFormatted forKey:@"message"];

    [params setObject:UIImagePNGRepresentation(picture) forKey:@"picture"];
    [FBRequestConnection startWithGraphPath:@"me/photos"
                             parameters:params
                             HTTPMethod:@"POST"
                      completionHandler:^(FBRequestConnection *connection,
                                          id result,
                                          NSError *error)
     {
         if (error)
         {
             //failure
             NSLog(@"ERROR in posting to facebook");
         }
         else
         {
             //success
             NSLog(@"Result after posting image successfully = %@",result);
         }
         // button.enabled = YES;
     }];
   }

1 个答案:

答案 0 :(得分:0)

您能否提供有关如何将该内容发布到Facebook的详细信息? 如果您正在使用UIActivityViewController实例,那么在NSHipster: UIActivity​View​Controller上有一篇关于它的好文章

将您的网址添加为活动项目的技巧。 Facebook(或推特)会自动将其解释为附加链接。也许想要这样的东西:

NSURL *linkURL = [NSURL URLWithString:@"https://www.facebook.com"];
NSString *linkDescription = @"New Game Name";
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[linkDescription, linkURL] applicationActivities:nil];
[navigationController presentViewController:activityViewController animated:YES completion:^{
    // ...
}];

P.S。:stackoverflow

在这里围绕这个主题进行了很好的讨论。