我的应用程序有一个动作按钮来拉出UIActivityViewController。在我的测试设备5S上,它会做得很好,包括发短信。但是,在我的5C测试设备上,它在尝试发送消息时崩溃。我的代码在这里有什么问题可以使它与某些手机一起发送Message而不是其他手机吗?
下面列出的代码有一些警告。首先,有时候,要发送的文本将超过140个字符,所以我首先检查字符串的长度。如果超过140个字符,则剪切出中间部分,在结束前添加...,并将其保持为140个字符。然后,在动作上,如果超过140个字符,则仅针对Twitter和消息发送编辑后的字符串,但针对所有其他字符发送常规字符串,如果是140个字符或以下,则发送常规字符串。
-(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;
}