IFTweetLabel RegexKitLite打开UIWebView

时间:2011-07-08 05:01:52

标签: iphone uiwebview hyperlink regexkitlite

我正在使用IFTweetLabel,并且知道链接,但我正在用IFTweetLabel创建的按钮打开webview。我正在运行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);
}

2 个答案:

答案 0 :(得分:1)

用户单击的字符串将传递给此方法,可以使用[notification object]找到。您在代码中所做的是将tweetLabel中的所有文本传递给webview。由于这不是网址,因此您的应用会崩溃。请改用: [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[notification object]]]];

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

答案 1 :(得分: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];
    }
}