让应用接入iCloud与多线程编程入门
立即解锁
发布时间: 2025-08-24 01:05:46 阅读量: 1 订阅数: 6 

### 让应用接入 iCloud 与多线程编程入门
#### 一、完善文档应用并避免数据丢失
在处理颜色选择时,需要完善 `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 控制台,可以看到文档加载和保存时的输出。
#### 二、为应用添加 iCloud 支持
要让应用支持 iCloud,需要进行以下操作:
1. **创建 iCloud 配置文件**
- 访问 [http://developer.apple.com](http://developer.apple.com) 登录开发者账户,进入 iOS 配置门户。
- 在 App IDs 部分,根据创建 TinyPix 时使用的标识符创建新的 App ID。例如,如果使用 `com.apress` 作为应用标识符的基础,TinyPix 的标识符将是 `com.apress.TinyPix`。
- 设置通用名称为 `TinyPix AppID`,保留弹出菜单为 `Use Team ID`,输入 `com.apress.TinyPix` 作为 Bundle Identifier,然后点击提交。
- 创建 App ID 后,在 iCloud 下会看到黄色圆点和“Configurable”字样,点击旁边的“Configure”链接,在新页面中启用“Enable for iCloud”复选框,然后点击“Done”返回 App ID 列表。
- 切换到配置部分,为该 App ID 创建新的配置文件。点击“New Profile”按钮,输入配置文件名称 `TinyPixAppPP`。如果没有开发证书,点击“Development Certificate”链接并按照页面上的说明操作。设置好开发证书后,选择 `TinyPix AppID` 作为 App ID,最后选择要运行应用的设备。
- 下载新的配置文件到 Mac 并双击安装到 Xcode。在 TinyPix 项目窗口中,选择顶级 TinyPix 对象,选择 TinyPix 项目本身,然后选择“Build Settings”选项卡。滚动到“Code Signing”部分,找到“Code Signing Identity”,在“Debug”中找到“Any iOS SDK”,点击浅绿色弹出框,选择 `TinyPixAppPP` 下列出的开发者证书名称。
2. **启用 iCloud 权限**
- 在项目导航器中选择顶级 TinyPix 项目,在右侧的项目和目标列表中选择 TinyPix 目标。
- 切换到“Summary”选项卡,滚动到“Entitlements”部分(当前为空)。
- 点击该部分顶部的“Enable Entitlements”复选框,Xcode 会自动填充其余字段,指定一个名为 `TinyPix` 的 Entitlements 文件,并在其他三个部分填充应用标识符。
#### 三、为 iCloud 支持修改代码
1. **添加属性**
在 `BIDMasterViewController.m` 中添加以下属性:
```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
```
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. **实现查
0
0
复制全文
相关推荐









