Cocoa osx NSSharingServicePicker set default email address - macos

In my app for Mac I use the NSSharingServicePicker for mailing some items. My problem is that I want to set the default email address to be sent. How can I set this value to show it when the mail client opens??
I have this code:
NSMutableArray *shareItems = [NSMutableArray arrayWithObject:#"MyText"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/MyFile.pdf", documentsDirectory];
NSURL* tempFileURL = [NSURL fileURLWithPath:fileName];
[shareItems addObject:tempFileURL];
NSSharingServicePicker *sharingPicker = [[NSSharingServicePicker alloc] initWithItems:shareItems];
sharingPicker.delegate = self;
This is what I want. I need to set the default destination address in the email.
Sorry for my poor english. Thanks in advance.
SOLVED:
-(id<NSSharingServiceDelegate>)sharingServicePicker:(NSSharingServicePicker *)sharingServicePicker delegateForSharingService:(NSSharingService *)sharingService
{
[sharingService setRecipients:#[#"email#email.com"]];
[sharingService setSubject:#"MySubject"];
return self;
}

NSSharingServiceNameComposeEmail is generally the standard way to achieve this:
NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameComposeEmail];
service.delegate = self;
service.recipients = #[#"tim.cook#apple.com"];
service.subject = [ NSString stringWithFormat:#"%#",NSLocalizedString(#"Re: Requested PDF",nil)];
[service performWithItems:shareItems];
It's not exactly clear what you are doing with NSSharingServicePicker, although this code can be used with shareItems, so the file would be attached along with the body text.

Related

NSManagedObject : having one answer of a loop instead of multiple answer from a loop Xcode

i have a problem when creating a txt file from a for statement. Here is the code of the statement on a CoreData.
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * allTickets = [[NSFetchRequest alloc] init];
[allTickets setEntity:[NSEntityDescription entityForName:#"Place" inManagedObjectContext:context]];
[allTickets setIncludesPropertyValues:NO];
NSError * error = nil;
NSArray * ticks = [context executeFetchRequest:allTickets error:&error];
for (NSManagedObject * tick in ticks) {
NSArray *keys = [[[tick entity] attributesByName] allKeys];
NSDictionary *myDict = [tick dictionaryWithValuesForKeys:keys];
NSString *theDate = [myDict objectForKey:#"date"];
NSString *theName = [myDict objectForKey:#"name"];
NSString *theNumber = [myDict objectForKey:#"number"];
NSString * finalExport = [NSString stringWithFormat:#"%#%#%#", theDate, theName, theNumber];
NSLog(#"%#",finalExport);
the NSLog result print out all the entries in CoreData correctly, running each request one after the other.
My problem is that when i want to create a txt with the finalExport NSString, i only have the first request in the file.
For exemple i got an NSLog with :
01.01.2015 MYNAME 34555445
02.01.2015 MYNAME 34523445
03.01.2015 MYNAME 34115445
04.01.2015 MYNAME 34552345
But in the text file i only got
01.01.2015 MYNAME 34555445
So how can i have a text file with all the NSLog Results
Thanks in advance for your help ;)
EDIT ++++++++++++++
Here is the rest of the code :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
NSError *error;
BOOL succeed = [finalExport writeToFile:[documentsDirectory stringByAppendingPathComponent:#"Number.txt"]
atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!succeed){
NSLog(#"NOT WORKING");
}
You need to make sure you write an array of entries to the file, not just one entry.

App terminates when using NSURL with parameters

In my app I sent a request to my PHP file to download some info. Whenever I use the code below it will terminate:
-(void)downloadItems
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *myString = [prefs stringForKey:#"commentsId"];
NSLog(#"The comments id is : %#",myString);
// Download the json file
NSString *urlstring = [NSString stringWithFormat:#"http:/MyWebSite.com/checkphp.php?title=%#", myString];
NSURL *jsonFileUrl = [NSURL URLWithString:urlstring];
NSLog(#"The qasida id is: %#", jsonFileUrl);
// Create the request
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];
// Create the NSURLConnection
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
But if I remove myString from urlString like this:
NSURL *jsonFileUrl = [NSURL URLWithString:#"http:/myWebSite/checkphp.php?title=%#"];
the app will not terminate and it will retrieve data.
Can any one tell me what is wrong with my code?
Thanks

attachment not sent with email from ipad

I have an iPad that has a routine to create a pdf and send as an attachment to an email. It all seems to work with the email composer opening showing the pdf document attached. However when tested on an iPad, when the email is received there is no attachment. Any ideas?
[mailComposer addAttachmentData:data mimeType:#"application/pdf" fileName:#"pdffile.pdf"];
[self presentViewController:mailComposer animated:YES completion:nil];
Many thanks
Detail:
The pdf file is created and called pdffile.pdf. The following is the full email routine:
MFMailComposeViewController *mailComposer;
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setSubject:[NSString stringWithFormat: #"i-observe Lesson Observation for: %s", "date"]];
[mailComposer setMessageBody:[NSString stringWithFormat: #"i-observe Lesson Observation for: %s", "name"] isHTML:NO];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:#"pdffile.pdf"];
NSMutableData *data=[NSMutableData dataWithContentsOfFile:file];
[mailComposer addAttachmentData:data mimeType:#"application/pdf" fileName:#"pdffile.pdf"];
[self presentViewController:mailComposer animated:YES completion:nil];
Try This Method:
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setSubject:#"Email with attached pdf"];
NSString *newFilePath = #"get path where the pdf reside";
NSData * pdfData = [NSData dataWithContentsOfFile:newFilePath];
[mail addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"yourpdfname.pdf"];
NSString * body = #"";
[mail setMessageBody:body isHTML:NO];
[self presentModalViewController:mail animated:YES];
[mail release];
}
else
{
NSLog(#"Message cannot be sent");
}
The next solutions is based assuming that the pdf file is in your main bundle:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *myFile = [mainBundle pathForResource: #"RealNameofFile" ofType: #"pdf"];
NSData *pdfD = [NSData dataWithContentsOfFile:myFile];
[mailViewController addAttachmentData:pdfData mimeType:#"application/pdf" fileName:#"Nametodisplayattached.pdf"];
make sure that the file has the same name as the table cell

Convert NSDictionary array values into NSString format for MFMailComposeViewController

I have a UIPickerViewDelegate that allows the user to select a term from the picker and see the term's definition by using NSDictionary to access data stored in a plist file. I also have a MFMailComposeViewController to allow the user to email the term and it's definition elsewhere.
But I can't seem to get the term and definition formatted properly for entry into the email. I looked at various "convert NSDictionary to NSString" solutions, but none seem to offer what I need, including variations of Term = [dict objectForKey:#"Term"].
Under viewDidLoad, I have the following:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Glossary" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.termArray = [dict objectForKey:#"Term"];
self.definitionArray = [dict objectForKey:#"Definition"];
My pickerView code includes this:
NSString *resultString = [[NSString alloc] initWithFormat:
#"%#",
[definitionArray objectAtIndex:row]];
definitionLabel.text = resultString;
NSString *termString = [[NSString alloc] initWithFormat:
#"%#",
[termArray objectAtIndex:row]];
The code for showEmail contains:
int definitionIndex = [self.termPicker selectedRowInComponent:0];
NSString *emailTitle = [NSString stringWithFormat:#"Definition of %#", termLabel];
NSString *emailDefinition = [definitionArray objectAtIndex:definitionIndex];
NSString *messageBody = [NSString stringWithFormat:#"The definition of <B>%#</B> is:<P> <B>%#</B>", termLabel, emailDefinition];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:YES];
I would like the email title and message body to pull from the Term selected in the picker and the Definition associated with it. I'm sure it must be a simple formatting issue, but I can't figure it out.

Problems adding data to a plist file

I've been trying to write data back to a pre-defined plist file (data.plist) in my bundle. Using the code below I call the routine 'dictionaryFromPlist' to open the file and then call 'writeDictionaryToPlist' to write to the plist file. However, no data gets added to the plist file.
NSDictionary *dict = [self dictionaryFromPlist];
NSString *key = #"Reports";
NSString *value = #"TestingTesting";
[dict setValue:value forKey:key];
[self writeDictionaryToPlist:dict];
- (NSMutableDictionary*)dictionaryFromPlist {
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
NSMutableDictionary* propertyListValues = [[NSMutableDictionary alloc]
initWithContentsOfFile:filePath];
return [propertyListValues autorelease];
}
- (BOOL)writeDictionaryToPlist:(NSDictionary*)plistDict{
NSString *filePath = #"data.plist";
BOOL result = [plistDict writeToFile:filePath atomically:YES];
return result;
}
The code runs through successfully and no error is thrown but no data is added to my plist file.
You can not write to your bundle, it is read only. If your case though, you are writing to a relative path, not to the bundle.
I'm not sure what the default working directory is for iOS apps. It is best to use absolute paths. You should be writing to the documents/cache directory. Something like this will get the path for you:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Then just grab the lastObject and prepend that to your file name.
As mentioned by #logancautrell you can not write in mainbundle, you can save your plist in the app documents folder, you could do so:
NSString *path = #"example.plist";
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count]> 0)? [paths objectAtIndex: 0]: nil;
NSString *documentPath = [basePath stringByAppendingPathComponent:path] // Documents
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL checkfile = [fileManager fileExistsAtPath: documentPath];
NSLog(#"%#", (checkFile ? #"Exist": #"Not exist"));//check if exist
if(!checkfile) {//if not exist
BOOL copyFileToDoc = [yourDictionary writeToFile:documentPath atomically: YES];
NSLog(#"%#",(copyFileToDoc ? #"Copied": #"Not copied"));
}

Resources