Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

ios - MFMessageComposeViewController iOS7 addAttachmentData:typeIdentifier:filename: not working

I want to attach an image to a MMS, on iOS7. I wrote following code:

    MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
    messageController.messageComposeDelegate = self;

    NSData *imgData = [NSData dataWithContentsOfFile:@"blablabla"];
    BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:@"public.data" filename:@"image"];

    if (didAttachImage)
    {
        // Present message view controller on screen
        [self presentViewController:messageController animated:YES completion:nil];
    }
    else
    {
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                               message:@"Failed to attach image"
                                                              delegate:nil
                                                     cancelButtonTitle:@"OK"
                                                     otherButtonTitles:nil];
        [warningAlert show];
        return;
    }

The problem is that when the SMS screen is presented, it doesn't recognize the image, and cannot send it. I see something like this:

Message Screen

I believe this has something to do either with imgData I am sending, or with typeIdentifier.

Note: I tried almost all possible typeIdentifiers: @"public.data", @"public.image", @"public.item", ... etc. None worked.

Can anybody please help me? What is the typeIdentifier you are using? I am testing on iPhone 5, iOS 7.0.2.

Thanks.


SOLUTION:

As Greg instructed, this solved my problem: set filename as @"image.png", and typeIdentifier to kUTTypePNG.

[messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:@"image.png"];

Thanks Greg.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

The full list of UTIs is available here: System-Declared Uniform Type Identifiers (Thanks @iWasRobbed!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...