Send attachments using sms/mms in iOS SDK - ios

In iOS 7, there is support for adding attachments in sms messages via third party applications.
I want to know:
What kind of files are supported as attachments? e.g. .png, .pdf etc.
Can I send NSData through an sms/mms message? e.g. .dat format
Would the recipient of these messages be able to open these attachments in third party applications using iOS's "Open In" feature?

The MFMessageComposeViewController wants the attachment to have the correct extension for the type of image you're uploading.
I verified by testing with a PNG file, and the following variations of adding the attachment data:
[messageController addAttachmentData:imgData typeIdentifier:#"public.data" filename:#"image"];
[messageController addAttachmentData:imgData typeIdentifier:#"public.data" filename:#"image.abc"];
[messageController addAttachmentData:imgData typeIdentifier:#"public.data" filename:#"image.png"];
Only the last option worked.
I didn't need to change the typeIdentifier, although it probably would make sense to choose a UTI that matches the type of data.

According to Apple MFMessageComposeViewController docs, you can do that by creating a MFMessageComposeViewController object, and add the attachment through the following functions:
func addAttachmentURL(URL, withAlternateFilename: String?)
Attaches a specified file to the message.
func addAttachmentData(Data, typeIdentifier: String, filename: String)
Attaches arbitrary content to the message.
(BTW you should check the canSendAttachments before you try to use those functions)

Related

iCloud Document Picker with Custom UTI

Does UIDocumentPickerViewController initWithDocumentTypes require a public UTI to function?
I am trying to utilize iCloud Documents to allow users to import a proprietary file type from iCloud Drive. Testing works fine for public UTI, such as: #"public.text"
If I don't include a public UTI in the initWithDocumentTypes array, I get a screen indicating:
No Documents. Documents in iCloud Drive are not available because the
iCloud Documents & Data setting is disabled.
My Imported UTI is defined in Target > Info as "com.domain.file". I have to believe this is set up correctly, as I can select one of my proprietary files in another app (e.g. Dropbox) and my app is displayed in the Open In... options.
In my import action, I've tried every variation I can think of to get my custom UTI to display the picker.
- (IBAction)importDocumentPickerTapped:(id)sender
{
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc initWithDocumentTypes:#[#"com.domain.file", #"com.domain.app.file", #"iCloud.com.domain.file", #"iCloud.com.domain.app.file" ] inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
}
If I add #"public.text" to the initWithDocumentTypes array, then the iCloud Drive Locations picker screen displays as expected. I can select .txt and .rtf files, but my custom file types are grayed out and not selectable.
Note:
I have not created a Document Provider extension, as I don't believe this is required, and my file format it not common.
I see the following warning when I take any action on UIDocumentPickerViewController, even if it's Cancel and even if the action on a file (i.e. save .txt) works. I've spent quite a bit of time just trying to track down the source of this warning, to no avail.
plugin com.apple.UIKit.fileprovider.default invalidated
I had this issue and addressed it by having my custom exported UTI conform to the 'public.data' UTI.
You can set this by selecting your target and then the info tab. Scroll to the Exported UTIs section and expand it. Under your custom UTI there is a box to declare that it conforms.
See here for a list of system defined UTIs:
https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html

send audio with MFMessageComposeViewController

does anyone know i this is possible by doing something like
[compose addAttachmentData:data typeIdentifier:(NSString *)kUTTypeAudio filename:#"test"];
The resulting message isn't of any playable format so not sure if its possible at all? The data file does play in an audio player so I know thats correct.
Thanks
Jules
You need to set the filename to include a proper file extension, e.g. #"test.caf", instead of just #"test". See this answer for a similar question:
MFMessageComposeViewController iOS7 addAttachmentData:typeIdentifier:filename: not working

Hide String in UIActivityViewController from User

I would like to pass a string to a UIActivityViewController but hide it from the user when they publish a tweet or share on Facebook.
In the free version of my app, the added hidden string will be 'Download my app now!', whereas in the paid version (via in-app purchase), this string will not be published.
In the free version, the user must not be able to delete the added string. But I can't find a way to hide it from them. So when they share, that pop-up appears and the string is included, editable. This is my code so far:
NSString *advertString = #"Download my app now!";
UIActivityViewController *shareController =
[[UIActivityViewController alloc]
initWithActivityItems:#[scoreString, advertString, url, image]
applicationActivities:nil];
How would I go about doing this? Or are there alternatives that would allow me to do this? Thanks for any help!
You can provide a subject for your email by implementing
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType;
in a class that conforms to theUIActivityItemSource protocol. See the documentation for more information.
Edited
You need to customize this by using different string on different sharing option.
Read this Tutorial, this will work.
Thanks.

Can I send UIDocuments by email, as an attachment?

I'm working with Xcode.
In my app I save some UIdocuments at that location
[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
I'm searching for a way to share documents, my first option is by email.
Can I send those documents by email, as an attachment? Can I open then with another device with the same app?
You could do like the following.
Create a MFMailComposeViewController and use - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename method to add your attachment.
For example.
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[mailVC setSubject:#"Shared documents"];
[mailVC setToRecipients:#[#"sample#example.com"]];
[mailVC setMessageBody:#"Here the docs I want to share" isHTML:NO];
[mailComposer addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"file.pdf"];
[mailVC setMailComposeDelegate:self];
[self presentViewController:mailVC animated:YES completion:nil];
where pdfData is of type NSData. So, you need to transform your document into a NSData.
From Apple doc.
addAttachmentData:mimeType:fileName:
This method attaches the specified data after the message body but
before the user’s signature. You may attach multiple files (using
different file names) but must do so prior to displaying the mail
composition interface. Do not call this method after presenting the
interface to the user.
About the second part of your question. Could you explain what type of document do you need to display?
In the meantime, take a look at Adding "Open In..." option to iOS app.
To send any attachment you need to get the contents into an NSData object. If the document is on disk then this is simple. You just need the path or file URL to the document. Then you can create the NSData object using the path or URL.
If the receiver of the email has the same app and the app is setup to appear in the "Open In" menu for documents of this type, then the user can open the app from the attachment. Your app then just needs to know what to do when it is asked to open a file of this type. There are plenty of existing documentation and questions here on SO that describe how to register an app to open certain file types.

Get copied data from UIPasteboard

i have copied image from UIwebView using clipboard and i want to mail it.For this,I use general pasteboard to get data,but there is a problem in retrieving data.When i check the pasteboard current data,it says the it has Apple Web Archive pasteboard type data,how to read this.here is my code of retriving text.
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
NSArray* array = [pasteboard pasteboardTypes];
for (NSString* type in array) {
NSLog(#"%#",type);
}
NSString* item = #"Apple Web Archive pasteboard type";
NSData* val = [pasteboard dataForPasteboardType:item];
I tried to create a UIImage using this data but that didn't work.
I don't understand what you mean by mail it? You can paste the webpage image copy right into the mail app and it will appear as an image.
You can rebuild the data from the Apple Web Archive pasteboard type if you need to manual. It is essentially a XML document with html and the actual image data all within. The html and accompanying images are base64 encoded. If you want to look at an archive example save this, or perhaps a simple webpage in safari as an archive. Open the archive file in something like Text wrangler. Text edit will probably try to render it.
I've written a post on how to make an Apple Web Archive pasteboard type that might help you understand the process.
http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/
I take it you are trying to mail it from within your app and not using the mail app?
If this is the case you will probably have to get the xml from the pasteboard, find the tag that holds the encoded image data, decode it and create an image from the decoded data.

Resources