Please find the code and attached pic. That cancel button not working . If I click that button, that modal alone closing. It is not triggering "mailComposeController:didFinishWithResult" function..I am new to Objective c IOS. Please help me to get out of this issue. I searched in stack overflow. But I problem didn't solve
#import "ViewController.h"
#import <MessageUI/MFMailComposeViewController.h>
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIButton *send;
- (IBAction)sendMail:(id)sender;
#end
#implementation ViewController
MFMailComposeViewController* controller;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
controller= [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)sendMail:(id)sender {
[controller setSubject:#"My Subject"];
[controller setMessageBody:#"Hello there." isHTML:NO];
if (controller) [self presentViewController:controller animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
NSLog(#"Coming here");
if (result == MFMailComposeResultSent) {
NSLog(#"Mail It's away!");
}
if (result == MFMailComposeResultFailed) {
NSLog(#"Mail Error!");
}
if(result == MFMailComposeResultCancelled){
NSLog(#"Mail Error!");
}
if(result == MFMailComposeResultSaved){
NSLog(#"Mail Saved!");
}
[controller dismissViewControllerAnimated:YES completion:nil];
return;
}
[![Screen shot][1]][1]#end
Man this cancel button does nothing at all other than dismissing action sheet. If you want to get rid of mail composer you have to press cancel at upper left corner which shows you an action sheet at the bottom with options Delete Draft, Save Draft and cancel. Delete draft and save draft will dismiss the mail composer and the delegate function will be called. Cancel button will actually dismiss action sheet in order to save the mail composer from dismissing in case if user accidentally clicked that cancel button at the top
Related
Hello everyone I want to make a popover for iPhone so here is my code where I am geting wrong please suggest I want to create somthing like below pic but mine is covering the whole screen and I dont want to use segue .So basicaly I want if I click on button a resized View controller should popup how can I achieve this please help.I had followed this tutorial.
https://www.youtube.com/watch?v=UQBbJQNEDA4
#import "ViewController.h"
#import "Popup.h"
#interface ViewController ()<UIPopoverPresentationControllerDelegate>
{
Popup * popupController;
UIPopoverPresentationController *popupPresentationController;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)popupOnClick:(id)sender {
popupController=[self.storyboard instantiateViewControllerWithIdentifier:#"Popup"];
popupController.modalPresentationStyle=UIModalPresentationPopover;
popupPresentationController= [popupController popoverPresentationController];
popupPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popupController.preferredContentSize=CGSizeMake(150, 300);
popupPresentationController.delegate = self;
[self presentViewController:popupController animated:YES completion:NULL];
// in case we don't have a bar button as reference
popupPresentationController.sourceView = _popupButton;
popupPresentationController.sourceRect = _popupButton.frame;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
return UIModalPresentationNone;
}
#end
Here's your problem:
[self presentViewController:popupController animated:YES completion:nil];
popupPresentationController= [popupController popoverPresentationController];
popupPresentationController.delegate = self;
That code is in the wrong order. You must set the delegate before calling presentViewController.
can anybody help e with the login screen?
i have designed a mobile application that uses Facebook login, I've got all of that working but having issue to view the main screen.
The Facebook button logs in a user and logs them out which i wanted, but the Facebook button does not take the user to the main screen how do i do that?
i have taken screen shot of the application, the two screen shot show a user can log in and log out and the third screen shot is the home page of an application, once the user have logged in, i want the application to take the user onto the main page.
the following is the code for Facebook button
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import "FBSDKLoginKit/FBSDKLoginKit.h"
#interface LoginViewController ()
#end
#implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
#end
Instead of using Facebook button you can use custom button and add functionality
you can use following code
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Add a custom login button to your app
UIButton *myLoginButton=[UIButton buttonWithType:UIButtonTypeCustom];
myLoginButton.backgroundColor=[UIColor darkGrayColor];
myLoginButton.frame=CGRectMake(0,0,180,40);
myLoginButton.center = self.view.center;
[myLoginButton setTitle: #"My Login Button" forState: UIControlStateNormal];
// Handle clicks on the button
[myLoginButton
addTarget:self
action:#selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
// Add the button to the view
[self.view addSubview:myLoginButton];
}
// Once the button is clicked, show the login dialog
-(void)loginButtonClicked
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: #[#"public_profile"]
fromViewController:self
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
NSLog(#"Process error");
} else if (result.isCancelled) {
NSLog(#"Cancelled");
} else {
// here you can push or present controller
NSLog(#"Logged in");
}
}];
}
#end
You can use FBSDKLoginButtonDelegate for catching event when user is loged in Facebook.
- (void) loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult: (FBSDKLoginManagerLoginResult *)result error:(NSError *)error {
if (result) {
[self perfromSegueWithIdentifier:#"YourSegueIdentifier" sender:self];
}}
I think, this can help you.
My task is to create custom UIAlertView with multiline text input field and everything works pretty much well up to the point where I need to do some actions if dismiss button was tapped. As example I'm using: http://iphonedevelopment.blogspot.co.uk/2010/05/custom-alert-views.html
I have created very simple popup for testing purpose. Basically it's UIViewController xib with single button in it. The main class is demonstrated below:
#import "testViewController.h"
#interface testViewController ()
#end
#implementation testViewController
- (id)init
{
self = [super initWithNibName:#"testViewController" bundle:nil];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)showInView:(UIView *)view
{
[view addSubview:[self view]];
[[self view] setFrame: [view bounds]];
[[self view] setCenter:[view center]];
}
- (IBAction)onTap:(id)sender {
NSLog(#"All OK");
}
#end
then in root UIViewController I'm calling my custom alert:
- (IBAction)showAlert:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
testViewController *alert = [[testViewController alloc] init];
[alert showInView:[self view]];
});
}
So far I have tried Main thread or global queue or even synchronous dispatch, but everything ends up with break on:
int main(int argc, char * argv[]) {
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); <-- Thread 1:EXC_BAD_ACCESS (code=1, address=0xa000000c)
}
}
tried to add observer for the button but still no go..
Any help is much appreciated
Here what happing is "if you want to add one viewcontroller view to other viewcontroller's view then after adding of views you should convey to the compiler that second view controller is going to be child to the first"
i.e pre intimation of parent to child relationship has to be done.
now just modify your showAlert method with the following . it is working 100%.
- (IBAction)showAlert:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
testViewController *alert = [[testViewController alloc] init];
[alert showInView:[self view]];
[self addChildViewController:alert];
[alert didMoveToParentViewController:self];
});
}
Check if your UIButton is assigned multiple IBoutlets or multiple IBActions. This a common issue which happens without our notice sometimes.
It turns out that my testViewController was released by ARC before I tap the button
i am new to objective C and im trying to make a share button for twitter in my game. Im using spritebuilder to create all my buttons and sprites. All of my code lies in MainScene.m while my code for twitter share sheet is in ShareViewController.m
In my MainScene.m file i have a method sendToController, which using an object of shareViewController calls a method postToTwitter which is in ShareViewController.m. Due to some reason this tweetsheet is not visbile and from what i gather is that there is a window heirachy problem which i am not able to solve.
Here is the code in MainScene.m
ShareViewController *_shareViewController;
#synthesize shareViewController;
(void)sendToController {
_shareViewController = [ShareViewController alloc];
[_shareViewController postToTwitter];
}
Here is the code for ShareViewController.m
#implementation ShareViewController {
MainScene *_mainSceneObj;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_mainSceneObj.ShareViewController = self;
}
- (void)postToTwitter {
NSLog(#"in postToTwitter %d", [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]);
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
// Sets the completion handler. Note that we don't know which thread the
// block will be called on, so we need to ensure that any required UI
// updates occur on the main queue
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
// This means the user cancelled without sending the Tweet
case SLComposeViewControllerResultCancelled:
break;
// This means the user hit 'Send'
case SLComposeViewControllerResultDone:
break;
}
};
// Set the initial body of the Tweet
[tweetSheet setInitialText:#"just setting up my twttr"];
if (![tweetSheet addImage:[UIImage imageNamed:#"screen.png"]]) {
NSLog(#"Unable to add the image!");
}
if (![tweetSheet addURL:[NSURL URLWithString:#"http://twitter.com/"]]){
NSLog(#"Unable to add the URL!");
}
// Presents the Tweet Sheet to the user
[self presentViewController:tweetSheet animated:NO completion:^{
NSLog(#"Tweet sheet has been presented.");
}]; }
}
I am trying to allow the player to share their score by SMS when the game is over.
I have imported the framework in to my project. Imported in the my viewController.h file.
here is my viewController.h file
#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <MessageUI/MessageUI.h>
#interface myViewController : UIViewController <MFMessageComposeViewControllerDelegate> {
}
#end
I also tried to import into MyScene.h like so:
#import <MessageUI/MessageUI.h>
#interface MyScene : SKScene <MFMessageComposeViewControllerDelegate> {
}
When I want to show the SMS share, I use this code in my MyScene.m file
MFMessageComposeViewController *textComposer = [[MFMessageComposeViewController alloc] init];
[textComposer setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
[textComposer setRecipients:[NSArray arrayWithObject:nil]];
[textComposer setBody:#"Happy Happy Joy Joy!"];
[self presentViewController:textComposer animated:YES completion:NULL];
} else {
NSLog(#"Cant send text!");
}
But on this line
[self presentViewController:textComposer animated:YES completion:NULL];
I get an "No visible #interface for 'MyScene' declares the selector 'presentViewController:animated:completion:'" error.
I have tried to search for the last couple hours. Try god knows how many variations and examples from other posts/tutorials(which was good to learn a few things unrelated to this). Nothing seems the work. I starting to run out of hair to pull out. So any help would be great. I am sure for some of you Gurus here this should be a walk in the park. Thanks.
EDIT: I am not using storyboard, or the view controller for buttons/menu/game play etc...hence why I am not able to call the function from within the viewController itself.
EDIT:
So I tried what Paulw11 suggested in his link. Now I have the following errors.
in myViewController
MyScene.MyViewController = self;
I get a "Property 'MyViewController' not found on object of type 'MyScene'" error
also in MyScene.m
- (void)sendToController
{
NSLog(#"ok");
// use the already-created spriteViewController
[_MyViewController sendSMS];
}
[_MyViewController sendSMS]; line I get an "No visible #interface for 'MyViewController' declares the selector 'SendSMS'"
EDIT 2: *EDIT 2:* EDIT 2: *EDIT 2:*
I got it to open up the SMS. Small problem, it does not allow me to dismiss it /cancel.
Here is my sendSMS code:
-(void) sendSMS {
MFMessageComposeViewController *textComposer = [[MFMessageComposeViewController alloc] init];
[textComposer setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
[textComposer setRecipients:[NSArray arrayWithObject:#" "]];
NSString *body = [NSString stringWithFormat:#"Happy Day!: %i. ", _score];
[textComposer setBody:body];
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: textComposer animated: YES completion:nil];
} else {
NSLog(#"Cant send text!");
}
}
Here is my dismiss code:
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
UIViewController *vc = self.view.window.rootViewController;
[vc dismissViewControllerAnimated:YES completion:NULL];
}
EDIT 3
The following code gives me the NSLog at the correct times, but does not dismiss the window.
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
UIViewController *vc = self.view.window.rootViewController;
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Result: SMS sending canceled");
break;
case MessageComposeResultSent:
NSLog(#"Result: SMS sent");
break;
case MessageComposeResultFailed:
NSLog(#"Result: SMS sending failed");
break;
default:
NSLog(#"Result: SMS not sent");
break;
}
[vc dismissViewControllerAnimated:YES completion:NULL];
}
If you refer to the MFMessageComposeViewController Class Reference you will see that you need to present it modally using presentModalViewController:animated:. You are also responsible for dismissing it via your delegate object once you are done.
I suggest you have a look at the Message Composer sample code for an example of using the MFMessageComposeViewController class.
UPDATE
You can just dismiss the view controller that was passed to your delegate method -
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller
didFinishWithResult:(MessageComposeResult)result
{
// Notifies users about errors associated with the interface
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Result: SMS sending canceled");
break;
case MessageComposeResultSent:
NSLog(#"Result: SMS sent");
break;
case MessageComposeResultFailed:
NSLog(#"Result: SMS sending failed");
break;
default:
NSLog(#"Result: SMS not sent");
break;
}
[controller dismissViewControllerAnimated:YES completion:NULL];
}