Questo piccolo snippet di codice \u00e8 di gran lunga, e a mio avviso, il modo pi\u00f9 veloce per inviare una mail tramite le API standard fornita dalla di iOS SDK (7+). Sicuramente mi ha aiutato risparmiandomi un sacco di tempo quando arrivavano richieste tipo “Hey possiamo fare questo email link clickabile?”. GRRR!<\/p>\n
1.Pima di tutto importiamo la MessagUI come segue<\/p>\n
[objc]
\n#import <MessageUI\/MessageUI.h>
\n[\/objc]<\/p>\n
2.Aggiungiamo il codice alla nostra classe, in modo che implementi il delegato seguente<\/p>\n
[objc]MFMailComposeViewControllerDelegate[\/objc]<\/p>\n
[objc]@property (strong, nonatomic)
\nMFMailComposeViewController *mailer;[\/objc]<\/p>\n
[objc]MFMailComposeViewController[\/objc]<\/p>\n
e comporre la mail<\/p>\n
[objc]
\n-(void) sendMailTo: (NSString *) recipient {
\n self.mailer = [[MFMailComposeViewController alloc] init];
\nself.mailer.mailComposeDelegate = self;<\/p>\n
[self.mailer setSubject:@”My Fabolous Subject”];<\/p>\n
NSArray *toRecipients = [NSArray arrayWithObjects:recipient, nil];
\n[self.mailer setToRecipients:toRecipients];<\/p>\n
\/* Potete scommentare il codice seguente, se avete
\n* immagini da inserire come attachments *\/
\n\/\/ UIImage *myImage = [UIImage imageNamed:@”myfabolousimage.png”];
\n\/\/ NSData *imageData = UIImagePNGRepresentation(myImage);
\n\/\/ [self.mailer addAttachmentData:imageData
\n\/\/ mimeType:@”image\/png” fileName:@”myfabolousimage.png”];<\/p>\n
NSString *emailBody = @””;
\n[self.mailer setMessageBody:emailBody isHTML:NO];<\/p>\n
[self presentViewController:self.mailer animated:YES completion:nil];<\/p>\n
}
\n[\/objc]<\/p>\n
[objc]MFMailComposeViewControllerDelegate[\/objc]<\/p>\n
indietro, cos\u00ec, andremo ad implementare il metodo delegato come segue, in modo da avere il pieno controllo dei valori di ritorno del mail controller, dopo che ha finito il suo lavoro. Aggiungete questo metodo alla classe:<\/p>\n
[objc]-(void)mailComposeController:
\n(MFMailComposeViewController *)controller
\ndidFinishWithResult:(MFMailComposeResult)result
\nerror:(NSError *)error {
\nif (error) {<\/p>\n
} else {
\n[self.mailer dismissViewControllerAnimated:YES completion:nil];
\n}
\n}
\n[\/objc]<\/p>\n
Questo piccolo snippet di codice \u00e8 di gran lunga, e a mio avviso, il modo pi\u00f9 veloce per inviare una mail tramite le API standard fornita dalla di iOS SDK (7+). Sicuramente mi ha aiutato risparmiandomi un sacco di tempo quando arrivavano richieste tipo “Hey possiamo fare questo email link clickabile?”. GRRR! 1.Pima di tutto […]<\/p>\n","protected":false},"author":1,"featured_media":7926,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"footnotes":""},"categories":[57],"tags":[],"class_list":["post-23664","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sviluppo-software"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/posts\/23664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/comments?post=23664"}],"version-history":[{"count":1,"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/posts\/23664\/revisions"}],"predecessor-version":[{"id":24314,"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/posts\/23664\/revisions\/24314"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/media\/7926"}],"wp:attachment":[{"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/media?parent=23664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/categories?post=23664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/odc.oimmei.dev\/it\/wp-json\/wp\/v2\/tags?post=23664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}