让应用接入iCloud与多线程编程优化
立即解锁
发布时间: 2025-08-24 01:08:13 阅读量: 1 订阅数: 7 

### 让应用接入 iCloud 与多线程编程优化
#### 1. 完善文档应用基础功能
在处理颜色选择相关功能时,需要完成 `chooseColor:` 方法的实现,代码如下:
```objc
- (IBAction)chooseColor:(id)sender {
NSInteger selectedColorIndex = [(UISegmentedControl *)sender
selectedSegmentIndex];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:selectedColorIndex forKey:@"selectedColorIndex"];
}
```
为避免用户返回主列表时文档未保存而丢失最新更改,需在 `viewWillDisappear:` 方法中添加关闭文档的代码,使文档在用户离开详情视图时自动保存:
```objc
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
UIDocument *doc = self.detailItem;
[doc closeWithCompletionHandler:nil];
}
```
此时,这个基于文档的应用就可以进行测试了。你可以创建、编辑文档,切换文档列表并选择其他文档,一切都能正常工作。若打开 Xcode 控制台,每次文档加载或保存时都会看到相应输出。
#### 2. 为应用添加 iCloud 支持
要让应用支持 iCloud,除了对加载可用文件列表和指定加载新文件 URL 的方法进行修改外,还需处理一些管理细节。
##### 2.1 创建配置文件
步骤如下:
1. 访问 http://developer.apple.com 并登录开发者账户,进入 iOS 配置门户。
2. 进入 App IDs 部分,基于创建应用时使用的标识符创建新的 App ID。可以在 Xcode 项目导航器中选择顶级 TinyPix 项,选择 Summary 标签,在 iOS Application Target 部分的 Identifier 字段查看该标识符。
3. 在当前版本的门户中,设置通用名称为 TinyPix AppID,保留弹出菜单为 Use Team ID,输入应用标识符(如 com.apress.TinyPix),然后点击 Submit。
4. 创建 App ID 后,在表格中找到它,在 iCloud 下会看到黄色圆点和 Configurable 字样,点击旁边的 Configure 链接,在新页面中勾选 Enable for iCloud 复选框,然后点击 Done 返回 App ID 列表。
5. 切换到配置部分,为该 App ID 创建新的配置文件。点击 New Profile 按钮,输入配置文件名称 TinyPixAppPP。若没有开发证书,点击 Development Certificate 链接并按页面说明操作。
6. 配置好开发证书后,选择 TinyPix AppID 作为 App ID,选择应用要运行的设备。
7. 准备好后,将新的配置文件下载到 Mac 并双击安装到 Xcode。在 TinyPix 项目窗口中,选择顶级 TinyPix 对象,选择 TinyPix 项目本身,然后选择 Build Settings 标签,滚动到 Code Signing 部分,点击 Code Signing Identity 下 Debug 中的 Any iOS SDK 行的浅绿色弹出框,选择 TinyPixAppPP 下列出的开发者证书名称。
##### 2.2 启用 iCloud 权限
在项目导航器中选择顶级 TinyPix 项,从导航器右侧的项目和目标列表中选择 TinyPix 目标,切换到 Summary 标签,滚动到 Entitlements 部分,点击顶部的 Enable Entitlements 复选框,Xcode 会自动填充其余字段。
#### 3. 为 iCloud 进行代码修改
选择 BIDMasterViewController.m 开始为 iCloud 进行修改,主要是改变查找可用文档的方式。
##### 3.1 添加属性
```objc
@interface BIDMasterViewController ()
@property (strong, nonatomic) NSArray *documentFilenames;
@property (strong, nonatomic) BIDTinyPixDocument *chosenDocument;
@property (strong, nonatomic) NSMetadataQuery *query;
@property (strong, nonatomic) NSMutableArray *documentURLs;
- (NSURL *)urlForFilename:(NSString *)filename;
- (void)reloadFiles;
@end
```
##### 3.2 修改文件列表方法
移除原有的 `reloadFiles` 方法,替换为以下代码:
```objc
- (void)reloadFiles {
NSFileManager *fileManager = [NSFileManager defaultManager];
// passing nil is OK here, matches first entitlement
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
NSLog(@"got cloudURL %@", cloudURL); // returns nil in simulator
self.query = [[NSMetadataQuery alloc] init];
query.predicate = [NSPredicate predicateWithFormat:@"%K like '*.tinypix'",
NSMetadataItemFSNameKey];
query.searchScopes = [NSArray arrayWithObject:
NSMetadataQueryUbiquitousDocumentsScope];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateUbiquitousDocuments:)
name:NSMetadataQueryDidFinishGatheringNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(updateUbiquitousDocuments:)
name:NSMetadataQueryDidUpdateNotification
object:nil];
[query startQuery];
}
```
##### 3.3 实现查询完成后的方法
```objc
- (void)updateUbiquitousDocuments:(NSNoti
```
0
0
复制全文
相关推荐










