您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS UIPasteboard 完全指南:打造可複製的 UILabel 與跨 App 資料共享

編輯:iOS 相關資訊

UILabel 與 UIImageView 預設不接收觸碰事件,自然也就無法複製。要讓它們支援複製,得自己動手。本文從自訂可複製標籤開始,一路談到跨應用程式的剪貼板溝通。

一、讓 UILabel 能被複製

新增一個類別繼承 UILabel,先覆寫兩個關鍵方法,讓它成為第一回應者並只回應 copy:

@interface UICopyLabel : UILabel
@end

@implementation UICopyLabel

-(BOOL)canBecomeFirstResponder {
    return YES;
}

// 決定選單裡要出現哪些項目
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:));
}

// 真正的複製動作
-(void)copy:(id)sender {
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.text;
}

@end

二、掛上手勢並彈出選單

能成為第一回應者只是前提,還得先接收到觸碰。手寫一個手勢掛上去:

-(void)attachTapHandler {
    self.userInteractionEnabled = YES;   // 使用者交互總開關,UILabel 預設為 NO
    UITapGestureRecognizer *touch =
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    touch.numberOfTapsRequired = 2;      // 這裡設為雙擊
    [self addGestureRecognizer:touch];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self attachTapHandler];
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    [self attachTapHandler];             // 走 xib 建立時也要掛
}

收到點擊後,把自己設為第一回應者並讓系統選單浮現:

-(void)handleTap:(UIGestureRecognizer *)recognizer {
    [self becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:self.frame inView:self.superview];
    [menu setMenuVisible:YES animated:YES];
}

三個覆寫加上手勢與選單,一個可複製的 UILabel 就完成了。

三、改成可複製的 UIImageView

做法幾乎相同,把 UILabel 換成 UIImageView 作為基底,然後調整 canPerformAction 與複製/貼上的內容:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(copy:) || action == @selector(paste:));
}

-(void)copy:(id)sender {
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.image = self.image;
}

-(void)paste:(id)sender {
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    self.image = pboard.image;
}

放兩個 imageView 在畫面上,就能直觀驗證複製與貼上是否互通。

四、剪貼板不只應用內,也能跨應用

你複製一個網址、打開 Safari 貼到網址列,這就是系統通用剪貼板在運作。App 之間可以透過它交換資料。

但如果你的剪貼板只想讓「自己人」存取,就不要用系統通用剪貼板,而是自行建立一個具名的:

// 名稱建議使用反寫網域,確保唯一
// 第二個參數代表不存在時是否建立
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];

如此一來,App A 寫入、App B 讀出,彼此溝通又不會污染使用者系統剪貼板的內容。微信就是靠這類做法在不同入口之間傳遞資訊。

五、存放非文字資料

常見型別已被包成便利屬性:

若資料能轉為 plist(NSString、NSArray、NSDictionary、NSDate、NSNumber、NSURL),可用 setValue:forPasteboardType:;其他型別則用 setData:forPasteboardType:(plist 型別也能用這個)。實務上常把自訂物件先打包成 plist 再存檔:

NSDictionary *dict = [NSDictionary dictionaryWithObject:textField.text forKey:@"content"];
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:dict];
[pb setData:dictData forPasteboardType:@"myType"];

讀取端做反向操作:

UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:[pb dataForPasteboardType:@"myType"]];
caption.text = [dict objectForKey:@"content"];

六、關於 PasteboardType(UTI)

Type 是統一型別標識符(Uniform Type Identifier),作用是讓 App 判斷這份資料自己能不能處理——只支援純文字的 App 收到 UIImage 是沒有意義的。你可以使用系統公用的 UTI,也可以自訂,蘋果的建議是採用反寫網域加上型別名稱,例如 com.myCompany.myApp.myType。

實務提醒

具名剪貼板適合在自己家族的 App 之間傳遞資料,但別把它當成持久化儲存或大量資料傳輸的通道,系統對剪貼板內容有保留時間與容量上的限制。跨 App 交換資料若追求穩定與可觀測性,URL Scheme、Universal Links 或 App Groups 都各自有更合適的適用場景。另外,從 iOS 開始系統會在使用者貼上內容時提示 App 讀取了剪貼板,涉及個資的欄位更要謹慎處理。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved