You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Twitter分享扩展文本超280字符时Tweet按钮默认启用问题

Fix: Disable Tweet Button When Share Text Exceeds 280 Characters

Got it, let's tackle this issue where the Twitter share button stays enabled even when your text goes over the 280-character limit. The default UIActivityViewController doesn't handle Twitter's specific character rules automatically, so we need to add custom logic to disable the button when the content is too long.

First, Understand Twitter's Character Counting Rule

A key thing to note: Twitter counts any URL as exactly 23 characters, no matter how long the actual URL is. So we need to account for this when calculating the total character count—otherwise our check will be inaccurate.

Step-by-Step Solution

  1. Calculate the adjusted character count
    We need to sum the length of your share text plus the standardized 23-character count for URLs (if you're including one).

  2. Use the activity view controller delegate to control button state
    Implement the UIActivityViewControllerDelegate method to disable the Twitter activity when the adjusted count exceeds 280.

Full Code Example

First, set up your activity view controller with the character count logic:

class YourViewController: UIViewController {
    // Store the adjusted count to access it in the delegate method
    private var adjustedTweetCharacterCount = 0
    
    func presentTwitterShare() {
        let shareText = delegate.shareText()
        let urlString = "your-target-url" // Replace with your actual URL
        
        // Start with the length of the share text
        adjustedTweetCharacterCount = shareText.count
        
        // Add 23 chars if we're including a URL (Twitter's standard URL count)
        if URL(string: urlString) != nil {
            adjustedTweetCharacterCount += 23
        }
        
        // Prepare activity items
        var activityItems: [Any] = [shareText]
        if let shareURL = URL(string: urlString) {
            activityItems.append(shareURL)
        }
        
        let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
        activityVC.delegate = self
        present(activityVC, animated: true)
    }
}

Then implement the delegate extension to disable the Twitter button when needed:

extension YourViewController: UIActivityViewControllerDelegate {
    func activityViewController(_ activityViewController: UIActivityViewController, activityType: UIActivity.ActivityType?, canPerformWithActivityItems activityItems: [Any]) -> Bool {
        // Check if we're dealing with the Twitter activity
        guard let activityType = activityType else { return true }
        
        if activityType == .postToTwitter {
            // Disable the button if total chars exceed 280
            return adjustedTweetCharacterCount <= 280
        }
        
        // Allow all other sharing activities to work normally
        return true
    }
}

Key Notes

  • Make sure your view controller conforms to UIActivityViewControllerDelegate (we do this via the extension above).
  • The adjustedTweetCharacterCount property is stored at the view controller level so the delegate method can access it easily.
  • This logic aligns with Twitter's official character rules, so it'll accurately reflect whether the tweet will be accepted. When the count is over 280, the Twitter button will be grayed out and unselectable—exactly the expected behavior.

内容的提问来源于stack exchange,提问作者Kishore Suthar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:37:29