0

私はIFTweetLabelを使用していて、リンクを認識できるようになっていますが、IFTweetLabelが作成するボタンでWebビューを開くのにひどい時間を過ごしています。NSLogを実行していると、ボタンが押されたときに各リンクを理解していることがはっきりとわかりますが、何らかの理由でURLが開かれません。

以下は、ビューを表示し、webViewに文字列をロードするために使用しているコードです。これはすべて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);
}
4

2 に答える 2

1

ユーザーがクリックした文字列はこのメソッドに渡され、[通知オブジェクト]を使用して見つけることができます。コードで行っているのは、tweetLabel内のすべてのテキストをWebビューに渡すことです。これは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 に答える