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.
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With