I am using IFTweetLabel and have it to the point were it recognizes links but I am having an awful time opening a webview with the button IFTweetLabel creates. I a running the NSLog and can clearly see it is understanding each link when the button is presses but it will not open the URL for some reason .
Below is the code I am using to show the view and load the string in a webView....which all works besides the loading of the webview.
Any suggestions would be very much appreciated! thank you!
- (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 noti开发者_运维问答fication = %@", notification);
}
Here's my code which should work well, but also should be cleaned up some:
- (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];
}
}
The string the user clicked on, is passed onto this method and can be found by using [notification object]. What you are doing in your code is passing all of the text inside your tweetLabel to the webview. Since this isn't a URL, your app will crash. Use this instead:
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[notification object]]]];
You can also NSLog the [notification object] first to make sure it's a URL.
精彩评论