Search code examples
objective-ciostwitternsurlhashtag

Open native twitter app with hash tag search


On iOS 5, I'm trying to open a native twitter app with a dynamic hashtag search term.

I've tried:

- (void)openForHashTag:(NSString *)hashTag {
UIApplication *app = [UIApplication sharedApplication];

NSURL *twitterURL = [NSURL URLWithString:[NSString stringWithFormat:@"twitter://search?q=%@", hashTag]];
DLog(@"Hashtag URL: %@ ", twitterURL);

if ([app canOpenURL:twitterURL]) {
    [app openURL:twitterURL];
}
else {
    NSURL *safariURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://mobile.twitter.com/search?q=%@", hashTag]];
    [app openURL:safariURL];
}

}

It seems to go to the hashtag search page, but spends forever loading... Is twitter://search?q=%@ the wrong format?


Solution

  • Well, after a lot of head scratching, I realised that the search query for a hash tag should not contain an actual hash tag... Therefore by adding the line

     NSString *cleanString = [hashTag stringByReplacingOccurrencesOfString:@"#" withString:@""];
    

    it now works fine... Mystery solved.