UIActivityViewController Not Sharing Both URL & Text in Messages - ios

I have a UIActivityViewController which takes in an array of URLs that resolve to images within the app's Documents folder and Strings:
var contentArray = [Any]();
contentArray.append("Test Text");
contentArray.append(URL(<<valid url to image in app's documents folder>>));
let shareSheet = UIActivityViewController(activityItems: contentArray, applicationActivities: nil);
if let popoverController = shareSheet.popoverPresentationController {
popoverController.sourceView = <<valid reference to sending view>>;
}
self.present(shareSheet, animated: true);
When the user clicks "Messages" it only shows the image that was retrieved from the local URL and the String "Test Text" is not appended to the message body.
However, if the URL is changed to an external URL such as https://google.com, both the URL and the "Test Text" appear in the Messages body.
Is there a reason both text (Strings) and images (URLs linking to the app's documents folder) aren't shown in the Messages body?

Related

XCODE / IOS - Share audio file to whatsapp with a direct opening

I am trying to share an audio file from my ios app to whatsapp, but with a direct opening of whatsapp, and not an opening of the sharing menu with all the tiles.
Here is what i have now:
// Getting the original file
let fileName = #MY FILE NAME#
let filePath = Bundle.main.path(forResource: fileName, ofType: "mp3")!
let urlData = URL.init(fileURLWithPath: filePath)
let nsData = NSData(contentsOf: urlData)
if (nsData != nil){
// Creating the temporary file to share in the accessible ressources
let newFileName = "file.mp3"
let newFilePath = "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/\(newFileName)"
nsData?.write(toFile: newFilePath, atomically: true)
let newUrlData = URL.init(fileURLWithPath: newFilePath)
// Sharing the file to whatsapp
// Possibility 1 (does not work yet)
// let documentController = UIDocumentInteractionController(url: newUrlData)
// documentController.uti = "net.whatsapp.audio"
// documentController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
// Possibility 2 (works only with the sharing menu)
let activityVC = UIActivityViewController(activityItems: [NSURL(fileURLWithPath: newFilePath)], applicationActivities: nil)
self.present(activityVC, animated: true, completion: nil)
}
As I do this, sharing an audio file to whatsapp works, but it first open the sharing menu, with the messenger tile, message tile, notes tile, ... (and it doesn't works for the messenger app). In the end I would like to be able to share on messenger AND whatsapp.
As explicated here in the whatsapp documentation, I want to open directly the whatsapp application when I try to share the file:
Alternatively, if you want to show only WhatsApp in the application list (instead of WhatsApp plus any other public/*-conforming apps) you can specify a file of one of aforementioned types saved with the extension that is exclusive to WhatsApp:
images - «.wai» which is of type net.whatsapp.image
videos - «.wam» which is of type net.whatsapp.movie
audio files - «.waa» which is of type net.whatsapp.audio
When triggered, WhatsApp will immediately present the user with the contact/group picker screen. The media will be automatically sent to a selected contact/group.
So I tried to change the line :
let newFileName = "file.mp3"
To one of these :
let newFileName = "file.mp3.waa"
let newFileName = "file.waa"
let newFileName = "file.waa.mp3"
But it still shows the same sharing menu (and can't read the audiofile if it ends with the .waa extension).
-> 1) Is it possible to do what I want to do ?
-> 2) If not, is there a way to share to messenger & whatsapp with the same code keeping one sharing menu
-> 3) If not, is there a way to reduce the sharing menu to only one tile depending on different calling event, so there is no confusing choosing of tiles
Thanks,
Antoine
Cf: XCODE / IOS - How to use exclusive extension to immediately present whatsapp (.wai, .waa, .wam)
FYI: As I went through a lot of tests with this, I couldn't find any solution yet.
Whatsapp recognize the file extension, but cannot even read it. Once shared, when you click on it, it's written ".whatsapp audio file", nothing more (And it's not even shared directly).
I sent a email to whatsapp developper team, they said they have others problem to fix currently, so it's not even on their to do list.
Wait & see..

How to enable camera in MFMessageComposeViewController?

I am developing an iOS application with a button to report an issue using SMS/iMessage. I am using MFMessageComposeViewController to present the message composition interface using the following code (Swift 3):
if(MFMessageComposeViewController.canSendText()){
let controller = MFMessageComposeViewController()
controller.messageComposeDelegate = self
controller.body = "Example Message"
controller.recipients = ["2345678901"]
self.present(controller, animated: true, completion: nil)
}
I have also implemented the MFMessageComposeViewControllerDelegate function to dismiss properly. A standard text message / iMessage sends successfully, but the user does not have the option to attach an image. The buttons for camera, iMessage Apps, etc. are there, but they are disabled and cannot be pressed. How can I enable these buttons (camera, specifically) to allow my users to attach images to messages composed with the app?
The Buttons in Question:
EDIT:
Thanks Abdelahad for the suggestion. I've modified his response to allow multiple recipients and to include a message body. I also updated it to remove the deprecated addingPercentEscapes(using: ) method.
Here is a solution using a url to open the Messages app. NOTE: This takes users out of the app.
let recipients = "2345678901,3456789012" //Phone Numbers
let messageBody = "This is a test"
let sms: String = "sms://open?addresses=\(recipients)&body=\(messageBody)"
let smsEncoded = sms.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
let url = URL(string: smsEncoded!)
UIApplication.shared.openURL(url!)
But still I would like a solution that does not take the user out of the app. Is this possible? Why would the MFMessageComposeViewController show the buttons without enabling them?
Don't use MFMessageComposeViewController use UIApplication.shared.openURL(url!) but this will takes the user out of the app
var phoneToCall: String = "sms: +201016588557"
var phoneToCallEncoded = phoneToCall.addingPercentEscapes(using: String.Encoding.ascii)
var url = URL(string: phoneToCallEncoded)
UIApplication.shared.openURL(url!)

Why do some users get a different Facebook Share Sheet Dialog box than others

FB Dialog Missing og: Title
When some users share to facebook their dialog box is missing the auto-populated og: title and the image is displayed like a polaroid rather than a banner.
let content = FBSDKShareLinkContent()
content.contentURL = NSURL(string: longUrlString)
FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: nil)

How to pass a Document File to UIActivityViewController i.e. Share Sheet

I have an iOS App which generates a CSV File and saves it to the working Documents directory of the device. Now when a user presses a button, the UIActivityViewController Share Sheet is displayed which allows you to open send data to other apps.
My question is how do I pass this CSV file to the Share Sheet. I know how to make this work with Text and Images, but not exactly sure how to get this to work with a CSV file. The end result is that I want this file to show up as an attachment in any email client which is selected from the Share Sheet.
This is the code I use to open a document (word, pdf, etc.). Should work for you...
#IBAction func openDocument(sender: AnyObject)
{
let interactionController = UIDocumentInteractionController(URL: documentURL)
// (build your document's URL from its path in the Documents directory)
interactionController.delegate = self
// First, attempt to show the "Open with... (app)" menu. Will fail and
// do nothing if no app is present that can open the specified document
// type.
let result = interactionController.presentOpenInMenuFromRect(
self.view.frame,
inView: self.view,
animated: true
)
if result == false {
// Fall back to "options" view:
interactionController.presentOptionsMenuFromRect(
self.view.frame,
inView: self.view,
animated: true)
}
// DONE
}

How to share a text file with UIActivityViewController

I've tried sharing an NSData object of the file contents, and the activity view comes up with the mail option, and then the mail compose controller displays, but there's no attachment.
I've tried sharing an NSUrl with the path of the file, but in that case when the activity view comes up it takes up the whole screen but is blank except for the "cancel" button at the bottom. Weird. Also the activity view only comes up on the device in this case, it never even comes up on the simulator.
If I convert the NSData to an NSString, then it does work, but it just pastes the string into the body of the email. I don't want that, I want to attach a file.
I've used the debugger to verify that the NSData object has (the correct) data and that the NSUrl object has the right file path. No dice.
var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var file = Path.Combine(documents, "file.txt");
NSData dataToShare = NSFileManager.DefaultManager.Contents(file);
UIActivityViewController activityViewController = new UIActivityViewController(new NSObject[] { dataToShare }, null); //Email comes up but data isn't attached
// OR
UIActivityViewController activityViewController = new UIActivityViewController(new NSObject[] { new NSUrl(file) }, null); //Activity view takes up the whole screen and is blank
// OR
UIActivityViewController activityViewController = new UIActivityViewController(new NSObject[] { (NSString)dataToShare.ToString() }, null); //Pastes string into email body

Resources