核心数据和 UIActivityViewController

Core Data and a UIActivityViewController

我的应用程序使用核心数据来保存数据。我在主 VC 上设置了 UIActivityViewController。一切如我所愿。

我的问题是,当您从核心数据模型中获取项目时,如何让它返回模型中的每个项目?

我应该使用谓词对项目进行排序吗?

编辑

这是我的 UIActivityViewController 设置。我知道一些 UIActivityViewController 操作不会出现在 sim 中,这很好。我正在使用确实显示的电子邮件操作。我看到我的 NSString 和 NSURL 显示没有问题,但我的项目名称 NSArray 没有显示。它应该出现在 sim 卡中吗?

- (IBAction)ShareItem:(id)sender {

NSString *textToShare = @"This is just some random text I put here";
NSURL *myWebsite = [NSURL URLWithString:@"http://www.martylavender.com/"];

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
fetchRequest.resultType = NSDictionaryResultType;

NSError *error      = nil;
NSArray *results    = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

NSMutableArray *itemNames = [results valueForKey:@"itemname"];

NSArray *objectsToShare = @[textToShare, myWebsite, itemNames];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                               UIActivityTypePrint,
                               UIActivityTypePostToTwitter,
                               UIActivityTypeAssignToContact,
                               UIActivityTypeSaveToCameraRoll,
                               UIActivityTypeAddToReadingList,
                               UIActivityTypePostToFlickr,
                               UIActivityTypePostToVimeo];

activityVC.excludedActivityTypes = excludeActivities;

[self presentViewController:activityVC animated:YES completion:nil];
NSLog(@"%@", itemNames); 
}

我确实在控制台中看到了我的项目打印出来。也许他们只是不会在 sim 中显示?

编辑

我想我可能知道为什么它没有出现。 UIActivityTypeMail 表示它支持:字符串、属性字符串和 url。不支持数据、图像、资产。

编辑

好的,明白了。我需要将我的 NSMutableArray 转换为 NSString。 NSString 有一种方法可以获取您的数组并将其粘贴到字符串中。它也有逆函数,它将把你的字符串粘贴到一个数组中。话虽这么说。

这是更新后的代码。我只需要弄清楚如何在将文本放入电子邮件后格式化文本,这将是很好的。

- (IBAction)ShareItem:(id)sender {

NSString *textToShare = @"This is just some random text I put here";
NSURL *myWebsite = [NSURL URLWithString:@"http://www.martylavender.com/"];

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
fetchRequest.resultType = NSDictionaryResultType;

NSError *error      = nil;
NSArray *results    = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSMutableArray *itemNames = [results valueForKey:@"itemname"];
NSString *names = [itemNames componentsJoinedByString:@""];
NSLog(@"%@",names);



NSArray *objectsToShare = @[textToShare, myWebsite, names];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                               UIActivityTypePrint,
                               UIActivityTypePostToTwitter,
                               UIActivityTypeAssignToContact,
                               UIActivityTypeSaveToCameraRoll,
                               UIActivityTypeAddToReadingList,
                               UIActivityTypePostToFlickr,
                               UIActivityTypePostToVimeo];

activityVC.excludedActivityTypes = excludeActivities;

[self presentViewController:activityVC animated:YES completion:nil];
}

编辑

我似乎完全把事情复杂化了。我只是在这里放了一个 \nNSString *names = [itemNames componentsJoinedByString:@""]; 并且让它们在每个项目之后都换行。

- (IBAction)ShareItem:(id)sender {

NSString *textToShare = @"This is just some random text I put here";
NSURL *myWebsite = [NSURL URLWithString:@"http://www.martylavender.com/"];

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
fetchRequest.resultType = NSDictionaryResultType;

NSError *error      = nil;
NSArray *results    = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSMutableArray *itemNames = [results valueForKey:@"itemname"];
NSString *names = [itemNames componentsJoinedByString:@" \n"];
NSLog(@"%@",names);



NSArray *objectsToShare = @[textToShare, myWebsite, names];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                               UIActivityTypePrint,
                               UIActivityTypePostToTwitter,
                               UIActivityTypeAssignToContact,
                               UIActivityTypeSaveToCameraRoll,
                               UIActivityTypeAddToReadingList,
                               UIActivityTypePostToFlickr,
                               UIActivityTypePostToVimeo];

activityVC.excludedActivityTypes = excludeActivities;

[self presentViewController:activityVC animated:YES completion:nil];
}

编辑

我似乎完全把事情复杂化了。我只是在这里放了一个 \nNSString *names = [itemNames componentsJoinedByString:@""]; 并且让它们在每个项目之后都换行。