Permalink
Browse files

Attachments for messages.

  • Loading branch information...
1 parent 56c7853 commit 915bd32a12fb05fb95a79d620ce5f435399fec67 @skylarsch committed Dec 15, 2012
@@ -18,6 +18,8 @@
9408340C167BC42A0034BE0E /* Default@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9408340B167BC42A0034BE0E /* Default@2x.png */; };
9408340E167BC42A0034BE0E /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 9408340D167BC42A0034BE0E /* Default-568h@2x.png */; };
94083415167BC4380034BE0E /* libSSPostmark.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 940833DE167BC3E80034BE0E /* libSSPostmark.a */; };
+ 948C4C29167D32F100986B57 /* SSPostmarkAttachment.m in Sources */ = {isa = PBXBuildFile; fileRef = 948C4C28167D32F100986B57 /* SSPostmarkAttachment.m */; };
+ 948C4C2C167D398C00986B57 /* sunset.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 948C4C2B167D398C00986B57 /* sunset.jpg */; };
94DEB660167C4AC200823F00 /* SSPostmark.m in Sources */ = {isa = PBXBuildFile; fileRef = 94DEB65F167C4AC200823F00 /* SSPostmark.m */; };
94DEB663167C4C6B00823F00 /* SSPostmarkMessage.m in Sources */ = {isa = PBXBuildFile; fileRef = 94DEB662167C4C6B00823F00 /* SSPostmarkMessage.m */; };
/* End PBXBuildFile section */
@@ -60,6 +62,9 @@
94083409167BC42A0034BE0E /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = "<group>"; };
9408340B167BC42A0034BE0E /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default@2x.png"; sourceTree = "<group>"; };
9408340D167BC42A0034BE0E /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
+ 948C4C27167D32F100986B57 /* SSPostmarkAttachment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSPostmarkAttachment.h; sourceTree = "<group>"; };
+ 948C4C28167D32F100986B57 /* SSPostmarkAttachment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSPostmarkAttachment.m; sourceTree = "<group>"; };
+ 948C4C2B167D398C00986B57 /* sunset.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = sunset.jpg; sourceTree = "<group>"; };
94DEB65E167C4AC200823F00 /* SSPostmark.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSPostmark.h; sourceTree = "<group>"; };
94DEB65F167C4AC200823F00 /* SSPostmark.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SSPostmark.m; sourceTree = "<group>"; };
94DEB661167C4C6B00823F00 /* SSPostmarkMessage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SSPostmarkMessage.h; sourceTree = "<group>"; };
@@ -125,6 +130,8 @@
94DEB65F167C4AC200823F00 /* SSPostmark.m */,
94DEB661167C4C6B00823F00 /* SSPostmarkMessage.h */,
94DEB662167C4C6B00823F00 /* SSPostmarkMessage.m */,
+ 948C4C27167D32F100986B57 /* SSPostmarkAttachment.h */,
+ 948C4C28167D32F100986B57 /* SSPostmarkAttachment.m */,
940833E4167BC3E80034BE0E /* Supporting Files */,
);
path = SSPostmark;
@@ -141,6 +148,7 @@
940833FD167BC42A0034BE0E /* SSPostmarkTestApp */ = {
isa = PBXGroup;
children = (
+ 948C4C2B167D398C00986B57 /* sunset.jpg */,
94083406167BC42A0034BE0E /* SSTAAppDelegate.h */,
94083407167BC42A0034BE0E /* SSTAAppDelegate.m */,
940833FE167BC42A0034BE0E /* Supporting Files */,
@@ -236,6 +244,7 @@
9408340A167BC42A0034BE0E /* Default.png in Resources */,
9408340C167BC42A0034BE0E /* Default@2x.png in Resources */,
9408340E167BC42A0034BE0E /* Default-568h@2x.png in Resources */,
+ 948C4C2C167D398C00986B57 /* sunset.jpg in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -248,6 +257,7 @@
files = (
94DEB660167C4AC200823F00 /* SSPostmark.m in Sources */,
94DEB663167C4C6B00823F00 /* SSPostmarkMessage.m in Sources */,
+ 948C4C29167D32F100986B57 /* SSPostmarkAttachment.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -0,0 +1,64 @@
+// SSPostmarkAttachment.h
+//
+// Copyright (c) 2012 Skylar Schipper
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer. Redistributions in binary
+// form must reproduce the above copyright notice, this list of conditions and
+// the following disclaimer in the documentation and/or other materials
+// provided with the distribution. Neither the name of the nor the names of
+// its contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#import <Foundation/Foundation.h>
+
+NSString static const *kSSPostmarkAttachmentName = @"Name";
+NSString static const *kSSPostmarkAttachmentContent = @"Content";
+NSString static const *kSSPostmarkAttachmentContentType = @"ContentType";
+
+@interface SSPostmarkAttachment : NSObject
+
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+@property (nonatomic, strong) NSString *name;
+
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+@property (nonatomic, strong) NSData *content;
+
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+@property (nonatomic, strong) NSString *contentType;
+
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+- (NSDictionary *)dictionary;
+
+@end
@@ -0,0 +1,96 @@
+// SSPostmarkAttachment.m
+//
+// Copyright (c) 2012 Skylar Schipper
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer. Redistributions in binary
+// form must reproduce the above copyright notice, this list of conditions and
+// the following disclaimer in the documentation and/or other materials
+// provided with the distribution. Neither the name of the nor the names of
+// its contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#import "SSPostmarkAttachment.h"
+
+@implementation SSPostmarkAttachment
+
+#pragma mark -
+#pragma mark -
+- (NSString *)contentType {
+ if (_contentType == nil || [_contentType isEqualToString:@""]) {
+ return @"application/octet-stream";
+ }
+ return _contentType;
+}
+
+#pragma mark -
+#pragma mark -
+- (NSString *)base64Body {
+ if (self.content == nil) {
+ return @"";
+ }
+ static const char _base64EncodingTable[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+ const unsigned char * rawData = [self.content bytes];
+ char * objPointer;
+ char * strResult;
+
+ NSUInteger intLength = [self.content length];
+ if (intLength == 0)
+ return nil;
+
+ strResult = (char *)calloc(((intLength + 2) / 3) * 4, sizeof(char));
+ objPointer = strResult;
+
+ while (intLength > 2) {
+ *objPointer++ = _base64EncodingTable[rawData[0] >> 2];
+ *objPointer++ = _base64EncodingTable[((rawData[0] & 0x03) << 4) + (rawData[1] >> 4)];
+ *objPointer++ = _base64EncodingTable[((rawData[1] & 0x0f) << 2) + (rawData[2] >> 6)];
+ *objPointer++ = _base64EncodingTable[rawData[2] & 0x3f];
+ rawData += 3;
+ intLength -= 3;
+ }
+
+ if (intLength != 0) {
+ *objPointer++ = _base64EncodingTable[rawData[0] >> 2];
+ if (intLength > 1) {
+ *objPointer++ = _base64EncodingTable[((rawData[0] & 0x03) << 4) + (rawData[1] >> 4)];
+ *objPointer++ = _base64EncodingTable[(rawData[1] & 0x0f) << 2];
+ *objPointer++ = '=';
+ } else {
+ *objPointer++ = _base64EncodingTable[(rawData[0] & 0x03) << 4];
+ *objPointer++ = '=';
+ *objPointer++ = '=';
+ }
+ }
+ *objPointer = '\0';
+ NSString *retVal = [[NSString alloc] initWithCString:strResult encoding:NSASCIIStringEncoding];
+ free(strResult);
+ return retVal;
+}
+
+#pragma mark -
+#pragma mark -
+- (NSDictionary *)dictionary {
+ return (@{
+ kSSPostmarkAttachmentContent : [self base64Body],
+ kSSPostmarkAttachmentContentType : self.contentType,
+ kSSPostmarkAttachmentName : self.name
+ });
+}
+
+@end
@@ -27,16 +27,20 @@
#import <Foundation/Foundation.h>
-NSString static const *kSSPostmarkHTMLBody = @"HtmlBody";
-NSString static const *kSSPostmarkTextBody = @"TextBody";
-NSString static const *kSSPostmarkFrom = @"From";
-NSString static const *kSSPostmarkTo = @"To";
-NSString static const *kSSPostmarkCC = @"Cc";
-NSString static const *kSSPostmarkBCC = @"Bcc";
-NSString static const *kSSPostmarkSubject = @"Subject";
-NSString static const *kSSPostmarkTag = @"Tag";
-NSString static const *kSSPostmarkReplyTo = @"ReplyTo";
-NSString static const *kSSPostmarkHeaders = @"Headers";
+NSString static const *kSSPostmarkHTMLBody = @"HtmlBody";
+NSString static const *kSSPostmarkTextBody = @"TextBody";
+NSString static const *kSSPostmarkFrom = @"From";
+NSString static const *kSSPostmarkTo = @"To";
+NSString static const *kSSPostmarkCC = @"Cc";
+NSString static const *kSSPostmarkBCC = @"Bcc";
+NSString static const *kSSPostmarkSubject = @"Subject";
+NSString static const *kSSPostmarkTag = @"Tag";
+NSString static const *kSSPostmarkReplyTo = @"ReplyTo";
+NSString static const *kSSPostmarkHeaders = @"Headers";
+NSString static const *kSSPostmarkAttachments = @"Attachments";
+
+
+@class SSPostmarkAttachment;
/** <#Description#>
@@ -115,12 +119,43 @@ NSString static const *kSSPostmarkHeaders = @"Headers";
*/
@property (nonatomic, retain) NSDictionary *headers;
+
+#pragma mark - Attachments
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+@property (nonatomic, retain) NSMutableSet *attachments;
+
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+- (void)addAttachments:(NSSet *)objects;
+
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+- (void)addAttachmentsObject:(SSPostmarkAttachment *)object;
+
/** <#Description#>
<#Discussion#>
*/
-//@property (nonatomic, retain) NSArray *attachments;
+- (void)removeAttachments:(NSSet *)objects;
+
+/** <#Description#>
+
+ <#Discussion#>
+
+ */
+- (void)removeAttachmentsObject:(SSPostmarkAttachment *)object;
+
#pragma mark - Setup the request
/** <#Description#>
@@ -26,6 +26,8 @@
// POSSIBILITY OF SUCH DAMAGE.
#import "SSPostmarkMessage.h"
+#import "SSPostmarkAttachment.h"
+
@implementation SSPostmarkMessage
@@ -60,7 +62,38 @@ - (NSDictionary *)dictionaryRepresentation {
if (self.textBody)
[d setObject:self.textBody forKey:kSSPostmarkTextBody];
+ if (self.attachments != nil && [self.attachments count] > 0) {
+ NSMutableArray *attachments = [NSMutableArray new];
+ for (SSPostmarkAttachment *att in [self.attachments allObjects]) {
+ [attachments addObject:[att dictionary]];
+ }
+ [d setObject:attachments forKey:kSSPostmarkAttachments];
+ }
+
return d;
}
+#pragma mark -
+#pragma mark - Attachments
+- (void)addAttachmentsObject:(SSPostmarkAttachment *)object {
+ [self addAttachments:[NSSet setWithObject:object]];
+}
+- (void)addAttachments:(NSSet *)objects {
+ if (self.attachments == nil) {
+ self.attachments = [NSMutableSet set];
+ }
+ [self.attachments addObjectsFromArray:[objects allObjects]];
+}
+- (void)removeAttachmentsObject:(SSPostmarkAttachment *)object {
+ [self removeAttachments:[NSSet setWithObject:object]];
+}
+- (void)removeAttachments:(NSSet *)objects {
+ if (self.attachments == nil) {
+ return;
+ }
+ for (id obj in [objects allObjects]) {
+ [self.attachments removeObject:obj];
+ }
+}
+
@end
@@ -8,6 +8,7 @@
#import "SSTAAppDelegate.h"
#import "SSPostmark.h"
+#import "SSPostmarkAttachment.h"
@implementation SSTAAppDelegate
@@ -29,7 +30,14 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
message.subject = @"Just a test";
message.tag = @"sspm-test";
- [[SSPostmark postmaster] setApiKey:SSPOSTMARK_TEST_API_KEY];
+ SSPostmarkAttachment *att = [[SSPostmarkAttachment alloc] init];
+ att.content = UIImagePNGRepresentation([UIImage imageNamed:@"sunset.jpg"]);
+ att.name = @"image.png";
+
+ [message addAttachmentsObject:att];
+
+
+ [[SSPostmark postmaster] setApiKey:@"b73973a3-1b2f-4134-b30e-d0c635b0c0be"];
[[SSPostmark postmaster] sendMessage:message];
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 915bd32

Please sign in to comment.