2010-08-26 12 views
8

Quiero enviar mensajes con datos de imagen. Entonces usé MFMessageComposeViewController. Pero ese controlador proporciona solo el servicio de SMS. Así que utilicé UIPasteBoard adjunto una imagen de datos. Pero tampoco funciona. No hay un botón "Pegar" creado al escribir mensajes. Adjuntar imagen al UIPasteBoard fue claramente un éxito. Creo que el uso de MFMessageComposeViewController no resuelve mi problema. ¿Cómo puedo lograr mi objetivo?Cómo adjuntar una imagen con un mensaje a través de la aplicación de iPhone?

+0

Realice la url Sms y luego use [[UIApplication sharedApplication] openURL: url]; Para enviar imágenes, debe pegar manualmente la imagen usando UIPasteBoard. – Jasmit

Respuesta

6

Esto no es posible con la API MessageUI actual: la MSMessageComposeViewController no acepta archivos adjuntos como el MFMailComposeViewController hace.

La única manera de hacer esto actualmente es utilizar un servicio externo que le permite enviar MMS a través de una llamada REST por ejemplo.

GSMA define una especificación REST precisamente para este propósito: http://www.gsmworld.com/oneapi/reference_documentation-version_1.html (múltiples archivos PDF en esta página)

tratar de encontrar un proveedor de servicio local que implementa esta especificación y ya está bueno para ir.

Solo para agregar el enlace wiki directo a la especificación OneAPI MMS: http://gsma.securespsite.com/access/Access%20API%20Wiki/MMS%20RESTful%20API.aspx y un enlace a la caja de arena PHP/Java https://github.com/OneAPI/GSMA-OneAPI donde MMS se puede probar localmente. Aclamaciones.

4

que tenían la misma pregunta que he publicado here. Hay un error en MFMessageComposeViewController y si sólo tiene que utilizar el código de abajo que lanzará un mensaje que se puede insertar imágenes en

NSString *phoneToCall = @"sms: 123-456-7890"; 
    NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded]; 

    [[UIApplication sharedApplication] openURL:url]; 
+1

aún más simple, al menos en un iPhone 4S, puede eliminar el número de teléfono falso y la codificación; todo lo que necesita es esto: 'NSURL * url = [[NSURL alloc] initWithString: @" sms: "];' then ' [[UIApplication sharedApplication] openURL: url]; ' – toblerpwn

4

Aquí está el correcto funcionamiento código y está funcionando perfectamente en mi dispositivo.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; 
pasteboard.persistent = NO; 

NSMutableDictionary *text = [NSMutableDictionary dictionaryWithCapacity:1]; 
[text setValue:label.text forKey:(NSString *)kUTTypeUTF8PlainText]; 

NSMutableDictionary *image = [NSMutableDictionary dictionaryWithCapacity:1]; 
[image setValue:imageView.image forKey:(NSString *)kUTTypePNG]; 

pasteboard.items = [NSArray arrayWithObjects:image,text, nil]; 

NSString *phoneToCall = @"sms:"; 
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded]; 
[[UIApplication sharedApplication] openURL:url]; 
+0

lo que es kUTTypeUTF8PlainText y kUTTypePNG? – Rick

+0

@Rick Ver este [enlace] (https://developer.apple.com/library/mac/documentation/MobileCoreServices/Reference/UTTypeRef/Reference/reference.html) –

+0

No funciona a partir de iOS 7 –

4

este método es probado y verificado. Lo usé en mi código.

if (![MFMessageComposeViewController canSendText]) { 
    UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device not support SMS \nOr you hadn't login your iMessage" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
    [alertV show]; 
    return; 
} 

MFMessageComposeViewController *mVC = [[MFMessageComposeViewController alloc] init]; 
mVC.body = @"jjjj"; 
mVC.recipients = @[@"00XXXXXXXXXX"]; 
mVC.messageComposeDelegate = self; 
if ([MFMessageComposeViewController canSendAttachments]) { 
    NSLog(@"ok"); 
} 
[mVC addAttachmentData: UIImageJPEGRepresentation([UIImage imageNamed:@"test.jpg"], 1.0) typeIdentifier:@"public.data" filename:@"image.jpeg"]; 

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

Puede usar cualquier formato jpeg jpg y png.

+0

en mi código nunca entra en if ([MFMessageComposeViewController canSendAttachments]) { NSLog (@ "ok"); } ¿Debo configurar otras configuraciones? – Niharika

0

¿Por qué no compartir la imagen y texto a través de la API Compartir (selección de mensaje, y si quieres exluding Facebook, Twitter, etc ..)

+0

Para tener un aspecto diferente – jgvb

1

manera Swift. Funciona en iOS11

func shareViaMessage() { 
    if !MFMessageComposeViewController.canSendText() { 
     showAlert("Text services are not available") 
     return 
    } 

    let textComposer = MFMessageComposeViewController() 
    textComposer.messageComposeDelegate = self 
    textComposer.body = "Try my #app" 

    if MFMessageComposeViewController.canSendSubject() { 
     textComposer.subject = "AppName" 
    } 

    if MFMessageComposeViewController.canSendAttachments() { 
     let imageData = UIImageJPEGRepresentation(imageView.image!, 1.0) 
     textComposer.addAttachmentData(imageData!, typeIdentifier: "image/jpg", filename: "photo.jpg") 
    } 

    present(textComposer, animated: true) 
} 
Cuestiones relacionadas