プログラム内で任意の文字列をクリップボードにコピーしたかったので検索。
下記サイトを参考にしました。
Objective-Cで指定した文字列をペーストボードにコピーする – Dolice Lab
1 2 |
UIPasteboard *board = [UIPasteboard generalPasteboard]; [board setValue:@"コピーさせたい文字列" forPasteboardType:@"public.utf8-plain-text"]; |
これだけだとユーザーがわかりにくいのでアラートビューを出すと良いかも。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
- (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"]; } |