How to set from in MFMailComposeViewController? - objective-c

How do I set the from address in the MFMailComposeViewController?
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"In app email..."];
[controller setMessageBody:#"To FirstName LastName: " isHTML:NO];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"eamil#somemail.com"]; // (NSString *) [feed valueForKey:#"email"]];
[controller setToRecipients:toRecipients];

There isn't a way to do what you want with the From field. The from address will default to whatever mail account the user has specified as "default" in Settings. Obviously if the user has only one mail account set up then it will be that account.

This is possible since iOS 11.0
Use setPreferredSendingEmailAddress(_:) to define a preferred address which will automatically select a corresponding account if available.
From the documentation:
Sets the preferred email address to use in the From field, if such an address is available.
If the user does not have an account with a preferred address set up, the default account is used instead.
Call this method before you display the mail composition interface only. Do not call it after presenting the interface to the user.

As far as I know, it's not possible. When the mail composer is open, you will be able to fetch the "from" field from the list of e-mails configured in the device, and the user selected default will be initially set.
I know that it would be useful to know the list of available "from" accounts and set the composer with a more appropriate user choice. Probably Apple doesn't want to give the app this possibility. You might open a "radar" with Apple, e.g. adding a special view controller to let the user define a different default address for the specific app, this would be a nice addition to the iOS.

I don't think you can change "from" if you use MFMailComposeViewController in app email in iOS Another workaround, you may use this, a standalone email client, instead. You will have better control of the sender programmatically.

Related

How could i add phonebook contacts to my chat app in ios by using xmpp framework and openfire server?

I am new to chat application and making it in objective-c. I wanted to know how could i save all my phonebook contacts as user. i know how to fetch contacts from address book but dont have much idea about xmpp framework and openfire.If anyone could help me.
First you know how to fetch contacts.now every contact you need to add Xmpp Roaster.like below
call this method called at your contact fetching class.
[[self appDelegate] addNewBuddyToMyAccount:tempNumber withNickName:[[tempContactList objectAtIndex:i] fullname]];
//in my terms tempNumber is the phone number that is JID.
in your xmpp class follow below code to add to roster
NSString * buddyNameJID = [NSString stringWithFormat:#"%##serverName",buddyUserID];
XMPPJID *jid = [XMPPJID jidWithString:buddyNameJID];
//if you don't need permission then use below line otherwise comment
[[self xmppRoster] acceptPresenceSubscriptionRequestFrom:jid andAddToRoster:YES];
//here we are adding our contact to our roster then check once in roaster in openfire
[[self xmppRoster] addUser:jid withNickname:nickName];
hope this helps.

Is there a way to send a calendar event via UIActivityViewController?

The title pretty much says it all. What follows is my best guess, but the event just doesn't appear at all.
NSArray *items = #[string, URL, (EKEvent *)[self createCalendarEvent]];
UIActivityViewController* activity = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:activities];
[self presentViewController:activity animated:YES completion:NULL];
Does anyone know of a way to accomplish this? Or do I have to create an ics file and send that?
UIActivityViewController:
The system provides several standard services, such as copying items
to the pasteboard, posting content to social media sites, sending
items via email or SMS, and more.
Apps can also define custom services.
It's not (yet) possible to share an EKEvent-object nor its parent EKCalendarItem as none of the standard services provide a way to interact with this type of activity.
You should indeed create an .ics-file to share your event. It's a bit strange that iOS doesn't convert the object by itself as it do this for strings (into .txt-files).
You could however fill in a request to get this option.

Sending multiple url requests with NSURL Connection

I need to send data many times within one view. I have about 6 UISwitches and every time there value gets changed, I need to send (via a POST method) data (a simple NSString) to my php script (http://www.mydomain.com/script/dowork.php)
I have been struggling over the last couple of days in doing this since I have only managed to get this to work with only 1 switch but not when users change multiple switches. If someone can please give me an example of how to do this it would mean a lot.
EDIT
This is how Im doing it now:
-(IBAction)switchSelector:(id)sender {
switch ([sender tag]) {
case 0:
if (switchMax.on) {
//here I Send it to the method that will make the connection
[self registerWithServer:#"Tokens"];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool: switchMax.on forKey: K_SWITCH_KEY];
[defaults synchronize];
} else {
//other stuff
}
break;
}
}
How did you manage to do it with one switch? Could you post the code as well?
For multiple switches just hook up your method with the valueChanged in interface builder. Inside that method check which switch is on - if ([mySwitch1 isOn]), edit your NSString and send it.
Just some thoughts, but do you really need to send the changes to your server immediately? Assuming that these switches are used in some settings panel, you could save the settings once the user is done with the panel and send it to your server, e.g. when they press the Done button or exit from this view. This way, you only send the request once without having to be burdened with sending it multiple times.
Keep in mind that the user is on a mobile device and you should try to keep data usage as low as possible.

Authenticating Dropbox in iOS

I am adding Dropbox support to my iOS application. Using the official Dropbox API and the tutorials online here I have gotten to the point where Dropbox needs to be authenticated. The code below is what is given to Authenticate when a button is pressed:
//MainViewController.m
....
#implementation CryptoMainViewController
.....
#pragma mark - Dropbox
- (void)didPressLink {
if (![[DBSession sharedSession] isLinked]) {
[[DBSession sharedSession] link];
}
}
But no matter how I change the code, where I put it or what button I link it to, nothing will happen. Using breakpoints I've found that the method does in-fact get triggered. I've even put it in an IBAction, but this gives the same result. What am I doing wrong? How can I get my app to authenticate the end-user?
And, once authenticated, How can I save an NSString to the user's Dropbox?
If this is just totally wrong, then where can I go to find resources on how to do this properly?
The whole tutorial, all of the documentation, api, etc. is available here.
I had the same problem; the reason was that I hadn't set the shared Dropbox session, e.g.
DBSession* dbSession = [[[DBSession alloc] initWithAppKey: #"your_app_key"
appSecret: #"your_app_secret"
root: kDBRootAppFolder] autorelease];
[DBSession setSharedSession: dbSession];
Once that was called the link worked fine.
this answer may be late but im guessing that you already linked your app before and want to do so again. The only way you can have the process of linking taking place again is if you run the following code:
[[DBSession sharedSession] unlinkAll];
You can place it in your viewDidLoad. When you then call didPressLink: the app should open up dropbox app(if available), safari or an in app window asking for your permission to access your dropbox. If this does not happen then the problem is somewhere else. Hope this helps
Does your view implement the <DBLoginControllerDelegate> ?
If so, link Dropbox like so:
DBLoginController* controller = [[DBLoginController new] autorelease];
controller.delegate = self;
[controller presentFromController:self];

Get Current User from root mode?

I am writing a login item and I am trying to find if its possible to get the current user.
Lets say, I have logged in with user "Test" and when I execute the exe with root privileges and if I use
char *user = getenv("USER");
user is "root".
My expected answer is "Test".
How can I get it?
I don't know if I can put objective c code in login item? Is it possible to NSUserName in login item.
NSString *user = NSUserName();
You want the SCDynamicStoreCopyConsoleUser function.
QA1133 gives some relevant details and caveats.
Login items can be Cocoa applications, so you can use NSUserName() (which is in Foundation by the way).
Maybe also look at getuid() / geteuid()