UIActivityViewController dismiss MailViewController - objective-c

I've try to send an email over the UIActivityViewController and it works fine, but the mail view is not dismissed after the email was send. It also wont't dismiss when the user has pushed the Cancel button.
What can I do to fix the problem?
Here the code I have used:
//conf share view
NSString *textToShare = #"Teststring!";
UIImage *imageToShare = fetchedImage;
NSURL *urlToShare = [NSURL URLWithString:string_url];
NSArray *activityItems = [[NSArray alloc] initWithObjects:textToShare, imageToShare,urlToShare,nil];
UIActivity *activity = [[UIActivity alloc] init];
NSArray *applicationActivities = [[NSArray alloc] initWithObjects:activity, nil];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:applicationActivities];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact,
UIActivityTypePostToWeibo,
UIActivityTypeCopyToPasteboard,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint];
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
NSLog(#"CompletionHandler was called!");
}];
Thanks!

I have the solution. You want to present the UIActivityViewController in your fundamental, i.e. your highest or last view controller. In my case, this worked:
[[[self parentViewController] parentViewController] presentViewController:activityViewController animated:YES completion:nil];
Try out yourself how many times you have to call parentViewController until you are accessing the highest view controller. In my simple MasterView/DetailView app, I had to call it twice.

it always come in this completion handler after the posting is done weather it was successfull or not
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if (completed)
{
UIAlertView *objalert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Posting was successful" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objalert show];
objalert = nil;
}else
{
UIAlertView *objalert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Posting was not successful" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objalert show];
objalert = nil;
}
}];

then try to dismiss it on the completitionHandler
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
//Dismiss here
}];

Related

MFMessageComposeViewController - Getting stuck after sending SMS in ios7

After sending SMS from my phone(it's working properly) in my project using the MFMessageComposeViewController I can't get back to the app or press cancel. Instead I'm stuck in Apple SMS interface and I can't do anything else then to restart the app. Do I miss something in didFinishWithResult or could it be something else? Thanks
- (IBAction)sendSMS:(id)sender {
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= #"Hi";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
#"", nil];
controller.delegate = self;
[self dismissViewControllerAnimated:YES
completion:nil];
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

Adding NSData attachment to Twitter as an image post

Currently my app gives the option to save to device, or email, the latter attaching the image automatically to the mail, I am looking to add a post to twitter option, simply attaching the image and posting to Twitter, I have done this a few times with other apps, but cannot seem to get this one working.
Here is the process for email;
-(void)displayComposerSheet
{
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#""];
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/email.png"];
[mSplashView SaveResultImage:path];
[mail addAttachmentData:[NSData dataWithContentsOfFile:path] mimeType:#"image/png" fileName:#"Attached image"];
NSString *msg = [NSString stringWithFormat:#"I made this image!", [UIDevice currentDevice].model];
NSString* mailcontent = [NSString stringWithFormat:#"<br> %# <br>", msg];
[mail setMessageBody:mailcontent isHTML:YES];
[self presentModalViewController:mail animated:YES];
[mail release];
}
I am struggling to see how I can use similar to attach the image to Twitter, I currently use this code, but it crashes when attempting to post;
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[self presentViewController:twitter animated:YES completion:nil];
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/email.png"];
[mSplashView SaveResultImage:path];
[twitter setInitialText:#"I made this image!"];
[twitter addURL:[NSData dataWithContentsOfFile:path]];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Thank you" message:#"Posted successfully to Twitter." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}
if(res == TWTweetComposeViewControllerResultCancelled) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Cancelled" message:#"You Cancelled posting the Tweet." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}
[self dismissViewControllerAnimated:YES completion:nil];
};
}
Normally I could simply call [twitter addImage:]; but unfortunately it seems the image is not grabbed correctly without going through the processes above in the mail sheet.
You can't pass NSData for -addURL: method.
If your image store on disk, you can create image with [UIImage imageWithContentsOfFile:imagePath] method. Next add it with -addImage:
NSString* path =[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/imgtweet.png"];
[UIImagePNGRepresentation(imageView.image) writeToFile:path atomically:YES];
//NSLog(#"path %#",path);
UIImage *new_image = [UIImage imageWithContentsOfFile:path];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:self.txtContent.text];
[tweetSheet addImage:new_image];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
Using Previous Answer,Thanks :)
Happy coding :)

iOS UIAlertView Other Button Action

I have this code currently for my UIAlertView:
if (counter > highscore) { //if highscore is achieved show an alert
highscore = counter;
NSString *nssHighscore = [NSString stringWithFormat:#"%i", highscore];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Congratulations, You Got A New High Score!!"
message:nssHighscore
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:#"Share"];
[alert show];
}
I want to run this code when the 'Share' button is clicked on the Alert
- (IBAction)PostToFacebook:(id)sender {
NSString *nssHighscore = [NSString stringWithFormat:#"%i", highscore];
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"My New Highscore is %d - Try to Beat it!, highscore]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
So far I've been following guides and questions on stack overflow but can't get it working.
Thanks
You'll want to use the alertView:clickedButtonAtIndex: delegate method, and check that the index matches [alertView firstOtherButtonIndex].
Like this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Congratulations, You Got A New High Score!!"
message:nssHighscore
delegate:self // <== changed from nil to self
cancelButtonTitle:#"Ok"
otherButtonTitles:#"Share"];
And in the same class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView firstOtherButtonIndex]) {
[self PostToFacebook:self];
}
}

How to deal with this NSString correctly with cocos2d?

I'm new with cocos2d so i've got a problem with my In-App Purchase Class Helper. I wrote game in Cocoa Touch and this class working perfectly, but i'm writing now the same game in Cocos2d and problem is with NSString.
Here are some snippets.
This method is called as first when i clicked some button. As you see completeIdentifier is simple string with bundleIdentifier and some string parameter. It's ok, in this place i can log this completeIdentifier.
- (void)prepareToPurchaseItemWithIdentifier:(NSString *)aIdentifier showAlertWithTitle:(NSString *)title description:(NSString *)description delegate:(id)aDelegate{
self.delegate = aDelegate;
identifier = aIdentifier;
completeIdentifier = [NSString stringWithFormat:#"%#.%#", [[NSBundle mainBundle] bundleIdentifier], aIdentifier];
askToPurchase = [[UIAlertView alloc] initWithTitle:title message:description delegate:self cancelButtonTitle:nil otherButtonTitles:#"Later", #"Yes", nil];
askToPurchase.delegate = self;
[askToPurchase show];
}
Next method is UIAlertViewDelegate method. Is called when i click YES on alertView from first method.
#pragma mark - UIAlertViewDelegate Methods
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"%#", completeIdentifier);
if (alertView == askToPurchase) {
NSLog(#"[TSIAPHelper] Clicked YES. Prepare...");
if ([SKPaymentQueue canMakePayments]) {
NSLog(#"[TSIAPHelper] Prepare to purchase [%#].",completeIdentifier);
SKProductsRequest *request =[[SKProductsRequest alloc] initWithProductIdentifiers:
[NSSet setWithObject:completeIdentifier]];
request.delegate = self;
[request start];
pleaseWait = [[UIAlertView alloc] initWithTitle:#"Please wait..." message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activity startAnimating];
[pleaseWait addSubview:activity];
activity.frame = CGRectMake(125, 50, 36, 36);
[pleaseWait show];
}
else {
NSLog(#"[TSIAPHelper] Purchase [FAILURE]. Prohibited by Parentar Control or something like that.");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Prohibited." message:#"Parental Control is enabled, cannot make a purchase. Turn off and try again." delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alert show];
}
}
}
The problem is: Whenever and anywhere i want to Log variable completeIdentifier i've got crash with selected line: 0x39e965d0: ldr r3, [r4, #8] but i don't know what's that mean. And this line is selected:
NSLog(#"%#", completeIdentifier);
How can i fix it? In Cocoa Touch is working perfectly. When i use cocos2d isn't.
I guess you aren't using ARC. In this case completeIdentifier will be autoreleased.
Cocos2d clears the autoreleasepool every frame, whereas in Cocoa this is not strictly defined but may still crash. You can fix this by retaining or copying the string.
completeIdentifier = [NSString stringWithFormat:#"%#.%#", [[NSBundle mainBundle] bundleIdentifier], aIdentifier];
completeIdenfitier = [completeIdentifier retain];

Two UIAlertView consecutively in didFinishLaunchingWithOptions

I want two alert views to show up only when the user opens my application for the first time -- the second to appear after the first is dismissed. I have it set up to only show the UIAlertViews when it has not been shown before and I do not need help with this. I need help figuring out how to display two alert views in a row when this is the case.
-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex does not work for me.
Here is the code I have -- remember this is in didFinishLaunchingWithOptions:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:#"DidFirstLaunch"];
if (!didFirstLaunch) {
[defaults setBool:YES forKey:#"DidFirstLaunch"];
UIAlertView *successAlert = //not important
[successAlert show];
[successAlert release];
//Somehow show second alert after the first is dismissed
}
I'm gonna post a very simple solution using GCD & blocks (GCD part is just in case the alert view is created on another thread then the main thread, callback should be safe to perform on the main thread). Remember, I just coded this in like 5 mins, so you definitely should work on improving the code. One thing that's a bit ugly is the delegate parameter that is overridden in my subclass. The interface of the subclass could be changed a bit to make it more obvious of what happens ...
Anyway, here goes ...
First create a subclass of UIAlertView, make it look somewhat like the following ...
#interface FSAlertView () <UIAlertViewDelegate>
#property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex);
#end
#implementation FSAlertView
#synthesize dismissHandler = _dismissHandler;
- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler
{
self.dismissHandler = dismissHandler;
self.delegate = self;
[self show];
}
// Alert view delegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^ {
if (_dismissHandler)
{
_dismissHandler(buttonIndex);
}
});
}
Now in the app we can create alert views like the following ...
FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:#"Alert 1"
message:#"Some message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Show 2nd Alert", nil];
[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) {
NSLog(#"button pressed: %d", buttonIndex);
if (buttonIndex == 1)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Alert 2"
message:#"Hi!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
}
}];
If i understand your question correctly , then this may help:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[firstAlert show];
[self performSelector:#selector(test:) withObject:firstAlert afterDelay:2];
[firstAlert release];
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[secondAlert show];
[self performSelector:#selector(test:) withObject:secondAlert afterDelay:2];
[secondAlert release];
-(void)test:(UIAlertView*)alert{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
This will show two alert views one after the other.
NOTE: I am not sure if you are dismissing the alerts with cancel button so i am dismissing them automatically after few seconds.
Try this:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[firstAlert setTag:444];
[firstAlert show];
firstAlert = nil;
AlertView Delegate Method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case 444:
{
//Cancel ButtonIndex = 0
if (buttonIndex == 1) {
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Title 2" message:#"Message2" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Dismiss", nil];
[secondAlert setTag:555];
[secondAlert show];
secondAlert = nil;
}
}
break;
case 555:
{
if (buttonIndex == 1) {
NSLog(#"Code Here");
}
}
break;
}
}