Sharing text as a string to another app
Asked Answered
P

5

9

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.

Pluckless answered 15/8, 2014 at 2:40 Comment(0)
P
25

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

Proteinase answered 15/8, 2014 at 2:50 Comment(4)
Is there a way to share to any app that can take text, instead of explicitly including certain apps? I know you can do that for files like images, .txt, etc.Pluckless
I've tried that and I don't think it brings up any third party apps. Maybe my best option is just to use custom URL schemes and include the most popular messaging apps.Pluckless
I haven't actually used UIActivityViewController before, I have used URL schemes in the past. They've worked for me in the situations I needed them.Proteinase
Thanks for your help. I'll use custom URL schemes and for cases where the app the user wants to share to isn't included I'll have an option to save as an image or .txt file.Pluckless
A
11

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);
Amara answered 26/6, 2015 at 15:59 Comment(0)
T
4

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)
Tubing answered 5/12, 2018 at 8:46 Comment(0)
J
0

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)
Josphinejoss answered 15/10, 2019 at 13:7 Comment(0)
A
0

For Swift 5:

let shareString = "Hello i am share string please share me!";
    
let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [shareString], applicationActivities: nil)

self.present(activityViewController, animated: true, completion: nil)
Agronomics answered 6/9, 2022 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.