我正在使用UIActivityViewController在我的应用中分享一些文字。我已经将UIActivityItemProvider子类化,以便我可以根据所选的共享应用程序处理文本。在我呈现视图控制器之前,我创建了一个可以附加到邮件消息的文本文件。 (我将文件写入URL并将其添加到共享项目数组。)如果用户点击“邮件”图标,则文本文档文件将附加到邮件中,并且一切都在世界范围内。这是我使用的代码:
- (IBAction)shareBarButtonPressed:(UIBarButtonItem *)sender
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *resultsFile = [docPath stringByAppendingString:@"/results.txt"];
[self createResultFile];
NSURL *fileUrl = [NSURL fileURLWithPath:resultsFile];
NSData *fileData = [[NSData alloc]initWithContentsOfFile:resultsFile];
[fileData writeToURL:fileUrl atomically:YES];
NSString *testString = @"This is my string";
PRActivityProvider *activityProvider = [[PRActivityProvider alloc] initWithPlaceholderItem:[self fillResultFromView]];
NSArray *items = @[activityProvider, testString];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:items
applicationActivities:nil];
NSString *subject = [NSString stringWithFormat:@"Results: %@",self.result.eventName];
[activityViewController setValue:subject forKey:@"subject"];
[activityViewController setExcludedActivityTypes:
@[UIActivityTypeSaveToCameraRoll,
UIActivityTypeAssignToContact,
UIActivityTypePostToTencentWeibo,
UIActivityTypeCopyToPasteboard]];
[self presentViewController:activityViewController animated:YES completion:nil];
[activityViewController setCompletionHandler:^(NSString *act, BOOL done)
{
NSString *ServiceMsg = nil;
if ( [act isEqualToString:UIActivityTypeMail] ) ServiceMsg = @"Results sent!";
if ( [act isEqualToString:UIActivityTypePostToTwitter] ) ServiceMsg = @"Results posted on Twitter!";
if ( [act isEqualToString:UIActivityTypePostToFacebook] ) ServiceMsg = @"Results posted on FaceBook!";
if ( [act isEqualToString:UIActivityTypeMessage] ) ServiceMsg = @"SMS sent!";
if ( done )
{
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:ServiceMsg message:@"" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[Alert show];
}
}];
但是,我有两个问题:
首先,当用户点击SMS图标以在文本中发送NSString时,先前创建的文件也会附加(或嵌入)在消息文本中。有没有办法选择性地选择'根据选择的共享服务附加文件?或者也许是一种在用户点击短信图标时删除文件的方法?我是否需要在活动提供程序子类中进行一些清理以清理文本消息?
其次,在真实设备上,SMS消息无法到达其目标。完成处理程序触发并显示SMS消息完成,但我从未收到它。不确定这里是否存在竞争条件 - 它实际上在我收到带有字符串的短信的情况下工作了一次。
这是activityController代码:
- (id) activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
// the number of item to share
static UIActivityViewController *shareController;
static int itemNo;
if (shareController == activityViewController && itemNo < numberOfSharedItems - 1)
itemNo++;
else {
itemNo = 0;
shareController = activityViewController;
}
PRResults *result = [[PRResults alloc]init];
result = self.placeholderItem;
// twitter
if ([activityType isEqualToString: UIActivityTypePostToTwitter]) {
return [self makeTweetFromResult:result];
}
// SMS text message
else if ([activityType isEqualToString: UIActivityTypeMessage]) {
return [self makeTweetFromResult:result];
}
// email
else if ([activityType isEqualToString: UIActivityTypeMail]) {
return @"Open the attached results file in a notepad app to view.";
}
// Facebook
else if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return [self makeFacebookPostFromResult:result];
}
return nil;