Core Data and a UIActivityViewController

253 views Asked by At

I have my app that is using core data to persist data. I have a UIActivityViewController setup on my main VC. Everything is working as I expect it to.

My question is when you go to grab items from a core data model, how do you have it give you back every item within the model?

Should I be using a predicate to sort the items?

EDIT

This is my UIActivityViewController setup. I know that some of the UIActivityViewController actions dont show up in the sim which is fine. I am using the email action which does show up. I see my NSString and NSURL show up without a problem but my itemnames NSArray is not showing up. Should it show up in the 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); 
}

I do see my items printed out in the console. Perhaps they just wont display in the sim?

EDIT

I think I may know why it doesnt show up. UIActivityTypeMail says that it supports: string, attributed string, and url. data, image, asset are not supported.

EDIT

Ok figured it out. I needed to take my NSMutableArray and cast that into an NSString. NSString has a method that will take your array and stick it into a string for you. It also has the inverse which will take your string and stick it into an array. That being said.

Here is the updated code. I just need to figure out how to format the text once its placed in the email and this will be golden.

- (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];
}

EDIT

I totally over complicate things it seems. I simply dropped a \n in here: NSString *names = [itemNames componentsJoinedByString:@""]; and that has them dropping to a new line after each item.

1

There are 1 answers

0
Martin On BEST ANSWER
- (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];
}

EDIT

I totally over complicate things it seems. I simply dropped a \n in here: NSString *names = [itemNames componentsJoinedByString:@""]; and that has them dropping to a new line after each item.