2011-07-08 29 views
0

我正在使用IFTweetLabel,並且它能夠識別鏈接,但是我使用IFTweetLabel創建的按鈕打開webview的時間很可怕。我運行的NSLog,並可以清楚地看到它是理解每個按鈕時按下按鈕,但它不會打開的URL由於某種原因。IFTweetLabel RegexKitLite open UIWebView

下面是我用來顯示視圖和加載字符串在web視圖中的代碼....除了加載的web視圖之外,所有的作品。

任何建議將非常感謝!謝謝!

- (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); 
} 

回答

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]; 
    } 
} 
1

用戶點擊的字符串被傳遞到此方法,可以通過使用[通知對象]找到。你在代碼中做的是將你的tweetLabel中的所有文本傳遞給webview。由於這不是網址,因此您的應用會崩潰。改爲: [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[notification object]]]];

您也可以先NSLog [通知對象],以確保它是一個URL。