Search code examples
iosobjective-csharing

Sharing text as a string to another app


I have a string that basically looks like this:

Scores:

Player One: Score
Player Two Score
Player Three: Score

I want to share this as text to apps such as WhatsApp, Facebook, iMessage, etc. What is the best way to do this? I have tried sharing as a .txt file, but it shares as a file instead of a regular message in WhatsApp.


Solution

  • You could use a custom URL scheme. Apps like Facebook and WhatsApp generally have their own schemes that you can use to send data into those apps. See WhatsApp's info here: Link

    Alternatively, you could use a UIActivityViewController. This also supports other data types, not just strings (see this SO question).

        NSString *textToShare = @"your text";
        UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[textToShare] applicationActivities:nil];
        activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll]; //Exclude whichever aren't relevant
        [self presentViewController:activityVC animated:YES completion:nil];
    

    Here's a nice blog post on this method: Link