Twitter分享扩展文本超280字符时Tweet按钮默认启用问题
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
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).Use the activity view controller delegate to control button state
Implement theUIActivityViewControllerDelegatemethod 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
adjustedTweetCharacterCountproperty 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

