-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiShare.m
109 lines (98 loc) · 4.41 KB
/
iShare.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// iShare.m
// iShare
//
// Created by Harikrishnan T on 10/07/14.
// Copyright (c) 2014 FyrWeel. All rights reserved.
//
#import "iShare.h"
@implementation iShare
-(void)initialize{
self.title = NSLocalizedString(@"Share Via", Nil);
self.delegate = self;
if ([MFMailComposeViewController canSendMail]) {
[self addButtonWithTitle:NSLocalizedString(@"Email", Nil)];
}
if ([MFMessageComposeViewController canSendText]) {
[self addButtonWithTitle:NSLocalizedString(@"SMS", Nil)];
}
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
[self addButtonWithTitle:NSLocalizedString(@"Facebook", Nil)];
}
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
[self addButtonWithTitle:NSLocalizedString(@"Twitter", Nil)];
}
self.cancelButtonIndex = [self addButtonWithTitle:NSLocalizedString(@"Cancel", Nil)];
self.destructiveButtonIndex = 0;
}
-(id)init{
self = [super init];
if (self) {
[self initialize];
}
return self;
}
-(void)composeMail {
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[mailer setSubject:contentSubject];
NSData *body = [shareContent dataUsingEncoding:NSUTF8StringEncoding];
NSData *myData = UIImagePNGRepresentation(shareImage);
[mailer addAttachmentData:myData mimeType:@"image/png" fileName:@"ShareImage.png"];
NSString *emailBody = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
[mailer setMessageBody:emailBody isHTML:NO];
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appdelegate.window.rootViewController presentViewController:mailer animated:YES completion:nil];
}
}
-(void)sendSms {
if([MFMessageComposeViewController canSendText])
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.body = shareContent;
controller.messageComposeDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appdelegate.window.rootViewController presentViewController:controller animated:YES completion:nil];
}
}
- (void) setShareSubject:(NSString *)subject andContent:(NSString *)content withImage:(UIImage *)image {
shareSubject = subject;
shareContent = content;
shareImage = image;
}
- (void)shareToFb {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[slComposeViewController setInitialText:shareContent];
[slComposeViewController addImage:shareImage];
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appdelegate.window.rootViewController presentViewController:slComposeViewController animated:YES completion:nil];
}
}
- (void)shareToTwitter {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slComposeViewController setInitialText:shareContent];
[slComposeViewController addImage:shareImage];
AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appdelegate.window.rootViewController presentViewController:slComposeViewController animated:YES completion:nil];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:NSLocalizedString(@"Email", Nil)]) {
[self composeMail];
}
else if ([buttonTitle isEqualToString:NSLocalizedString(@"SMS", Nil)]) {
[self sendSms];
}
else if ([buttonTitle isEqualToString:NSLocalizedString(@"Facebook", Nil)]) {
[self shareToFb];
}
else if ([buttonTitle isEqualToString:NSLocalizedString(@"Twitter", Nil)]) {
[self shareToTwitter];
}
}
@end