Box: Getting sharedlink parameter immediately after uploading new file - ios

Is there any way to get sharedLink parameter, immediately after uploading a new file on box in BoxFile block.
I've tried following code, but it's returning sharedlink parameter as nil.
BoxFilesRequestBuilder *builder = [[BoxFilesRequestBuilder alloc] init];
builder.name = imageName;
builder.parentID = folderID;
//--- shared link object ---//
BoxSharedObjectBuilder *sharedBuilder = [[BoxSharedObjectBuilder alloc] init];
sharedBuilder.access = BoxAPISharedObjectAccessOpen;
builder.sharedLink = sharedBuilder;
NSInputStream *inputStream = [NSInputStream inputStreamWithFileAtPath:imagePath];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:imagePath error:nil];
long long contentLength = [[fileAttributes objectForKey:NSFileSize] longLongValue];
[[BoxSDK sharedSDK].filesManager uploadFileWithInputStream:inputStream contentLength:contentLength MIMEType:nil requestBuilder:builder success:fileBlock failure:failureBlock progress:nil];

You have to make a second call after the file is uploaded.

Related

Send HTML email with SKPSMTP iOS

I am trying to send an HTML email from my SKPSMTP code in iOS. Right now, I'm just sending plain text, but I'm trying to upgrade that a little. I've included that code below.
I can't find any documentation. How can I upload an HTML file and include that as it's body. Also, there's an image that's being loaded from the same directory as the HTML file, if that makes a difference in the answer. Thanks.
NSMutableString *emailBody = [NSMutableString stringWithFormat:#"Here's your code again, "];
[emailBody appendString:userCode];
SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
email.fromEmail = #"me#gmail.com";
NSString *toEmail = [NSString stringWithFormat:#"%#", self.loginInput.text];
email.toEmail = toEmail;
email.relayHost = #"smtp.gmail.com";
email.requiresAuth = YES;
email.login = #"me#gmail.com";
email.pass = #"myPass";
email.subject = #"Your Validation Code";
email.wantsSecure = YES;
email.delegate = self;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,
emailBody,kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
email.parts = [NSArray arrayWithObjects:plainPart, nil];
// Send it!
[email send];
So, here's the answer I came across, just so everyone else can get the benefit of me struggling through:
//Send them an e-mail
NSError* error = nil;
NSString *path = [[NSBundle mainBundle] pathForResource: #"loginEmail" ofType: #"html"];
NSString *result = [NSString stringWithContentsOfFile: path encoding:
NSUTF8StringEncoding error: &error];
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:#"<!--INJECT CODE HERE -->"
options:0
error:&error];
NSString *emailBody = [regex stringByReplacingMatchesInString:result options:0 range:NSMakeRange(0, [result length]) withTemplate:code];
NSLog(#"%#", [emailBody class]);
SKPSMTPMessage *email = [[SKPSMTPMessage alloc] init];
email.fromEmail = #"myemail#gmail.com";
NSString *toEmail = [NSString stringWithFormat:#"%#", self.loginInput.text];
email.toEmail = toEmail;
email.relayHost = #"smtp.gmail.com";
email.requiresAuth = YES;
email.login = #"myemail#gmail.com";
email.pass = #"myPass"
email.subject = #"Your Validation Code";
email.wantsSecure = YES;
email.delegate = self;
NSDictionary *htmlPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/html",kSKPSMTPPartContentTypeKey, emailBody,kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey, nil];
email.parts = [NSArray arrayWithObjects:htmlPart, nil];
// Send it!
NSLog(#"ABOUT TO SEND");
[email send];
So, I had to write an HTML file, host all my images on tinypic to include in the HTML, write some text to regex switch out my code variable, load in it in here and attach it as the part of my email with key "text/html". This code works, but if anyone has any other suggestions that are helpful, I'm willing to mark them as the right answer!

GDataXML updating xml data

I am trying to update an xml file. After suggestion I ended up choosing GdataXml.
So I am trying to update options.xml file.
Original File
<Dat>
<Name>Tom</Name>
<Option>1</Option>
</Dat>
I need to change "Tom" to "Jim" and save in the same file
Here is the code I tried.
-(void)saveToXML
{
NSString* path = [[NSBundle mainBundle] pathForResource:#"options" ofType:#"xml"];
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
GDataXMLElement *rootElement = [GDataXMLElement elementWithName:#"Dat"];
NSArray *mySettings = [doc.rootElement elementsForName:#"Dat"];
for (GDataXMLElement *mySet in mySettings)
{
NSString *name;
NSArray *names = [mySet elementsForName:#"Name"];
if (names.count > 0)
{
GDataXMLElement *childElement = (GDataXMLElement *) [names objectAtIndex:0];
name = childElement.stringValue;
NSLog(childElement.stringValue);
[childElement setStringValue:#"Jim"];
}
}
[xmlData writeToFile:path atomically:YES];
}
But this is not updating options.xml file. Can some one help on this ?
The missing line of code you are looking for is
NSData *xmlData = doc.XMLData;
from Anupdas answer on your last question. You are currently reading a file into memory, initializing a new object using that memory, updating that new object and then writing the original file's memory into the new file location. So essentially you are reading a file and then writing the same file back to the file location.

Multipart Upload to Amazon S3 with IOS 6

Trying to upload files bigger than 5 mb to Amazon S3
http://aws.amazon.com/articles/0006282245644577, here it is stated that
// The S3UploadInputStream was deprecated after the release of iOS6
Files under 5 mb I can easily upload over wifi with:
NSData *dataForAamzon = [[NSFileManager defaultManager] contentsAtPath:pathForFiile];
#try {
NSString *uploadName= #"somestring";
S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uploadName
inBucket:[Constants userEventBucket]];
por.contentType = nil;
por.data = dataForAamzon;
// Put the image data into the specified s3 bucket and object.
S3PutObjectResponse *putObjectResponse = [self.s3 putObject:por];
[self performSelector:#selector(uploadAllFilesToAmazon:error:) withObject:nil withObject:putObjectResponse.error];
}
#catch ( AmazonServiceException *exception ) {
NSLog( #"Upload Failed, Reason: %#", exception );
}
Since S3UploadInputStream *stream was deprecated , how can I check if file is bigger than 5mb and use multipart upload in IOS 6 or later versions.
Thanks,
I hope you need to check the size of a local file exceeds 5MB right ?
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:_client.filename error:nil];
if(fileAttributes)
{
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long totalFileSize = [fileSizeNumber longLongValue];
if(totalFileSize > 5*1024*1024)
// Go for multipart upload
else
// Go for Direct Put request
}
Or from the NSData you can take the Length and perform the same calculation.
New release of AWS SDK solved my problem.
New AWS SDK is handling files size automatically you need to create trasnfer manager variable and delegate it to s3 object
something like
-(void) uploadAgendatoAmazon
{
//pass string in memory to nsdictionary
NSData * data = [_uploadPlistString dataUsingEncoding:NSUTF8StringEncoding];
NSString *uploadName= [NSString stringWithFormat:#"%#/%#/agenda.plist",[[NSUserDefaults standardUserDefaults] valueForKey:#"email"],self.textEventName.text];
self.s3.maxRetries = 10;
self.s3.timeout = 60;
self.tm = [S3TransferManager new];
self.tm.s3 = self.s3;
self.tm.operationQueue.maxConcurrentOperationCount = 2;
S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:uploadName
inBucket:[Constants userEventBucket]];
por.contentType = nil;
por.data = data;
por.delegate = self;
por.requestTag = #"uploadAgendatoAmazon";
// Put the image data into the specified s3 bucket and object.
[self.tm upload:por];
}
Here is the blog post for full details;
http://mobile.awsblog.com/post/TxIRFEQTW9XU8G/S3TransferManager-for-iOS-Part-I-Asynchronous-Uploads

IOS AWS s3 bucket image uploading problems

Been trying to get this to work for a while know i have tried it with data and with streaming but with no luck, I assume i do not need to create the bucket as shown in most examples as the bucket i want to upload to already exists, also it tells me the bucket i am trying to access, however when i try to upload i get this
"Error uploading image to S3: The XML you provided was not well-formed or did not validate against our published schema"
here is the code
UIImage * img = [[[[CurrentGamesInfo sharedCurrentGamesInfo]GameInCreation] GetImageToSend]_imageToSend];
NSData *savedImageData = UIImageJPEGRepresentation(img, 0.8);
NSString *pictureName = [[[CurrentGamesInfo sharedCurrentGamesInfo]GameInCreation] GetGameID];
#try
{
AmazonS3Client *s3 = [[[AmazonS3Client alloc] initWithAccessKey:MY_ACCESS_KEY_ID withSecretKey:MY_SECRET_KEY] autorelease];
//[s3 createBucket:[[[S3CreateBucketRequest alloc] initWithName:MY_PICTURE_BUCKET] autorelease]];
NSArray *bucketArray = [s3 listBuckets];
S3Bucket *bucket = [[s3 listBuckets] objectAtIndex:0];
NSLog(#"%#",[bucket name]);
S3PutObjectRequest *por = [[[S3PutObjectRequest alloc] initWithKey:pictureName inBucket:MY_PICTURE_BUCKET] autorelease];
por.contentType = #"image/jpeg";
//por.data = savedImageData;
//por.cannedACL = [S3CannedACL publicRead];
S3UploadInputStream *stream = [S3UploadInputStream inputStreamWithData:savedImageData];
//stream.delay = 0.2;
//stream.packetSize = 16;
por.contentLength = [savedImageData length];
por.stream = stream;
[s3 putObject:por];
}
#catch (AmazonClientException *exception)
{
NSLog(#"Error uploading image to S3: %#", exception.message);
}
sooooo...what am i doing wrong?
Nevermind...the variable "pictureName" was nil...I slink away in shame

Email attachment sent using SKPSMTPMessage appears in iOS mail client as contact

I have tested the code for sending attachments using this useful library:
Skpsmtpmessage library
The email seems to get sent correctly, and when I view it through hotmail or gmail clients I see the jpeg image. However, when I view this same email through an iOS mail client, the attachment appears as a "contact" and clicking on this gives me the option to save the file as a new contact.
I have tried sending an email with jpeg attachment from hotmail, and when I do this it appears correctly in the iOS client.
Does anyone know whether this is the code or iOS getting it wrong?
//the guts of the message.
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
testMsg.fromEmail = #"aname#gmail.com";
testMsg.toEmail = #"aname#gmail.com";
testMsg.relayHost = #"smtp.gmail.com";
testMsg.requiresAuth = YES;
testMsg.login = #"aname#gmail.com";
testMsg.pass = #"password";
testMsg.subject = #"The message subject";
testMsg.wantsSecure = YES; // smtp.gmail.com doesn't work without TLS!
// Only do this for self-signed certs!
// testMsg.validateSSLChain = NO;
testMsg.delegate = self;
//email contents
NSDate* now = [NSDate date];
NSString * bodyMessage = [NSString stringWithFormat:#"The message body"];
// email image if it exists
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/file.jpeg"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray* parts = [[NSMutableArray alloc] init];
// add plain part
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,
bodyMessage ,kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
[parts addObject: plainPart];
// add attachments
NSData *attachmentData = [NSData dataWithContentsOfFile:jpgPath];
NSString *directory = #"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"file.jpeg\"";
NSString *attachment = #"attachment;\r\n\tfilename=\"file.jpeg\"";
NSDictionary *image_part = [NSDictionary dictionaryWithObjectsAndKeys:
directory,kSKPSMTPPartContentTypeKey,
attachment,kSKPSMTPPartContentDispositionKey,
[attachmentData encodeBase64ForData],kSKPSMTPPartMessageKey,
#"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
[parts addObject: image_part];
testMsg.parts = parts;
[testMsg send];
Try to change
NSString *directory = #"text/directory;...
to
NSString *directory = #"text/jpeg;...
I hope this works for you!
Steffen

Resources