xcodeでクリップボードへコピー

プログラム内で任意の文字列をクリップボードにコピーしたかったので検索。

下記サイトを参考にしました。
Objective-Cで指定した文字列をペーストボードにコピーする – Dolice Lab

UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setValue:@"コピーさせたい文字列" forPasteboardType:@"public.utf8-plain-text"];

これだけだとユーザーがわかりにくいのでアラートビューを出すと良いかも。

- (IBAction)copyTextButton:(id)sender
{
    [self setText];
    UIAlertView *alart = [[UIAlertView alloc] initWithTitle:@"下記の内容をクリップボードにコピーしますか?"
                                                    message:settingTextWithURL
                                                   delegate:self
                                          cancelButtonTitle:@"キャンセル"
                                          otherButtonTitles:@"OK",nil];
    alart.tag = 1;
    [alart show];
    
}

-(void) copyText //クリップボードにコピーする
{
    // UIPasteboardのインスタンスを生成する。
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    
    [self setText];
    
    // 文字列を貼付ける。
    [pasteboard setValue:settingTextWithURL forPasteboardType:@"public.utf8-plain-text"];
    
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です