這是在一個類別中儲存資料
UIPasteboard *pasteboard4 = [UIPasteboard pasteboardWithName:@"Pasteboard4" create:YES];
NSDictionary *loginDic = [NSDictionary dictionaryWithObject:@"用户名密码登陆" forKey:@"getKSWay"]; NSData *loginDictData = [NSKeyedArchiver archivedDataWithRootObject:loginDic]; [pasteboard4 setData:loginDictData forPasteboardType:@"getKSWay"];
然後在另一個類別中讀取
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"Pasteboard4" create:NO];
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:[pb dataForPasteboardType:@"getKSWay"]]; NSLog(@"登陆方式:%@",[dict objectForKey:@"getKSWay"]); 输出是null
首先你要好好理解UIPasteboard的使用。這點可以學習這篇文章
使用剪貼簿UIPasteboard
利用UIPasteboard保存data型資料時,設定pasteboardType為“public.data”,這個字串是蘋果提供的UTI,關於UTI,你可以參考我的文章
詳解蘋果提供的UTI