Search code examples
iosios7ios8

How to send image via mms in ios 7+ programmatically?


How to send image via mms in ios 7+ programmatically ? I have image name saved locally and I need to sendvia mms. Does ios support this ?


Solution

  • You can approach this by two ways, 1 - By using MFMessageComposeViewController 2 - By MMS

    In the first way you can send the image via iMessage In the second way you can send MMS via Career network

    For 1st process the code is

    -(void)sendSMSto:(NSString *)number withImage:(UIImage *)sentImage{
     MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    
    if([MFMessageComposeViewController canSendText]) {
        NSMutableString *messageBody = [[NSMutableString alloc] initWithString:@""];
        picker.messageComposeDelegate = self;
        picker.recipients = number?[NSArray arrayWithObject:number]:nil;// your recipient number or self for testing
        [picker setBody:messageBody];
    
        if ([picker respondsToSelector:@selector(addAttachmentData:typeIdentifier:filename:)]) {
            NSData *imageData = UIImagePNGRepresentation(sentImage);
            [picker addAttachmentData:imageData typeIdentifier:(@"public.image") filename:@"emoji.png"];
        }
    
        picker.body = messageBody;
        ELogs(@"Picker -- %@",picker.body);
        [self presentViewController:picker animated:YES completion:^{
           ELogs(@"SMS fired");
        }];
    }
    }
    

    For second approach use the UIPasteboard to copy the image and then paste it in the MMS screen

    the code is

    -(void)sendSMSto:(NSString *)number withImage:(UIImage *)sentImage{
    if (sentImage) {
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.persistent = YES;
        pasteboard.image = sentImage;
    }
    
    //For sms through network career
    NSString *phoneToCall = @"sms:";
    NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
    [[UIApplication sharedApplication] openURL:url];
    
    }
    

    Please accept the answer, if you find this useful