【问题标题】:Open native twitter app with hash tag search使用哈希标签搜索打开本机 Twitter 应用程序
【发布时间】:2012-06-14 10:59:17
【问题描述】:

在 iOS 5 上,我试图打开一个带有动态标签搜索词的本地 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];
}

}

它似乎进入了标签搜索页面,但永远加载... twitter://search?q=%@ 格式错误吗?

【问题讨论】:

    标签: objective-c ios twitter nsurl hashtag


    【解决方案1】:
    - (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];
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      twitter://search?query=hashtag
      

      【讨论】:

        【解决方案3】:

        好吧,经过一番摸索,我意识到哈希标签的搜索查询不应该包含实际的哈希标签......因此通过添加行

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

        现在可以正常工作了……谜团解开了。

        【讨论】:

          【解决方案4】:

          斯威夫特 3.0+

          更新上述代码的 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)
                      }
                  }
          

          同时在 info.plist

          中添加权限
          <key>LSApplicationQueriesSchemes</key>
          <array>
              <string>twitter</string>
          </array>
          

          【讨论】:

            猜你喜欢
            • 2012-09-08
            • 2014-03-04
            • 2022-12-17
            • 1970-01-01
            • 1970-01-01
            • 2014-08-13
            • 2014-04-07
            相关资源
            最近更新 更多