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);
the UIActivityViewController takes to parameters,
1st activityItems , which is an array of type Any, you can pass whatever you want to share, if you want to share different datatypes , you can use an NSArray and cast it to an array of any like that let activityItems : NSArray = [object1, object2] as! [Any]
2nd application activities, which is the services that your application may provided, which will be mostly nil.
For Swift 3 -> 4.2, Use the below syntax :
let yourMessage = "this is your\n message that you want to share !!"
let activityItems = [yourMessage]
let activityController = UIActivityViewController(activityItems: activityItems , applicationActivities: nil)
self.present(activityController, animated: true, completion: nil)
since iOS 6, Apple has provided the handy, but much-overlooked, UIActivityViewController
class.
first of all Create an instance of UIActivityViewController
and, for activityItems
, pass in a message or a URL or both to your app’s data file. Depending on the activity the user selects from the activity menu, iOS uses the message you pass in to pre-populate its content.
For example, if the user chooses to share the book via Email, the message you passed in will pre-populate the body of the email. Since not all activities have such capability, such as AirDrop, iOS may discard the message.
finally present
the UIActivityViewController
to the user.
let shareString = "Hello This is a sharingText!"
let shareUrl = URL(string: "https://www.google.com")
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [shareString, shareUrl], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
来源:https://stackoverflow.com/questions/25320520/sharing-text-as-a-string-to-another-app