i want to send the mail from iphone within my application - iphone

I am using the following code to send the mail but when i click on the send button, it come out of the app and send the mail.
i dont want to come out of my application.
-(IBAction) done:(id) sender
{
[self sendEmailTo: #"uttam.beldar#yahoo.com" withSubject: #" Question"
withBody:[textbody text]];
}
- (void) sendEmailTo:(NSString *)to withSubject:(NSString *) subject withBody:(NSString *)body
{
NSString *mailString = [NSString stringWithFormat:#"mailto:?to=%#&subject=%#&body=%#",
[to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
[body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];
}
can any one have the solution for this ?

You probably want to look into MFMailComposeViewController - it's the best option for that after the release of iPhone OS 3.0 software.
If you don't want a UI, you need to implement the SMTP protocol, since I don't think there's a built in one. But there is a google code project that provides this, if you want to incorporate it in your app.

You can implement send mail in this easy way.
Tell me if you have problems.
A

Related

Make phone call on iPhone and take user back to app? (UIWebView does it)

I used this code to make phone call:
NSString *phoneNumber = [#"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
and when the call ends, it does not take user back to the app. However, if I show a website in UIWebView and there is a phone number in the website(ie UIWebView recognises it), and if I tap that phone number link to make phone call, I will be taken back to the app when the call finishes.
My preliminary thinking is that the UIWebView does something internally like a deep link to the Phone app then another deep link inside the deep link to take the user back to the app. But I'm not sure. Is there a way to implement this feature?
Thanks!
You need to use the telprompt URL, not tel.
So:
NSString *phoneNumber = [#"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
This will also give the user a confirmation box before calling the number.
Edit:
This question covers the same issue.
Edit 2:
For those wondering if this URL will result in rejection from the App Store, the answer is generally no. The greater risk is that Apple will suddenly stop supporting the telprompt scheme. As explained by this article, there is a slightly 'safer' way of implementing telprompt with UIWebView (which uses telprompt internally, even if you call it with tel). The relevant code from the article shows how using the documented tel scheme can still give you the effect of telprompt:
+ (void)callWithString:(NSString *)phoneString {
[self callWithURL:[NSURL URLWithString:[NSString
stringWithFormat:#"tel:%#",phoneString]]];
}
+ (void)callWithURL:(NSURL *)url {
static UIWebView *webView = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
webView = [UIWebView new];
});
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
(Code taken from the article reference in my second edit)
The website: http://www.raizlabs.com/dev/2014/04/getting-the-best-behavior-from-phone-call-requests-using-tel-in-an-ios-app/ has a discussion of whether to use telprompt (undocumented, Apple could potentially change the API without notice), and instead using a category that sends the number to a web view which opens it using telprompt. This has the advantage of not breaking if Apple does something odd.

Make calls from iPhone app [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
make a call in iphone from my application
I want to make a phone call on given number from my iPhone application. Could you suggest any good tutorial which explains it the best or tell me the process?
You can try :
NSURL *phoneNumberURL = [NSURL URLWithString:#"tel:80001212"];
[[UIApplication sharedApplication] openURL:phoneNumberURL];
NSString* phoneNumber=TextFiled Name
NSString *number = [NSString stringWithFormat:#"%#",phoneNumber];
NSURL* callUrl=[NSURL URLWithString:[NSString stringWithFormat:#"tel:%#",number]];
//check Call Function available only in iphone
if([[UIApplication sharedApplication] canOpenURL:callUrl])
{
[[UIApplication sharedApplication] openURL:callUrl];
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"ALERT" message:#"This function is only available on the iPhone" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Try this:-
NSString *phoneNumber = #"15555551212";
NSString *dtmfAfterPickup = #"1234";
NSString *telString = [NSString stringWithFormat:#"tel:%#,%#", phoneNumber, dtmfAfterPickup];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:telString]];
Opening an app from within another app is managed in iOS through the "url scheme" mechanism. If an app defines an url scheme, and this scheme is public, you can then use it to run that app.
Basic rule is to first check that your device supports that scheme (e.g. you cannot make a phone call on an iPad because the phone app is not installed) and then, if the answer is positive, call it:
if([[UIApplication sharedApplication] canOpenURL:myURL]) {
[[UIApplication sharedApplication] openURL:myURL];
} else {
// do something else, e.g. inform the user that he/she cannot open the app
}
This check is important as for some schemes, e.g. the phone one, the system checks is the url is well formed or not. E.g.: for phone numbers space between digits is not supported.
The most common Apple URL schemes are described here:
http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html#//apple_ref/doc/uid/TP40007893-SW1
In particular, the telephone url scheme is here:
http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html#//apple_ref/doc/uid/TP40007893-SW1
Finally there is a web site, called handleOpenURL that is trying to collect all apps url schemes. If you define an app that exposes an url scheme and you want it to be public, don't hesitate to post it in this site.
There are two ways to acheive this:-
1) [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://9912345678"]];
2) You can use the UITextView and enable phone detection. After that the phone number will look like hyperlinked. Use the following code.
mytextview.text = #"9943586256";
mytextview.dataDetectorTypes = UIDataDetectorTypePhoneNumber;
mytextview.editable=NO;
Helpful if you want to show the phone number inside a custom tableview cell.
I would personally like the second one due to requirements in some project. When i have give the telephone number in UITextView and upon pressing that will start the calling.

Attachments using email uri scheme in iOS

I am implementing an iphone application (iOS 4.2) from where I would like to trigger the email client to send messages with attachments. I could effectively use uri schemes in combination with the class NSURL in order to trigger the email app but I was wondering whether it is possible to attach images. I have tried with mailto:whoever#wherever.org?subject=sthg&body=sthgelse&attachment=/path/to/file but the attachments are not included. I know iphone applications are sandboxed therefore it is possible that the email utility were not able to access the path to my image since it is located in my application bundle. On the other hand I was considering to administer my images with the photo manager. (1) Is there a way to include attachments in this way? (2) If so, is it possible to reference images either from my app or from the photo client? I could not find any attachments argument in the mailto RFC but maybe Apple has provided some way to achieve this.
Thanks in advance for your help,
Luis
MFMailComposeViewController will be able to do that, some example of usage belows:
remember to add MessageUI.framework
MFMailComposeViewController *email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;
[email setSubject:#"Whatever"];
// Set up recipients
NSArray recipients = [NSArray arrayWithObject:#"whoever#wherever.org"];
[email setToRecipients:recipients];
// Attach an image to the email
UIImage *attachment = ...;
NSData *data = UIImagePNGRepresentation(attachment);
[email addAttachmentData:myData mimeType:#"image/png" fileName:#"ok.png"];
// Fill out the email body text
NSString *emailBody = #"test mail";
[email setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[email release];
Instead of using the mailto: URL scheme, you should use the MFMailComposeViewController which allows you to add attachments. It also has the added benefit that using will not leave your app.
If one does not have account MFMailComposeViewController simply crashes.
Yes, you can call canSendMail first with the result NO(!), what next?
The answer is - use 'mailto:'. It'll popup dialog to create account.

iOS: Connect to Facebook without leaving the app for authorization

I know that it was possible before the Graph API.
I work on an iPhone app that may not be in the background (one of the requirements).
In addition there is a login screen on the app launching.
Therefore it is not suitable to go to background in order to authenticate to Facebook and then return to the app and login once again each time the user wants to share something.
So, my question is if there is a way to authenticate with Facebook without leaving the app.
BTW, I have tried to use the old API.
It worked in the beginning but yesterday it stopped working.
I just see a blank screen inside the old Facebook login web view.
I have also checked one of my old apps that use that old Facebook Connect API and I get the same blank login screen in that app too.
Any idea will be appreciated.
Thank you in advance.
--
Michael.
In Facebook.m
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
safariAuth:(BOOL)trySafariAuth {
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
_appId, #"client_id",
#"user_agent", #"type",
kRedirectURL, #"redirect_uri",
#"touch", #"display",
kSDKVersion, #"sdk",
nil];
method comment out this
UIDevice *device = [UIDevice currentDevice];
if ([device respondsToSelector:#selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
if (tryFBAppAuth) {
NSString *fbAppUrl = [FBRequest serializeURL:kFBAppAuthURL params:params];
didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
}
if (trySafariAuth && !didOpenOtherApp) {
NSString *nextUrl = [NSString stringWithFormat:#"fb%#://authorize", _appId];
[params setValue:nextUrl forKey:#"redirect_uri"];
NSString *fbAppUrl = [FBRequest serializeURL:loginDialogURL params:params];
didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
}
}
This will prevent the app from going to background and show you the standard fb dialog.
Here you have an alternative solution.
If you don't like to change the facebook sdk code and want a solution that allows you to choose between SSO or the old-fashioned mechanism, you can Implement an extension like this:
//Facebook_SSOExtension.h
--------------------------------------------------------
#interface Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO;
#end
//Facebook_SSOExtension.m
--------------------------------------------------------
//So warnings do not appear
#interface Facebook(PrivateSSOExtension)
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
safariAuth:(BOOL)trySafariAuth;
-(void) setPermissions:(NSArray*) permissions;
#end
#implementation Facebook(SSOExtension)
-(void) authorize:(NSArray*)permissions useSSO:(BOOL) useSSO
{
[self setPermissions: permissions];
[self authorizeWithFBAppAuth:useSSO safariAuth:useSSO];
}
#end
Even though this requires more work than commenting-out the sso code, you will be able to update the facebook-sdk without major problems (if they rename authorizeWithFBAppAuth:safariAuth: your extension will not work, use asserts to detect this issue quickly). Also, if you are building a reusable component to interact with facebook without repeating things over and over again, this will save you some work too.
Cheers.

iPhone - Anyway to get iPhone's Email App Inside Your Own App?

Has anyone every embeded the iPhone's Email program inside your own App?
Let me try and simplify that. Tap Tap Revenge allows you to "Challenge A Friend". When you choose to do so they open the standard iPhone email program (if they mimicked it, it looks damn good), within the application with pre-populated data. All you have to do is select a friend from your contacts and press send. You never leave the Tap Tap Revenge App.
Any ideas how this is done?
You need to include the MessageUI.framework into your project, and inside your header file you need to set the delegate:
#import <MessageUI/MessageUI.h>
#interface RootViewController : UIViewController <MFMailComposeViewControllerDelegate> {
MFMailComposeViewController *email;
}
#property (nonatomic, retain) MFMailComposeViewController *email;
Once you do that, you have a few delegate methods inside your implementation file you need to include (You should check to see the result, but I am trying to keep as little code as needed):
#synthesize email;
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[email dismissModalViewControllerAnimated:YES];
}
Wherever you want to use this, you need to initialize and set it up like this:
email = [[MFMailComposeViewController alloc] init];
email.mailComposeDelegate = self;
// Subject
[email setSubject:#"Testing"];
// Optional Attachments
NSData *artwork = UIImagePNGRepresentation([UIImage imageNamed:#"albumart.png"]);
[email addAttachmentData:artwork mimeType:#"image/png" fileName:#"albumart.png"];
// Body
[email setMessageBody:#"This is the body"];
// Present it
[self presentModalViewController:email animated:YES];
Further to Garett's great response, should you get the warning:
'MFMailComposeViewController' may not respond to '-setMessageBody:'
add isHTML: so the complete line reads like:
[mail setMessageBody:#"This is the body" isHTML:NO];
There is two answers for that. For applications that should support iPhone OS prior 3.0 the only way to go is to build a custom message composer. Or you may want to have a look at the component built by Joe Hewitt who wrote the Facebook iPhone app.
http://github.com/joehewitt/three20/blob/master/src/Three20/TTMessageController.h
With iPhone SDK 3.0 you are able to use the Mail message composer UI straight out if the box using the MessageUI.framework as explained above.
messagecomposer is the way to go! mails and sms inside the app, run from iOS 3.1. need sdk 4