简体   繁体   English

使用哈希标记搜索打开本机Twitter应用

[英]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. 在iOS 5上,我正在尝试使用动态主题标签搜索术语打开本机Twitter应用。

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? 它似乎进入了标签搜索页面,但是花费了永远的加载...是twitter://search?q=%@格式错误?

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

打开Twitter应用程序和搜索主题标签的正确URL是

twitter://search?query=hashtag

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. 它现在工作正常......神秘解决了。

Swift 3.0+ Swift 3.0+

Updating swift version of above code 更新上面代码的swift版本

       private func openTwitterHashTag() {

           // self.viewModel.programInfo.twitterHash = "#tag"

            guard let hashTagQuery = self.viewModel.programInfo.twitterHash.addingPercentEncoding(
                withAllowedCharacters: .urlHostAllowed
            ) else { return }

            // CASE: OPEN IN NATIVE TWITTER APP
            if let twitterUrl = URL(string: "twitter://search?query=\(hashTagQuery)"),
                UIApplication.shared.canOpenURL(twitterUrl) {
                UIApplication.shared.open(twitterUrl, options: [:], completionHandler: nil)
                return
            }

            // CASE: OPEN ON SAFARI VIEW CONTROLLER
            if let twitterWebUrl = URL(string: "http://mobile.twitter.com/search?q=\(hashTagQuery)") {
                let svc = SFSafariViewController.init(url: twitterWebUrl)
                self.present(svc, animated: true, completion: nil)
            }
        }

Also Add permission in info.plist 另外在info.plist中添加权限

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>twitter</string>
</array>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM