2012-06-11 23 views
2

在iOS 5上,我嘗試使用動態哈希標籤搜索詞打開本機twitter應用程序。使用散列標籤搜索打開本機twitter應用程序

我已經試過:

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

}

似乎去包括hashtag搜索頁面,但花費永遠載入中...是twitter://search?q=%@格式錯誤?

回答

1

好,很多撓頭的後,我意識到,對於一個散列標籤的搜索查詢不應該包含實際的哈希標籤......因此,通過將行

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

現在工作得很好。 .. 謎團已揭開。

2

用於打開Twitter的應用程序和搜索主題標籤的正確網址是

twitter://search?query=hashtag 
3
- (void)openForHashTag:(NSString *)hashTag { 
    UIApplication *app = [UIApplication sharedApplication]; 

    // NOTE: you must percent escape the query (# becomes %23) 
    NSString *cleanQuery = [hashTag stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *twitterURL = [NSURL URLWithString:[NSString stringWithFormat:@"twitter://search?query=%@", cleanQuery]]; 
    if ([app canOpenURL:twitterURL]) { 
     [app openURL:twitterURL]; 
    } else { 
     NSURL *safariURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://mobile.twitter.com/search?q=%@", cleanQuery]]; 
     [app openURL:safariURL]; 
    } 
} 
+1

這是正確的答案 – capikaw