Skip to content

Commit

Permalink
add: email share
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanFuentealba committed Jul 27, 2016
1 parent 97fb130 commit 1faeac7
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 12 deletions.
21 changes: 21 additions & 0 deletions ios/EmailShare.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// FacebookShare.h
// RNShare
//
// Created by Diseño Uno BBCL on 23-07-16.
// Copyright © 2016 Facebook. All rights reserved.
//


#import <Social/Social.h>
#import <UIKit/UIKit.h>
#import "RCTConvert.h"
#import "RCTBridge.h"
#import "RCTUIManager.h"
#import "RCTLog.h"
#import "RCTUtils.h"
#import <MessageUI/MessageUI.h>
@interface EmailShare : NSObject <MFMailComposeViewControllerDelegate>

- (void *) shareSingle:(NSDictionary *)options failureCallback:(RCTResponseErrorBlock)failureCallback successCallback:(RCTResponseSenderBlock)successCallback;
@end
72 changes: 72 additions & 0 deletions ios/EmailShare.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// FacebookShare.m
// RNShare
//
// Created by Diseño Uno BBCL on 23-07-16.
// Copyright © 2016 Facebook. All rights reserved.
//

#import "EmailShare.h"


@implementation EmailShare
- (void)shareSingle:(NSDictionary *)options
failureCallback:(RCTResponseErrorBlock)failureCallback
successCallback:(RCTResponseSenderBlock)successCallback {

NSLog(@"Try open view");

if ([options objectForKey:@"message"] && [options objectForKey:@"message"] != [NSNull null]) {
NSString *text = [RCTConvert NSString:options[@"message"]];

if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
[mailController setMailComposeDelegate:self];
if ([options objectForKey:@"subject"] && [options objectForKey:@"subject"] != [NSNull null]) {
[mailController setSubject: options[@"subject"]];
}
if ([options objectForKey:@"message"] && [options objectForKey:@"message"] != [NSNull null]) {
[mailController setMessageBody:options[@"message"] isHTML:NO];
}

NSURL *URL = [RCTConvert NSURL:options[@"url"]];
if (URL) {
if (URL.fileURL || [URL.scheme.lowercaseString isEqualToString:@"data"]) {
NSError *error;
NSData *data = [NSData dataWithContentsOfURL:URL
options:(NSDataReadingOptions)0
error:&error];
if (!data) {
failureCallback(error);
return;
}
// ONLY images attach
time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
NSString *timestamp=[NSString stringWithFormat:@"%ld",unixTime];
[mailController addAttachmentData:data mimeType: @"image/jpg" fileName: [timestamp stringByAppendingString: @".jpg"] ];

} else {
[mailController setMessageBody:[options[@"message"] stringByAppendingString: [@" " stringByAppendingString: options[@"url"]]] isHTML:NO];
}
}

UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController];


[root presentViewController:mailController animated:YES completion:nil];


}

}

}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
NSLog(@"finish email");
[controller becomeFirstResponder];
[controller dismissViewControllerAnimated:YES completion:nil];
}


@end
16 changes: 11 additions & 5 deletions ios/RNShare.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import "RCTUIManager.h"
#import "GenericShare.h"
#import "WhatsAppShare.h"
#import "EmailShare.h"

@implementation RNShare
- (dispatch_queue_t)methodQueue
Expand All @@ -25,19 +26,24 @@ - (dispatch_queue_t)methodQueue
NSLog(social);
if([social isEqualToString:@"facebook"]) {
NSLog(@"TRY OPEN FACEBOOK");
GenericShare *shareCtl = [GenericShare new];
GenericShare *shareCtl = [[GenericShare alloc] init];
[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback serviceType: SLServiceTypeFacebook];
} else if([social isEqualToString:@"twitter"]) {
NSLog(@"TRY OPEN Twitter");
GenericShare *shareCtl = [GenericShare new];
GenericShare *shareCtl = [[GenericShare alloc] init];
[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback serviceType: SLServiceTypeTwitter];
} else if([social isEqualToString:@"googleplus"]) {
NSLog(@"TRY OPEN google plus");
GenericShare *shareCtl = [GenericShare new];
GenericShare *shareCtl = [[GenericShare alloc] init];
//[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback serviceType: SLServiceType];
} else if([social isEqualToString:@"whatsapp"]) {
NSLog(@"TRY OPEN google whatsapp");
WhatsAppShare *shareCtl = [WhatsAppShare new];
NSLog(@"TRY OPEN whatsapp");

WhatsAppShare *shareCtl = [[WhatsAppShare alloc] init];
[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback];
} else if([social isEqualToString:@"email"]) {
NSLog(@"TRY OPEN email");
EmailShare *shareCtl = [[EmailShare alloc] init];
[shareCtl shareSingle:options failureCallback: failureCallback successCallback: successCallback];
}
} else {
Expand Down
6 changes: 6 additions & 0 deletions ios/RNShare.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
B33C22851D441713006BCD98 /* GenericShare.m in Sources */ = {isa = PBXBuildFile; fileRef = B33C22841D441713006BCD98 /* GenericShare.m */; };
B33C22891D4419E8006BCD98 /* WhatsAppShare.m in Sources */ = {isa = PBXBuildFile; fileRef = B33C22881D4419E8006BCD98 /* WhatsAppShare.m */; };
B33C228B1D442577006BCD98 /* EmailShare.m in Sources */ = {isa = PBXBuildFile; fileRef = B33C228A1D442576006BCD98 /* EmailShare.m */; };
B3E7B58A1CC2AC0600A0062D /* RNShare.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RNShare.m */; };
/* End PBXBuildFile section */

Expand All @@ -30,6 +31,8 @@
B33C22861D441723006BCD98 /* GenericShare.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GenericShare.h; sourceTree = "<group>"; };
B33C22871D4419DA006BCD98 /* WhatsAppShare.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WhatsAppShare.h; sourceTree = "<group>"; };
B33C22881D4419E8006BCD98 /* WhatsAppShare.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WhatsAppShare.m; sourceTree = "<group>"; };
B33C228A1D442576006BCD98 /* EmailShare.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EmailShare.m; sourceTree = "<group>"; };
B33C228C1D442583006BCD98 /* EmailShare.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EmailShare.h; sourceTree = "<group>"; };
B3E7B5881CC2AC0600A0062D /* RNShare.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNShare.h; sourceTree = "<group>"; };
B3E7B5891CC2AC0600A0062D /* RNShare.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNShare.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -70,6 +73,8 @@
B33C22861D441723006BCD98 /* GenericShare.h */,
B33C22871D4419DA006BCD98 /* WhatsAppShare.h */,
B33C22881D4419E8006BCD98 /* WhatsAppShare.m */,
B33C228A1D442576006BCD98 /* EmailShare.m */,
B33C228C1D442583006BCD98 /* EmailShare.h */,
);
name = social;
sourceTree = "<group>";
Expand Down Expand Up @@ -132,6 +137,7 @@
files = (
B3E7B58A1CC2AC0600A0062D /* RNShare.m in Sources */,
B33C22891D4419E8006BCD98 /* WhatsAppShare.m in Sources */,
B33C228B1D442577006BCD98 /* EmailShare.m in Sources */,
B33C22851D441713006BCD98 /* GenericShare.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
7 changes: 0 additions & 7 deletions ios/WhatsAppShare.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ - (void)shareSingle:(NSDictionary *)options

if ([options objectForKey:@"message"] && [options objectForKey:@"message"] != [NSNull null]) {
NSString *text = [RCTConvert NSString:options[@"message"]];

NSURL *URL = [RCTConvert NSURL:options[@"url"]];


NSString * urlWhats = [NSString stringWithFormat:@"whatsapp://send?text=%@", [text stringByAppendingString: [@" " stringByAppendingString: options[@"url"]] ]];
NSURL * whatsappURL = [NSURL URLWithString:[urlWhats stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Expand All @@ -31,9 +27,6 @@ - (void)shareSingle:(NSDictionary *)options
}
}




}


Expand Down

0 comments on commit 1faeac7

Please sign in to comment.