title: SMS behavior using UIActivityViewController
body: <p>I'm using a UIActivityViewController to share some text from within my app. I've subclassed the UIActivityItemProvider so that I can handle the text depending on the sharing application selected. Before I present the view controller, I create a text file that can get attached to a mail message. (I write the file to a URL and add it to the shared items array.) If the user taps the Mail icon, the text document file gets attached to the message and all is good in the world. Here is the code I use:</p>
  
  <pre><code>- (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];
       }
   }];    
  </code></pre>
  
  <p>However, I have two problems:</p>
  
  <p>First, when the user taps the SMS icon to send the NSString in a text, the file that got created earlier also gets attached (or imbedded) in the text of the message. Is there a way to 'selectively' attach a file depending on which sharing service is selected? Or maybe a way to remove the file when the user taps the SMS icon? Is there some housekeeping that I need to do in the activity provider subclass to clean up the text message?</p>
  
  <p>Second, on a real device the SMS message doesn't get to its target. The completion handler triggers and shows the SMS message completes, but I never receive it. Not sure there is a race condition here or not - it actually worked once where I received the text message with the string.</p>
  
  <p>Here is the activityController code:</p>
  
  <pre><code>- (id) activityViewController:(UIActivityViewController *)activityViewController
        itemForActivityType:(NSString *)activityType
  
  // the number of item to share
  static UIActivityViewController *shareController;
  static int itemNo;
  if (shareController == activityViewController &amp;&amp; itemNo &lt; 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;
  </code></pre>