My app has an action button to pull up a UIActivityViewController. On my test device 5S, it will do everything fine, including texting. However, on my 5C test device, it crashes upon trying to send a Message. Is there something wrong with my code here that would make it work with SOME phones for sending Message, and not others?
The code listed below has some caveats to it. First of all, some times, the text to be sent will be over 140 characters, so I run a check first on the length of the string. If over 140 characters, it clips out middle part, adds ... before the end, and keeps it at 140 characters. Then, on the action, if over 140 characters it sends the edited string just for Twitter and Message, but the regular string for all others, and regular string, if 140 characters or under.
-(void)sendit {
NSString *string = label1.text;
if ([string length] > 140) {
int maxChars = 140;
int charsOver = [string length] - maxChars;
NSString *replacementString = @"...";
charsOver += [replacementString length]; //account for adding "..."
NSArray *components = [string componentsSeparatedByString:@" - "];
NSMutableString *stringToTrim = [NSMutableString string];
int numberOfComponents = [components count];
for (int i = 0; i < numberOfComponents - 1; i++) {
NSString *component = [components objectAtIndex:i];
if (i < numberOfComponents - 2) {
[stringToTrim appendFormat:@"%@ - ", component];
}
else {
[stringToTrim appendString:component];
}
}
NSString *trimmedString = [stringToTrim substringToIndex:[stringToTrim length] - charsOver];
self.finalString = [NSString stringWithFormat:@"%@%@%@", [trimmedString stringByAppendingString:replacementString], @" - ", [components objectAtIndex:numberOfComponents - 1]];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:nil];
activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,
UIActivityTypeAssignToContact,
UIActivityTypePrint,
UIActivityTypeAirDrop
];
[self presentViewController:activityVC animated:YES completion:nil];
}
else {
NSArray *activityItems = @[label1.text];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,
UIActivityTypeAssignToContact,
UIActivityTypePrint,
UIActivityTypeAirDrop
];
[self presentViewController:activityVC animated:YES completion:nil];
}
}
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
if ([activityType isEqualToString:UIActivityTypeMail]) {
NSLog(@"TEST");
return label1.text;
}
if ([activityType isEqualToString:UIActivityTypePostToTwitter]){
NSString *string = label1.text;
if ([string length] > 140) {
return self.finalString;
}
else {
return label1.text;
}
}
if ([activityType isEqualToString:UIActivityTypePostToFacebook]){
return label1.text;
}
if ([activityType isEqualToString:UIActivityTypeCopyToPasteboard]){
return label1.text;
}
if ([activityType isEqualToString:UIActivityTypeMessage]){
NSString *string = label1.text;
if ([string length] > 140) {
return self.finalString;
}
else {
return label1.text;
}
}
return nil;
}