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!
Related
I have this code
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:#[self.imageView.image] applicationActivities:nil];
__weak typeof(self) weakSelf = self;
UIActivityViewControllerCompletionWithItemsHandler completionHandlerBlock = ^void(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError) {
if (activityType == UIActivityTypeSaveToCameraRoll && completed) {
[EkoAlertService presentAlertOnViewController:weakSelf
withTitle:NSLocalizedString(#"save_photo_dialog_title", nil)
message:NSLocalizedString(#"save_photo_dialog_message", nil)
completion:nil];
}
};
[activityViewController setCompletionWithItemsHandler:completionHandlerBlock];
[self presentViewController:activityViewController animated:YES completion:nil];
It works fine in the case when user click allow permission to Save Image, but when they click don't allow permission I expect it to return some error but activityError is nil and I have no way to check whether you successfully save image to camera roll. Both cases return completed = YES
Is there a way to check this?
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:^{}];
});
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 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?
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.