I am new to iphone development.I have created a tabbar based application . In the first i want the email composer to be displayed. I am able to display it but the cancel and send button are not working,I don't know where do i go wrong .Please help me out. Here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
[self displayComposerSheet];
}
-(void)displayComposerSheet
{
picker = [[MFMailComposeViewController alloc] init];
[[picker navigationBar] setTintColor:[UIColor blackColor]];
picker.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail])
{
[picker setToRecipients:[NSArray arrayWithObjects:#"name#gmail.com",nil]];
[picker setSubject:#"Sample"];
}
[self.view addSubview:picker.view];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissModalViewControllerAnimated:YES];
}
You are presenting the mail composer twice.
Remove the line:
[self.view addSubview:picker.view];
And replace the next line with:
[self.navigationController presentModalViewController:picker animated:YES];
If you are adding only subview of mailcomposser you have to remove it from self.view,
In your code you are adding subview and present also,
If you are use only use [self.view addSubview:picker.view]; than
Try with to remove it.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[controller.view removeFromSuperview];
}
I'm still suggest to use
[self.navigationController presentModalViewController:picker animated:YES]; for Present MFMailComposeViewController ,
and use [self dismissModalViewControllerAnimated:YES]; to dismiss it.
Use this code:
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObjects:#"niftyapplications#gmail.com", #"support#niftysol.com", nil];
[controller setToRecipients:toRecipients];
[controller setTitle:#"Contact Us"];
controller.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:controller animated:YES];
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self becomeFirstResponder];
NSString *strMailResult;
switch (result)
{
case MFMailComposeResultCancelled:
strMailResult = NSLocalizedString(#"E-Mail Cancelled",#"");
break;
case MFMailComposeResultSaved:
strMailResult = NSLocalizedString(#"E-Mail Saved",#"");
break;
case MFMailComposeResultSent:
strMailResult = NSLocalizedString(#"E-Mail Sent",#"");
break;
case MFMailComposeResultFailed:
strMailResult = NSLocalizedString(#"E-Mail Failed",#"");
break;
default:
strMailResult = NSLocalizedString(#"E-Mail Not Sent",#"");
break;
}
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"ISO Audit",#"") message:strMailResult delegate:self cancelButtonTitle:NSLocalizedString(#"OK",#"") otherButtonTitles:nil];
[alertView show];
[self dismissModalViewControllerAnimated:YES];
}
Set Delegate of MFMailComposeViewController
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc]init];
mailcomposer.mailComposeDelegate = self;
And Use this Delegate Method
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
}
Related
I want to determine it a user is signed on to twitter, so I can prompt them to post a tweet but only if they are set up with ios and twitter. Also, is there a way to set up the twitter screen with a default tweet?
For iOS 5.x, you can check if the user is signed in to twitter using:
[TWTweetComposeViewController canSendTweet]
As for presenting a tweet screen with default message, you can do something like:
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:defaultMsg];
[self presentModalViewController:tweetSheet animated:YES];
You can refer this tutorial for more.
EDIT: For iOS 6.0 and above, use:
// requires "Social.framework"
[SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]
So, an example usage could be:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler = myBlock;
[controller setInitialText:#"#myHashTag"];
[controller addImage:myImage];
[self presentViewController:controller animated:YES completion:Nil];
}
else
{ /* Show error alert, etc*/ }
Further reading
As I said in my comment if you are using IOS 5 or later versions just add Twitter.framework to your projects
#import <Twitter/Twitter.h>
//post tweets
- (IBAction)postTapped:(id)sender{
if ([TWTweetComposeViewController canSendTweet])
{
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:#" #hashtag"];
[self presentModalViewController:tweetSheet animated:YES];
}
else
{
TWTweetComposeViewController *viewController = [[TWTweetComposeViewController alloc] init];
//hide the tweet screen
viewController.view.hidden = YES;
//fire tweetComposeView to show "No Twitter Accounts" alert view on iOS5.1
viewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if (result == TWTweetComposeViewControllerResultCancelled) {
[self dismissModalViewControllerAnimated:NO];
}
};
[self presentModalViewController:viewController animated:NO];
//hide the keyboard
[viewController.view endEditing:YES];
}
}
I'm trying to write code to select an image from the photo library on an iPad. The code that I'm using is below (taken from Apple's website), but it keeps giving me an error saying that On iPad, UIImagePickerController must be presented via UIPopoverController. I've tried changing this line: UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init]; to use a UIPopoverController, but obviously I'm doing something wrong because its not working.
- (BOOL) selectImage: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// Displays saved pictures and movies, if both are available, from the
// Camera Roll album.
mediaUI.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeSavedPhotosAlbum];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = NO;
mediaUI.delegate = delegate;
[controller presentModalViewController: mediaUI animated: YES];
return YES; }
The Apple Developer page also says: "On iPad, you can alternatively present the browser interface using a popover as described in initWithContentViewController: and “Presenting and Dismissing the Popover” in UIPopoverController Class Reference." I've read it but I still can't get it to work. Any help would be appreciated.
Something like this should work:
// create an image picker controller
UIImagePickerController *imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imagePickerController.delegate = self;
// present it in a popover
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:imagePickerController] autorelease];
self.popoverController.delegate = self;
// I was presenting the popover from a button, but you would set the rect to whatever is appropriate for your app
[self.popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Declare an ivar such as:
UIPopoverController *imagePopover;
Then:
imagePopover = [[UIPopoverController alloc] initWithContentViewController:mediaUI];
[imagePopover setDelegate:self];
[imagePopover presentPopoverFromRect:someRect
inView:someView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
in place of
[controller presentModal...];
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[popover presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 400.0)
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
refer below link
http://www.techotopia.com/index.php/An_Example_iOS_4_iPad_Camera_and_UIImagePickerController_Application_%28Xcode_4%29
I went through a lot of pain coming up with a solution which works on both iPad and iPhone, this is the final code which some of it comes from comments of other people:
the code has some bugs but it's a very good place to start :)
it covers permissions needed as well as how to manage viewcontrollers popoing up on ipad.
requires following imports :
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
and add this delegates :
UIActionSheetDelegate, UIImagePickerControllerDelegate
definitions :
__weak IBOutlet UIButton *attachButton;
UIImage *image;
button's action :
- (IBAction)doAttach:(id)sender {
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:#"Select image from" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"From library",#"From camera", nil] ;
[action showInView:self.view];
}
#pragma mark - ActionSheet delegates
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if( buttonIndex == 1 ) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusAuthorized)
{
NSLog(#"%#", #"You have camera access");
}
else if(authStatus == AVAuthorizationStatusDenied)
{
NSLog(#"%#", #"Denied camera access");
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
NSLog(#"Granted access to %#", AVMediaTypeVideo);
} else {
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“no camera access“
message: #“if you need to use camera in this application go to settings -> appName -> and turn on camera.”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
NSLog(#"Not granted access to %#", AVMediaTypeVideo);
return ;
}
}];
}
else if(authStatus == AVAuthorizationStatusRestricted)
{
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“no camera access“
message: #“if you need to use camera in this application go to settings -> appName -> and turn on camera.”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
NSLog(#"%#", #"Restricted, normally won't happen");
}
else if(authStatus == AVAuthorizationStatusNotDetermined)
{
NSLog(#"%#", #"Camera access not determined. Ask for permission.");
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
NSLog(#"Granted access to %#", AVMediaTypeVideo);
} else {
NSLog(#"Not granted access to %#", AVMediaTypeVideo);
return ;
}
}];
}
else
{
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“No camera access“
message: #“error accusing camera”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
return;
//NSLog(#"%#", #"Camera access unknown error.");
}
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *pickerView =[[UIImagePickerController alloc]init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
pickerView.sourceType = UIImagePickerControllerSourceTypeCamera;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[ self.presentedViewController dismissViewControllerAnimated:YES completion:nil ];
pickerView.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popPC = pickerView.popoverPresentationController;
popPC.sourceView = attachButton;
popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:pickerView animated:YES completion:nil];
} else {
[self presentModalViewController:pickerView animated:YES ];
}
}
}else if( buttonIndex == 0 ) {
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
case ALAuthorizationStatusRestricted:
case ALAuthorizationStatusDenied:
{
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#“no access to library”
message: #“if you wish to access photos in this app go to settings -> appName-> and turn on photos .”
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#“ok” style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
break;
default:
{
UIImagePickerController *pickerView = [[UIImagePickerController alloc] init];
pickerView.allowsEditing = YES;
pickerView.delegate = self;
[pickerView setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[ self.presentedViewController dismissViewControllerAnimated:YES completion:nil ];
pickerView.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popup = pickerView.popoverPresentationController;
popup.sourceView = attachButton;
popup.permittedArrowDirections = UIPopoverArrowDirectionAny;
[self presentViewController:pickerView animated:YES completion:nil];
} else {
[self presentModalViewController:pickerView animated:YES ];
}
}
break;
}
}
}
#pragma mark - PickerDelegates
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissModalViewControllerAnimated:true];
UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage];
image = img;
}
#interface ViewController : UIViewController { //....yor variables }
in ViewController.m file:
(void)viewDidLoad {
[super viewDidLoad];
self.title = #"Sample Email Application"; // title of navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(composeMail:)] autorelease]; // for adding a compose button //in navigation bar. //...your code }
-(void) composeMail: (id) sender{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate = self;
[[picker navigationBar] setTintColor:[UIColor blackColor]];
[picker setSubject:#"Sample Email Application"];
[picker setMessageBody:[NSString stringWithFormat:#"Visit for more help %#. ",#"http://google.com"] isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[controller dismissModalViewControllerAnimated:YES];
}
It looks like you are missing the -setToRecipients, which you can't send mail unless you have an address to send to.
[picker setToRecipients:[NSArray arrayWithObjects:#"your#emailstring.com", nil]];
I've got a problem when i dismiss a modal view:
I've got a tabBar with five tabs.
I'm on the fourth tab and i display a modal view.
When i dismiss it. I go on the first tab but i would like to stay on the fourth.
To show the modal I call this method, in the parent controller:
[self presentModalViewController:controller animated:YES];
To hide the modal I call this method, in the parent controller:
[self dismissModalViewControllerAnimated:YES];
I already try to call self.tabBarController/self.navigationController methods instead of self method but it's the same issue.
Someone has an idea?
EDIT:
I call the methods in the fourth tab's controller.
This is the context:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.allowsEditing = NO;
imgPicker.delegate = self;
switch (buttonIndex) {
case 0: {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kMobiglissFromPicker];
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imgPicker animated:YES];
break;
}
case 1: {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kMobiglissFromPicker];
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imgPicker animated:YES];
}
break;
}
}
}
- (void)imagePickerController:(UIImagePickerController *)imgPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *origin = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
UIImage *photo = [origin thumbnailImage:280
transparentBorder:0.0
cornerRadius:0.0
interpolationQuality:kCGInterpolationHigh];
[icon replaceImageWithImage:photo];
[serverProxy updateProfileAvatar:photo];
[self dismissModalViewControllerAnimated:YES];
imgPicker.delegate = nil;
}
- (IBAction)editAvatar:(id)sender {
NSString *camera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? [WPLanguage get:#"SignupPhotosheetCamera"] : nil;
UIActionSheet *photoSheet = [[UIActionSheet alloc] initWithTitle:[WPLanguage get:#"SignupPhotosheetTitle"]
delegate:self
cancelButtonTitle:[WPLanguage get:#"CANCEL"]
destructiveButtonTitle:nil
otherButtonTitles:[WPLanguage get:#"SignupPhotosheetGallery"], camera, nil];
[photoSheet showInView:self.view];
}
Just a guess (you need to add more details): Does your viewController, which implements the UITabBarDelegate, set the selected item on viewWillAppear: (or one of the other methods called?
Apple provides the code to use MFMailComposeViewController.but it uses
- (IBAction)buttonPressed
{
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc]init];
controller.mailComposeDelegate = self;
[controller setSubject:#"In app email..."];
[controller setMessageBody:#"...a tutorial from mobileorchard.com" isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
by default it uses bottom UP transition.suppose if i want to use following, it gives wrong
ouput.can i use other add subview like that instead of presentModalViewController
{
UIViewAnimationTransition trans = UIViewAnimationTransitionFlipFromRight;
[UIView beginAnimations: nil context: nil];
[UIView setAnimationTransition:trans forView: [self view] cache: YES];
[self presentModalViewController: controller animated:YES];
[UIView commitAnimations];
}
it works correctly for other view controller,but it did not work in MFMailComposeViewController any help please?
hi i have done like this,but current view controller flips, and then composer comes from
bottom..?will you help?
- (IBAction)clickedMailButton:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mcontroller = [[MFMailComposeViewController alloc]init];
//[mcontroller setSubject:#"My Pocket Schedule"];
[mcontroller setTitle:#"New Message"];
[mcontroller setMessageBody:#"Check out My Pocket Schedule in the iTune Store" isHTML:NO];
mcontroller.mailComposeDelegate = self;
UIViewAnimationTransition trans = UIViewAnimationTransitionFlipFromRight;
[UIView beginAnimations: nil context: nil];
[UIView setAnimationTransition:trans forView: [self view] cache: YES];
[self presentModalViewController:mcontroller animated:YES];
[UIView commitAnimations];
[mcontroller release];
}
Use the modalTransitionStyle property:
MFMailComposeViewController *mcontroller = [[MFMailComposeViewController alloc] init];
[mcontroller setTitle:#"..."];
[mcontroller setMessageBody:#"..." isHTML:NO];
mcontroller.mailComposeDelegate = self;
mcontroller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:mcontroller animated:YES];
[mcontroller release];
Try
presentModalViewController:withTransition: