Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 572
Dale Townsend Avatar asked Aug 15 '14 02:08

Dale Townsend


2 Answers

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

like image 145
rebello95 Avatar answered Sep 27 '22 23:09

rebello95


In swift you can do like this to share a string

var shareString = "Hello i am share string please share me!";

        var activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [shareString], applicationActivities: nil);

        var currentViewController:UIViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!

        currentViewController.presentViewController(activityViewController, animated: true, completion: nil);
like image 38
guest Avatar answered Sep 27 '22 23:09

guest