共享图像对象时 UIActivityViewController 不显示消息选项
UIActivityViewController not showing Message option When Sharing Image Object
我正在使用 UIActivityViewController 分享我生成的图像,它适用于某些应用程序,如 Whatsapp 或通过电子邮件分享,但我也想使用消息应用程序分享它。我已经尝试了几种方法来做到这一点,但我无法在其上显示消息应用程序。
这是我的代码:
UIImage *receiptStage3 = [CreateReceiptImage putLogo:nil putLabel:TRANSACTION_RECEIPT inImage:receiptStage2];
NSData *imageData = UIImageJPEGRepresentation(receiptStage3,0.5);
// create url
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *fileName = [NSString stringWithFormat:@"%@%@%@", @"FormatHere-", [dateFormatter stringFromDate:[NSDate date]],@".jpg"];
NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:fileName]];
NSData *data = imageData;
// write data
[data writeToURL:url atomically:NO];
NSArray *activityItems = @[url];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePostToFlickr, UIActivityTypePostToTwitter, UIActivityTypePostToFacebook, UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeOpenInIBooks, UIActivityTypeAirDrop,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList];
[activityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
//Delete file
NSError *errorBlock;
if([[NSFileManager defaultManager] removeItemAtURL:url error:&errorBlock] == NO) {
NSLog(@"error deleting file %@",@"error");
return;
}
}];
[self presentViewController:activityVC animated:YES completion:nil];
正如我之前所说,我尝试了几种方法,例如将 NSUrl @[url] 和 UIImage *receiptStage3 直接放入 initWithActivityItems。我也尝试删除 excludedActivityTypes 但 Message 选项不会显示。我错过了什么吗?还是有其他方法可以做到这一点?先谢谢了
你的代码完全没问题,对我有用。
首先,如果您在设置中禁用了 iMessage,您的信息应用程序可能会丢失。转到“设置”->“消息”并确保它已打开:
此外,您可能在默认项目中看不到它,请查看隐藏项目:
我正在使用 UIActivityViewController 分享我生成的图像,它适用于某些应用程序,如 Whatsapp 或通过电子邮件分享,但我也想使用消息应用程序分享它。我已经尝试了几种方法来做到这一点,但我无法在其上显示消息应用程序。 这是我的代码:
UIImage *receiptStage3 = [CreateReceiptImage putLogo:nil putLabel:TRANSACTION_RECEIPT inImage:receiptStage2];
NSData *imageData = UIImageJPEGRepresentation(receiptStage3,0.5);
// create url
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *fileName = [NSString stringWithFormat:@"%@%@%@", @"FormatHere-", [dateFormatter stringFromDate:[NSDate date]],@".jpg"];
NSURL *url = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:fileName]];
NSData *data = imageData;
// write data
[data writeToURL:url atomically:NO];
NSArray *activityItems = @[url];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePostToFlickr, UIActivityTypePostToTwitter, UIActivityTypePostToFacebook, UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeOpenInIBooks, UIActivityTypeAirDrop,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList];
[activityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
//Delete file
NSError *errorBlock;
if([[NSFileManager defaultManager] removeItemAtURL:url error:&errorBlock] == NO) {
NSLog(@"error deleting file %@",@"error");
return;
}
}];
[self presentViewController:activityVC animated:YES completion:nil];
正如我之前所说,我尝试了几种方法,例如将 NSUrl @[url] 和 UIImage *receiptStage3 直接放入 initWithActivityItems。我也尝试删除 excludedActivityTypes 但 Message 选项不会显示。我错过了什么吗?还是有其他方法可以做到这一点?先谢谢了
你的代码完全没问题,对我有用。
首先,如果您在设置中禁用了 iMessage,您的信息应用程序可能会丢失。转到“设置”->“消息”并确保它已打开:
此外,您可能在默认项目中看不到它,请查看隐藏项目: