Share link + text + image in whatsapp from swift

1

I'm doing an app, which is an RSS reader.

What I want is that you can share a story by whatsapp, and that the data you share are:

  • An image
  • A title
  • A link

At the moment I have this:

let urlString = "Hello Friends, Sharing some data here..."
        let urlStringEncoded = urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
        //URLHostAllowedCharacterSet
        urlToGo  = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")
        print(urlToGo)
        if UIApplication.sharedApplication().canOpenURL(urlToGo!){
            UIApplication.sharedApplication().openURL(urlToGo!)
        }else{
            let alertController = UIAlertController(title: "Error", message: "No tienes instalado Whatsapp en el dispositivo", preferredStyle: .Alert)

            let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)

            alertController.addAction(defaultAction)
            presentViewController(alertController, animated: true, completion: nil)

        }

How can I add a link, a text and an image?

There is a library with which you can do it

Thanks !!!!

    
asked by 04.07.2016 в 08:45
source

1 answer

0

You can use UIActivityViewController to share the image, text and link.

NSString *textToShare = @"Enter your text to be shared";
UIImage * image = [UIImage imageNamed:@"imagename"];

NSArray *objectsToShare = @[textToShare, image];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];


[self presentViewController:activityVC animated:YES completion:nil];

Someone asked the same thing in stackoverflow in English: ( link )

@AlbertoMier I asked in the previous route, if I could use the code for Swift 2 and they have answered me with this part of the code in case you want to try it.

let shareText="Hello, world!" if let image = UIImage (named: "myImage") {let vc = UIActivityViewController (activityItems: [shareText, image], applicationActivities: []) presentViewController (vc, animated: true, completion: nil)}

I leave a link to an app made in swift that says it does what you want. The app has been updated 2 days ago, it is sure that it is useful for swift 2. ( link )

    
answered by 04.07.2016 / 09:12
source