Why isn't the *textToShare appearing after clicking my #"Share" button? - objective-c

I want to it so when a player presses the #"share" button they can text and email a message and url to a friend. The code I wrote says a message and url but when I press Share and then either messages or email it's just an empty text box.
Also the last line seems to have problems.
It tells me there's NO visible #interface for 'MyScene' declares the selector 'presentViewController:activityVC animated:YES completion'
This is my IBAction
- (IBAction)shareAction:(id)sender {
//
NSLog(#"share");
NSString *textToShare = #"YOOO CHECK IT OUT";
UIImage *imageToShare = [UIImage imageNamed:#"beck.png"];
NSURL *urlToShare = [NSURL URLWithString:#"http://www.google.com"];
NSArray *activityItems = #[textToShare, imageToShare, urlToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];
UIActivityViewControllerCompletionHandler myBlock = ^(NSString *activityType,BOOL completed) {
NSLog(#"%#", activityType);
if(completed) {
NSLog(#"completed");
} else
{
NSLog(#"cancled");
}
[activityVC dismissViewControllerAnimated:YES completion:Nil];
};
activityVC.completionHandler = myBlock;
[self presentViewController:activityVC animated:YES completion:nil];
}

You have mention mail and message in excludedActivityType then how come it will in uiActivityViewController
remove Both from exclude and try to run.

Related

Check if user shares with Twitter

I am using an UIActivityViewController to show my share-dialogue.
How can I check if the user is using Twitter/Facebook/etc.?
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
UIPopoverPresentationController *presentationController = [activityViewController popoverPresentationController];
[self presentViewController:activityViewController animated:YES completion:nil];
I want to customize the Text for sharing with different services, so this would be really helpful.
You can use setCompletionWithItemsHandler like this:
UPDATE - remove the if(complete)
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
[activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError)
{
//if (completed)
//{
if ([activityType isEqualToString:UIActivityTypeMail]) {
//code for email
} else if ([activityType isEqualToString:UIActivityTypeMessage]) {
//code for iMessage
} else if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
//code for Facebook
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
//code for twitter
}
//}
}];
UIPopoverPresentationController *presentationController = [activityViewController popoverPresentationController];
[self presentViewController:activityViewController animated:YES completion:nil];

UIActivityViewController presenting issue iphone

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[message] applicationActivities:nil];
activityVC.excludedActivityTypes = nil;
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed)
{
activityVC.completionHandler = nil;
// if (completed)
// [Utils alertMessage:kNSLocalizedString(#"TITLE20_KEY#0", nil) title:nil delegate:nil cancelButton:kNSLocalizedString(#"OK_KEY#0", nil) otherButton:nil hiden:nil];
[activityVC dismissViewControllerAnimated:YES completion:^{
_isSharing = NO;
}];
}];
if ([self respondsToSelector:#selector(popoverPresentationController)]) {
activityVC.popoverPresentationController.sourceView = self.view;
}
[self presentViewController:activityVC animated:YES completion:^{
_isSharing = YES;
}];
I am getting the below warning.
Warning: Attempt to present
on which is already presenting (null)
Check for your Viewcontroller as its giving self as null.
You can check tutorial for UIActivityViewController from below link
http://www.amaniphoneblog.com/2014/09/uiactivityviewcontroller-ios-tutorial.html
Use this method to call the function contains presenting UIActivityViewController
[self performSelector: #selector(urMethodToPresentActivityController) withObject: nil afterDelay: 0.1];`

In App Email - MFMailComposeViewController - Error Message

My code is as follows:
ViewController.m:
#import "ViewController.h"
#import "MyScene.h"
#import "MainMenu.h"
#import <MessageUI/MessageUI.h>
#implementation ViewController
- (IBAction)showMailPicker:(id)sender
{
// You must check that the current device can send email messages before you
// attempt to create an instance of MFMailComposeViewController. If the
// device can not send email messages,
// [[MFMailComposeViewController alloc] init] will return nil. Your app
// will crash when it calls -presentViewController:animated:completion: with
// a nil view controller.
if ([MFMailComposeViewController canSendMail])
// The device can send email.
{
[self displayMailComposerSheet];
}
else
// The device can not send email.
{
self.feedbackMsg.hidden = NO;
self.feedbackMsg.text = #"Device not configured to send mail.";
}
}
- (IBAction)showSMSPicker:(id)sender
{
// You must check that the current device can send SMS messages before you
// attempt to create an instance of MFMessageComposeViewController. If the
// device can not send SMS messages,
// [[MFMessageComposeViewController alloc] init] will return nil. Your app
// will crash when it calls -presentViewController:animated:completion: with
// a nil view controller.
if ([MFMessageComposeViewController canSendText])
// The device can send email.
{
[self displaySMSComposerSheet];
}
else
// The device can not send SMS.
{
self.feedbackMsg.hidden = NO;
self.feedbackMsg.text = #"Device not configured to send SMS.";
}
}
- (void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Support for Brick Smasher Extreme"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"help.bricksx#gmail.com"];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
// Attach an image to the email
//NSString *path = [[NSBundle mainBundle] pathForResource:#"rainy" ofType:#"jpg"];
//NSData *myData = [NSData dataWithContentsOfFile:path];
//[picker addAttachmentData:myData mimeType:#"image/jpeg" fileName:#"rainy"];
// Fill out the email body text
NSString *emailBody = #"Message:";
[picker setMessageBody:emailBody isHTML:YES];
//[self presentViewController:picker animated:YES completion:NULL];
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:picker
animated:YES
completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
self.feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
self.feedbackMsg.text = #"Result: Mail sending canceled";
break;
case MFMailComposeResultSaved:
self.feedbackMsg.text = #"Result: Mail saved";
break;
case MFMailComposeResultSent:
self.feedbackMsg.text = #"Result: Mail sent";
break;
case MFMailComposeResultFailed:
self.feedbackMsg.text = #"Result: Mail sending failed";
break;
default:
self.feedbackMsg.text = #"Result: Mail not sent";
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
#end
The error is: Warning: Attempt to present <MFMailComposeViewController: 0xbf95170> on <UIViewController: 0xbf78d10> whose view is not in the window hierarchy!
I'm trying to send email through my app. Using MFMailComposeViewController, it always gives an error (above). But when I use the SMS part, there is no problem at all.
Using: Xcode 5, iOS 7, iPhone Retina 4"
Your problem is likely here:
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:
First of all, you're already calling this from a ViewController, so you can just [self presentViewController:]. It does sound like maybe you're calling your displayMailComposerSheet before ViewController's view is visible (viewDidLoad or viewWillAppear maybe), which would raise the error you're seeing.
use
[self presentViewController:picker animated:YES completion:nil];
instead of
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:picker animated:YES completion:nil];

Compose UIActivityTypeMessage with UIImage

Was wandering if anyone can offer some insight. For the life of me I can't figure out how to send a UIImage with UIActivityTypeMessage all though some say it is possible.
The docs say:
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIActivity_Class/Reference/Reference.html
UIActivityTypeMessage
The object posts the provided content to the Messages app. When using this service, you can provide NSString and NSAttributedString objects as data for the activity items. You may also specify NSURL objects whose contents use the sms scheme.
Available in iOS 6.0 and later.
Declared in UIActivity.h.
So to my understanding I can only send NSString/NSURL. I don't see you it is possible.
I'm using this:
UIImage *image; // some image here.
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[ image ] applicationActivities:nil];
Any help would be much appreciated.
Here's how to use UIActivityViewController universally, with text and an image.
NSMutableArray *items = [NSMutableArray new];
[items addObject:#"Hello world!"];
[items addObject:[UIImage imageNamed:#"MyImage"]];
NSArray *activityItems = [NSArray arrayWithArray:items];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self presentViewController:activityViewController animated:YES completion:nil];
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIPopoverController *aPopoverController = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[aPopoverController presentPopoverFromBarButtonItem:self.actionButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
// Examples:
// iPhone:
// http://anglerally.com/wp-content/uploads/2012/09/6-Default-568h#2x.png
// http://anglerally.com/wp-content/uploads/2012/09/7-Default-568h#2x.png
// iPad:
// http://anglerally.com/wp-content/uploads/2012/09/6-Default-Portrait#2xipad.png
// http://anglerally.com/wp-content/uploads/2012/09/7-Default-Portrait#2xipad.png
While it seems possible to get the message app to appear in the share sheet using UIActivityViewController in iOS6, there is some secret to get it to work--iOS7 is a different story, passing the NSURL to the asset works out of the box.
Not sure you are getting your images from the ALAssetsLibrary, but if so, grab the ALAsset using its NSURL rather than pulling the UIImage. The share sheet appears in a fraction of the time. This isn't exactly the code I'm using, but similar. But on iOS6, the activity view controller will not show the messages app so long as there is an image attached.
- (void) presentActivityViewController:(ALAsset*) asset {
// add this to your viewController class
NSDictionary *urls = [asset valueForProperty:ALAssetPropertyURLs];
NSURL *url;
if ([urls count]) {
for (NSString *key in urls) {
// I'm making an assumption that the URL I want is the first URL in the dictionary
url = [urls objectForKey:key];
break;
}
}
NSArray *activityItems = #[url];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}

reading a value from preferences

I have just recently implemented the inAppSettings kit. My goal was to load my default view and then add a navigation bar button item to the right called "settings". Once the user pressed the settings button it would take them to my settings bundle where they would make a choice to which website they wanted, and then press back which would load up my default view once again with webview loaded once again with the new url.
I have implemented everything that I just described above but once the selection is made within the settings and the user presses back (aka dismisses the settings view to go back to the default view), the app crashes and I have no idea why. My code is below and if anyone knows why this is happening, it would be much appreciated. Let it be noted that once I run the app again after it crashes, the website loads correctly based on the settings they chose before it crashed.
P.S. the options the user can click to select are: Google, stackoverflow.
ERROR Message: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Upcoming settingsViewControllerDidEnd:]: unrecognized selector sent to instance 0x281c70'
Thank you in advance
- (IASKAppSettingsViewController*)appSettingsViewController {
if (!appSettingsViewController) {
appSettingsViewController = [[IASKAppSettingsViewController alloc] initWithNibName:#"IASKAppSettingsView" bundle:nil];
appSettingsViewController.delegate = self;
}
return appSettingsViewController;
}
-(IBAction)selectSettings {
UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:self.appSettingsViewController];
//[viewController setShowCreditsFooter:NO]; // Uncomment to not display InAppSettingsKit credits for creators.
// But we encourage you not to uncomment. Thank you!
self.appSettingsViewController.showDoneButton = YES;
[self presentModalViewController:aNavController animated:YES];
[aNavController release];
}
-(NSDictionary *)intialDefaults {
NSArray *keys = [[[NSArray alloc] initWithObjects:kPicture, nil] autorelease];
NSArray *values= [[[NSArray alloc] initWithObjects: #"none", nil] autorelease];
return [[[NSDictionary alloc] initWithObjects: values forKeys: keys] autorelease];
}
-(void)setValuesFromPreferences {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults registerDefaults:[self intialDefaults]];
NSString *picturePreference= [userDefaults stringForKey:kPicture];
if([picturePreference isEqualToString:#"google"]) {
[self getUpcoming:#"http://www.google.ca"];
} else
if ([picturePreference isEqualToString:#"stackoverflow"]) {
[self getUpcoming:#"http://www.stackoverflow.com"];
} else {
[self getUpcoming:#"http://www.yahoo.com"];
}
}
-(void)getUpcoming:(id) hello {
NSURL *url= [NSURL URLWithString:hello];
NSURLRequest *requestURL= [NSURLRequest requestWithURL:url];
[web loadRequest:requestURL];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
web.hidden=NO;
[spinner stopAnimating];
[load_message dismissWithClickedButtonIndex:0 animated:TRUE];
pic1.hidden=YES;
}
-(void) loadMethod {
load_message = [[UIAlertView alloc] initWithTitle:#"Loading..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
spinner= [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(135.0, 60.0);
[load_message addSubview:spinner];
[load_message show];
[spinner startAnimating];
[self performSelector:#selector(setValuesFromPreferences) withObject:nil afterDelay:0.0];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *settingButton= [[UIBarButtonItem alloc]
initWithTitle:#"Settings"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(selectSettings)];
self.navigationItem.rightBarButtonItem = settingButton;
web.hidden=YES;
pic1.hidden=NO;
}
- (void) viewDidAppear:(BOOL)animated {
[self loadMethod];
}
Do you implement the InAppSettingKit delegate? Add this to your current class above
- (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController *)sender
{
// dismiss the view here
[self dismissModalViewControllerAnimated:YES];
// do whatever you need to do
}