export profile Data

This commit is contained in:
TrueZhuangjia
2025-03-29 21:44:06 +08:00
committed by TrueZhuanjia
parent c8522de476
commit cc285d5488
6 changed files with 44 additions and 16 deletions
@@ -13,7 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
/**
This view displays a list of on screen controller profiles and gives the user the ability to select any of the profiles to be the 'Selected' profile whose on screen controller layout configuration will be shown on the game stream view, or in the on screen controller layout view. This view also allows the user to swipe and delete any of the listed profiles.
*/
@interface OSCProfilesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@interface OSCProfilesTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIDocumentPickerDelegate>
- (void)profileViewRefresh;
@@ -166,9 +166,37 @@ const double NAV_BAR_HEIGHT = 50;
[self profileViewRefresh];
}
- (IBAction) exportDataTapped:(id)sender {
// 创建临时占位文件(仅用于提供文件名)
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"profiles.bin"];
[[NSData new] writeToFile:tempPath atomically:YES]; // 空文件
// 初始化选择器
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithURL:[NSURL fileURLWithPath:tempPath] inMode:UIDocumentPickerModeExportToService];
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (IBAction) exitTapped:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
#pragma mark - UIDocumentPickerDelegate
- (void)documentPicker:(UIDocumentPickerViewController *)controller
didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
if (urls.count == 0) return;
NSURL *destinationURL = urls.firstObject;
NSArray *profiles = [profilesManager getAllProfiles];
// 1. 序列化数据
NSError *error;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:profiles requiringSecureCoding:YES error:&error];
if (!data) {
NSLog(@"序列化失败: %@", error);
return;
}
// 2. 安全写入
[destinationURL startAccessingSecurityScopedResource];
BOOL success = [data writeToURL:destinationURL options:NSDataWritingAtomic error:&error];
[destinationURL stopAccessingSecurityScopedResource];
if (!success) {
NSLog(@"写入失败: %@", error);
}
}