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.
Related
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!
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:^{}];
});
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.
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.
I'm getting the following result when I try to initialize the Messenger component of UIActivityViewController:
I'm using this code to instantiate my UIActivityViewController:
NSString *textToShare = #"Text that will be shared";
NSArray *itemsToShare = [[NSArray alloc] initWithObjects:textToShare, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects: UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo,UIActivityTypePostToFacebook,UIActivityTypePostToTwitter, nil];
[activityVC setValue:#"My Subject Text" forKey:#"subject"];
[self presentViewController:activityVC animated:YES completion:nil];
I then get the following error in my logs when I attempt to send with Message
Launch Services: Unable to find app identifier com.apple.MobileSMS
2013-07-31 01:27:15.180 Coolist[54111:c07] Application tried to push a nil view controller on target <MFMessageComposeViewController: 0xa38db30>.
Is this just a simulator problem?