0

我正在使用 IFTweetLabel 并指出它可以识别链接,但我在使用 IFTweetLabel 创建的按钮打开 web 视图时遇到了可怕的时间。我正在运行 NSLog 并且可以清楚地看到它在按下按钮时正在理解每个链接,但由于某种原因它不会打开 URL。

下面是我用来显示视图并在 webView 中加载字符串的代码......除了加载 webview 之外,所有这些都有效。

任何建议将不胜感激!谢谢你!

- (void)handleTweetNotification:(NSNotification *)notification

{
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:1.0];

CGRect viewFrame = [MainwebView frame];
viewFrame.origin.x = 220;
MainwebView.frame = viewFrame;        

MainwebView.alpha = 1.0; 
web.alpha = 1.0;

MainwebView.layer.shadowColor = [[UIColor blackColor] CGColor];
MainwebView.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
MainwebView.layer.shadowRadius = 8.0f;
MainwebView.layer.shadowOpacity = 1.0f;     



[self.view addSubview:MainwebView];
[UIView commitAnimations];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:tweetLabel.text]]]; 

NSLog(@"handleTweetNotification: WTF notification = %@", notification);
}
4

2 回答 2

1

用户点击的字符串被传递给这个方法,并且可以通过使用 [通知对象] 找到。您在代码中所做的是将 tweetLabel 中的所有文本传递给 webview。由于这不是 URL,因此您的应用程序将崩溃。改用这个: [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[notification object]]]];

您也可以先 NSLog [通知对象] 以确保它是一个 URL。

于 2011-08-20T08:20:33.687 回答
1

这是我的代码,应该可以正常工作,但也应该清理一些:

    - (void)handleTweetNotification:(NSNotification *)notification {    

    unichar theChar = [(NSString *)notification.object characterAtIndex:0];
    NSString *theString = (NSString *)notification.object;


    if ( [[NSString stringWithCharacters:&theChar length:1] isEqualToString:@"#"]) {
        DLog(@"This is a hashtag");
        theString = [theString stringByReplacingOccurrencesOfString:@"#" withString:@"%23"];
        NSURL *hashtagURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/#!/search?q=%@", theString]];
        WebViewController *theWebVC = [[WebViewController alloc] init];
        theWebVC.request = [NSURLRequest requestWithURL:hashtagURL];
        UINavigationController *theNavigationVC = [[UINavigationController alloc] initWithRootViewController:theWebVC];
        [self presentModalViewController:theNavigationVC animated:YES];

    }

    if ( [[NSString stringWithCharacters:&theChar length:1] isEqualToString:@"@"]) {
        DLog(@"This is a Mention");
        theString = [theString stringByReplacingOccurrencesOfString:@"@" withString:@""];
        NSURL *mentionURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/%@", theString]];
        WebViewController *theWebVC = [[WebViewController alloc] init];
        theWebVC.request = [NSURLRequest requestWithURL:mentionURL];
        UINavigationController *theNavigationVC = [[UINavigationController alloc] initWithRootViewController:theWebVC];
        [self presentModalViewController:theNavigationVC animated:YES];

    }
    if ( [[NSString stringWithCharacters:&theChar length:1] isEqualToString:@"h"]) {
        DLog(@"This is a hyperlink");
        theString = [[theString componentsSeparatedByString: @"\n"] objectAtIndex:0];
        NSURL *hyperlinkURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", theString]];
        WebViewController *theWebVC = [[WebViewController alloc] init];
        theWebVC.request = [NSURLRequest requestWithURL:hyperlinkURL];
        UINavigationController *theNavigationVC = [[UINavigationController alloc] initWithRootViewController:theWebVC];
        [self presentModalViewController:theNavigationVC animated:YES];
    }
}
于 2012-02-13T21:24:55.563 回答