Files
youle_app_ios/msext/Class/Utils/PasteboardManager.m
joywayer 93b1881e52 feat: 优化分享功能和剪贴板管理
- 修改gameController分享逻辑,支持不同分享类型
- 新增PasteboardManager类,优化iOS 14+剪贴板访问
- 减少剪贴板权限弹窗,提升用户体验
2025-06-17 20:53:07 +08:00

72 lines
2.3 KiB
Objective-C
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// PasteboardManager.m
// msext
//
// Created on 2025/06/17.
// Copyright © 2025年. All rights reserved.
//
#import "PasteboardManager.h"
@implementation PasteboardManager
#pragma mark - Public Methods
+ (nullable NSString *)safeReadPasteboardText {
NSLog(@"🔍 [PasteboardManager] 尝试安全读取剪贴板内容");
if (@available(iOS 14.0, *)) {
// iOS 14+ 先检查是否有文本内容,避免不必要的权限弹窗
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
if (pasteboard.hasStrings) {
NSString *text = pasteboard.string;
NSLog(@"✅ [PasteboardManager] 成功读取剪贴板文本: %@", text ? @"有内容" : @"空内容");
return text ?: @"";
} else {
NSLog(@" [PasteboardManager] 剪贴板中无文本内容");
return @"";
}
} else {
// iOS 14以下直接读取
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *text = pasteboard.string;
NSLog(@"✅ [PasteboardManager] 读取剪贴板文本: %@", text ? @"有内容" : @"空内容");
return text ?: @"";
}
}
+ (void)writeTextToPasteboard:(NSString *)text {
NSLog(@"🔍 [PasteboardManager] 写入文本到剪贴板: %@", text ? @"有内容" : @"空内容");
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = text ?: @"";
NSLog(@"✅ [PasteboardManager] 文本已写入剪贴板");
}
+ (BOOL)hasTextInPasteboard {
if (@available(iOS 14.0, *)) {
BOOL hasText = [UIPasteboard generalPasteboard].hasStrings;
NSLog(@"🔍 [PasteboardManager] 检查剪贴板是否有文本: %@", hasText ? @"" : @"");
return hasText;
} else {
// iOS 14以下版本需要直接访问内容来判断
NSString *text = [UIPasteboard generalPasteboard].string;
BOOL hasText = (text != nil && text.length > 0);
NSLog(@"🔍 [PasteboardManager] 检查剪贴板是否有文本: %@", hasText ? @"" : @"");
return hasText;
}
}
+ (void)clearPasteboard {
NSLog(@"🔍 [PasteboardManager] 清空剪贴板");
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"";
NSLog(@"✅ [PasteboardManager] 剪贴板已清空");
}
@end