iOS: UIActivityViewController Freezes My UI If I Press Cancel? - ios

I am currently implementing a UIActivityViewController in my app, and it's working great. If I select an action (Post to FB, Save Image, etc) then it works well and my UI responds afterwards. However, if I press cancel, the activity view is dismissed (visually) but my UI freezes (doesn't respond to touch).
This happens on my iPhone 5. However, it does not occur in the simulator.
- (IBAction)share:(id)sender {
NSString *myString = #"Download our game & beat our score.";
NSURL *myURL = [NSURL URLWithString:#"http://myurl"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[myString, myURL, _resultsImage]applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypePostToVimeo, UIActivityTypePrint];
[self presentViewController:activityViewController animated:YES completion:nil];
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
if ( completed ) {
NSLog(#"Action Completed");
} else {
NSLog(#"Action Canceled");
//If this takes place my UI freezes
}
};
}
What's wrong here?

Related

iOS - UIActivityViewController's items show incorrect position

I'm trying implement a function that can share a URL link to other app via UIActivityViewController. It works very well on most times. However, sometime it show the items in weird position like following picture.
if (!share_Text) {
return;
}
NSURL *share_Url = [NSURL URLWithString:share_Text];
if(shareImage) {
activityItems = #[ share_Url?share_Url:share_Text,
shareImage ];
}
else {
activityItems = #[ share_Url?share_Url:share_Text ];
}
NSMutableArray *activityApp = [[NSMutableArray alloc] init];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:activityApp];
activityVC.excludedActivityTypes = #[UIActivityTypeAirDrop, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll,UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList];
if(NSClassFromString(#"UIPopoverPresentationController")) {
activityVC.popoverPresentationController.sourceView = controller.view;
}
if([[UIApplication sharedApplication] respondsToSelector:(#selector(setCompletionWithItemsHandler:))]){
[activityVC setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if(completeFunc) completeFunc(activityType, completed, activityError);
}];
}else{
[activityVC setCompletionHandler:^(NSString *activityType, BOOL completed) {
if(completeFunc) completeFunc(activityType, completed, nil);
}];
}
[controller presentViewController:activityVC animated:YES completion:nil];
I have already tried to search some solution on the internet, but I don't know what keyword should I use. I'm so appreciate If anyone knows how to solve this problem or what causes this problem. Thanks!

UIActivityViewController show showing ios objective c

I'm trying to present an activity view controller. but I don't know why is this error occurs again and again. Same code I have used for my other app and its working fine over there. However, in my current app its starts crashing.
NSString *url=#"http://itunes.apple.com/us/app/APPNAME/idXXXXXXXXX";
NSString * title =[NSString stringWithFormat:#"Download App %# and get free reward points!",url];
NSArray* dataToShare = #[title];
UIActivityViewController* activityViewController =[[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypeAirDrop];
if (activityViewController == nil){
return;
}
[self presentViewController:activityViewController animated:YES completion:^{}];
Try this code with main thread mention in below.
NSString *url=#"http://itunes.apple.com/us/app/APPNAME/idXXXXXXXXX";
NSString * title =[NSString stringWithFormat:#"Download App %# and get free reward points!",url];
NSArray* dataToShare = #[title];
UIActivityViewController* activityViewController =[[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypeAirDrop];
if (activityViewController == nil){
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:activityViewController animated:YES completion:^{}];
});

mailto:recipients is not working with UIActivityViewController in iOS

I am using ActivityViewController for sending mail from App.
I was able to auto fill subject and body but not participants.
Example code :
NSURL *recipients = [NSURL URLWithString:#"mailto:foo#bar.com"]; //Not working
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:#[recipients, #"bodyText"]
applicationActivities:nil];
[activityViewController setValue:mailSubject forKey:#"Subject"];
activityViewController.excludedActivityTypes = #[UIActivityTypeMessage, UIActivityTypePostToFacebook, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypePostToWeibo];
[self.navigationController presentViewController:activityViewController
animated:YES
completion:^{
}];
MailTo participants is not working.
When i run the code, pop up is coming with selected mail app but to field is empty.

How do I resolve a crash on this line in Xcode: int retVal = UIApplicationMain(argc, argv, nil, #"AppController");

First off, I'm a newbie in programming, please bear with me guys. I'm working on an iOS game built using the Cocos2Dx engine in Xcode. The game has a Share button which is used to share scores. The button seems to work with the iPhone but crashes all the time on an iPad. Xcode points to this line each time it crashes on the iPad:
int retVal = UIApplicationMain(argc, argv, nil, #"AppController");
I figured this issue came about after I changed another piece of code to implement the functionality of the share button. The code which I changed is in the function below.
- (void) displayShare:(NSString*)strText imageIdx:(int)nIdx URL:(NSString*)strURL
{
UIActivityViewController *activityView;
if(nIdx >= 0)
{
NSString* str = [NSString stringWithFormat:#"new-arc-%d-ipad.png", nIdx+1];
UIImage* image = [UIImage imageNamed:str];
activityView = [[UIActivityViewController alloc] initWithActivityItems:#[strText, image, [NSURL URLWithString:strURL]] applicationActivities:nil];
}
else
activityView = [[UIActivityViewController alloc] initWithActivityItems:#[strText, [NSURL URLWithString:strURL]] applicationActivities:nil];
activityView.excludedActivityTypes = #[UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypePrint];
// [self.viewController presentViewController:activityView animated:YES completion:nil];
[window.rootViewController presentViewController:activityView animated:YES completion:nil];
[activityView setCompletionHandler:^(NSString *activityType, BOOL completed) {
NSLog(#"completed dialog - activity: %# - finished flag: %d", activityType, completed);
if(completed)
{
if([activityType isEqualToString: #"com.apple.UIKit.activity.PostToFacebook"] )
{
g_bArchiveMark[19] = true;
AppSettings::setArchieveInfo(19);
}
else if([activityType isEqualToString: #"com.apple.UIKit.activity.PostToTwitter"] )
{
g_bArchiveMark[20] = true;
AppSettings::setArchieveInfo(20);
}
}
}];
The line I changed is this one (which is in the function above):
[window.rootViewController presentViewController:activityView animated:YES completion:nil];
I would like to know why that line works on the iPhone devices but not on the iPad? And how could I resolve it to work on the iPad as well? Maybe an alternative piece code?
Take a look at
UIActivity activityViewController being presented modally on iPad instead of in popover
On iPad an UIActivityViewController must be displayed inside a UIPopoverController

Quirk in UIActivityViewController integration

I integrated a sharing option into my app and everything seems fine, basically I excluded every type of UIActivity except for Message, Twitter, and Facebook, and when I tested to see if it functions, what I see in the text box is
"I got a new highscore!
com.apple.UIKit.activity.Message
com.apple.UIKit.activity.PostToFacebook
com.apple.UiKit.activity.PostToTwitter".
and the text appears in each of activities.
Is there anyway to remove the text, or is it supposed to be that way?
Here is a method in my MainViewController which presents the UIActivityViewController.
-(void)sharedView {
APActivityProvider *APActivityPro = [[APActivityProvider alloc] init];
NSArray *activites = #[APActivityPro,UIActivityTypeMessage, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter];
activityController = [[UIActivityViewController alloc] initWithActivityItems:activites applicationActivities:nil];
activityController.excludedActivityTypes = #[UIActivityTypePostToWeibo,
UIActivityTypeMail,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo,
UIActivityTypeAirDrop];
[self presentViewController:activityController animated:YES completion:nil];
[activityController setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if (completed)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:NSLocalizedString(#"Post Sent!",#"the confirmation of the sent message/post")
delegate:nil
cancelButtonTitle:NSLocalizedString(#"OK",#"done")
otherButtonTitles:nil];
[alert show];
}
}
];
}
And also my code in my APActivityProvider.m file.
-(id) activityViewController:(UIActivityViewController*)activityViewController
itemForActivityType:(NSString *)activityType
{
if ([activityType isEqualToString:UIActivityTypeMessage])
return NSLocalizedString(#"I got a new highscore!",#"Comment for message");
if ([activityType isEqualToString:UIActivityTypePostToFacebook])
return NSLocalizedString(#"I got a new highscore!",#"Comment for Facebook");
if ([activityType isEqualToString:UIActivityTypePostToTwitter])
return NSLocalizedString(#"I got a new highscore!",#"Comment for twitter");
return nil;
}
- (id) activityViewControllerPlaceholderItem:(UIActivityViewController *)
activityViewController
{
return #"";
}
It would appear you are using the API incorrectly. Specifically:
NSArray *activites = #[APActivityPro,UIActivityTypeMessage, UIActivityTypePostToFacebook, UIActivityTypePostToTwitter];
activityController = [[UIActivityViewController alloc] initWithActivityItems:activites applicationActivities:nil];
Activity Items are your content (Strings, Images, etc) while applicationItems are the custom activity options you add to the default system ones (something you code such as an open in app store activity). You do not need to tell the UIActivityViewController what system activities you want. All are enabled by default.
For an example of how to use this API, I have included the following:
NSNumber *AppStoreID = [NSNumber numberWithInt:550008982];
NSArray *activityItems =
#[ AppStoreID ];
NSArray *applicationActivities =
#[ [ATFAppStoreActivity alloc] ];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:applicationActivities];
[self presentViewController:activityViewController animated:YES completion:nil];
Here, I have my subclassed UIActivity (ATFAppStoreActivity) that takes an NSNumber to display the App Store page for that app.

Resources