UIActivityViewController show showing ios objective c - ios

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:^{}];
});

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!

UIActivity Controller linkedIn Post is not working

LinkedIn Sharing is not working in UIActivity view controller(Without SDK), while trying to share the post. LinkedIn post is getting crashed and I am getting a crashlog like linkedIn : plugin com.linkedin.LinkedIn.ShareExtension invalidated
NSArray *items = #[shareContentHeadingString,shareUrl];
activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:items applicationActivities:nil];
NSArray *excludeActivities = #[UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToWeibo];
activityViewController.excludedActivityTypes = excludeActivities;
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")){
activityViewController.popoverPresentationController.sourceView = self.view;
}
if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")) {
sharePopOverController = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
shareRect = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 1, 1);
sharePopOverController.delegate = self;
[sharePopOverController presentPopoverFromRect:shareRect inView:self.view permittedArrowDirections:0 animated:YES];
}
else{
[self presentViewController:activityViewController animated:YES completion:nil];
}
}
else{
[self presentViewController:activityViewController animated:YES completion:nil];
}
I faced same problem. then i removed URL and Image and passed only string value to LinkedIn. and it worked.

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

iOS how to implement the official share function

Include facebook, twitter, message, email and so on ,like this:
In your ViewController.m :
NSString *message = #"The Flyer http://flyerdream.tumblr.com";
UIImage *image = [UIImage imageNamed:#"flyer"];
NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:message, image, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:arrayOfActivityItems applicationActivities:nil];
[self.navigationController presentViewController:activityVC animated:YES completion:nil];

iOS Memory Leak with Activity View

I just discovered a memory leak in my app but I'm not really sure how to go about fixing it. It involves an activity view which is used to share the url of the current article being viewed from an rss feed. I'm not really sure why the leak is happening. Any ideas or advice?
The method in question for the iPhone version is:
- (void) showMenu
{
NSURL *urlToShare = hackyURL;
NSArray *activityItems = #[urlToShare];
TUSafariActivity *activity = [[TUSafariActivity alloc] init];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:#[activity]];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
EDIT: I've fixed the iPhone leak by changing the above code to this:
__block UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:#[activity]];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:YES completion:^{activityVC.excludedActivityTypes = nil; activityVC = nil;}];
The iPad version is essentially the same except that it uses a popover to display the activity view.
- (void)showMenu
{
NSURL *urlToShare = hackyURL;
NSArray *activityItems = #[urlToShare];
TUSafariActivity *activity = [[TUSafariActivity alloc] init];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:#[activity]];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];
if ([popover isPopoverVisible])
{
[popover dismissPopoverAnimated:YES];
popover = nil;
}
else
{
self.popover = [[UIPopoverController alloc]initWithContentViewController:activityVC];
[self.popover presentPopoverFromBarButtonItem:systemAction permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
I've fixed the iPhone leak by changing the above code to this:
__block UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:#[activity]];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePostToWeibo, UIActivityTypeSaveToCameraRoll];
[self presentViewController:activityVC animated:YES completion:^{activityVC.excludedActivityTypes = nil; activityVC = nil;}];
but I can't figure out how to fix the leak for the popover on iPad.
if ([self.popover isPopoverVisible])
{
[self.popover dismissPopoverAnimated:YES];
self.popover = nil;
}
else
{
self.popover = [[UIPopoverController alloc]initWithContentViewController:activityVC];
[self.popover presentPopoverFromBarButtonItem:systemAction permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Hope it will resolve your issue.

Resources