Unable to added Facebook and Twitter to UIActivityViewController - ios

Trying to share application on social median, and email, and message.
Since Email and message is working fine for me.
But Facebook,and twitter is not all showing to UIActivityViewController
Below is code which i try.
NSArray *excludeActivities = #[UIActivityTypeAirDrop,
UIActivityTypePostToFacebook,
UIActivityTypePostToTwitter];
UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:#[string, URL]
applicationActivities:nil];
activityViewController.excludedActivityTypes = excludeActivities;
[self presentViewController:activityViewController
animated:YES
completion:^{
}];
- (id)activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return NSLocalizedString(#"calling facebook", );
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return NSLocalizedString(#"calling twitter", );
} else {
return nil;
}
}
#All
Please let me know what i need to do.

For that just create one class for that like this.
SharingActivityProvider.h
#import <Foundation/Foundation.h>
#interface SharingActivityProvider : UIActivityItemProvider
#end
**SharingActivityProvider.m**
#import "SharingActivityProvider.h"
#implementation SharingActivityProvider {
}
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
// Create the default sharing string
NSString *shareString = #"";
// customize the sharing string for facebook, twitter, weibo, and google+
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
shareString = [NSString stringWithFormat:#"Attention Facebook:%#", shareString];
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
shareString = [NSString stringWithFormat:#"Attention Twitter:%#", shareString];
} else if ([activityType isEqualToString:UIActivityTypePostToWeibo]) {
shareString = [NSString stringWithFormat:#"Attention Weibo:%#", shareString];
}
return shareString;
}
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return #"";
}
#end
Then after just use below code, suppose button action.
-(IBAction)shareAppTapped:(id)sender{
SharingActivityProvider *sharingActivityProvider = [[SharingActivityProvider alloc] init];
NSArray *activityProviders = #[sharingActivityProvider,#"what ever text"];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityProviders applicationActivities:nil];
// tell the activity view controller which activities should NOT appear
activityViewController.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:activityViewController animated:YES completion:nil];
// [self.btnShareApp setBackgroundColor:OnebackgroundColorTable];
}

One mistake here is exclude activities also have facebook and twitter. Hence it will not show by any means.
Note excludedActivityTypes should be which you does not need in controller.
For example it should be:
NSArray *excludeActivities = #[UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypeAssignToContact,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList,
UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo,
UIActivityTypePostToTencentWeibo];

Related

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 to block to open default Facebook sharing in UIActivityViewController in iOS?

in my application want to open custom pop up in UIActivityViewController. So i don't want to open default sharing pop up in facebook. So how to dismiss that pop up or how to block to open that pop up in iOS.
Please give me solution ...
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dismissPopup) name:dismissMJPopUp object: nil];
//ReferFriendVC *okpopup=[[ReferFriendVC alloc]initWithNibName:#"ReferFriendVC" bundle:nil];
// [self presentPopupViewController:okpopup animationType:MJPopupViewAnimationSlideBottomTop];
UIPopoverController *popup;
MyActivityItemProvider *message = [[MyActivityItemProvider alloc] init];
NSArray *activityItems;
UIImage *myImage=[UIImage imageNamed:#"3.png"];
activityItems = #[message, myImage];
NSArray * applicationActivities = nil;
UIActivityViewController * activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityViewController.excludedActivityTypes = #[
UIActivityTypePostToWeibo,
UIActivityTypeAssignToContact,
UIActivityTypeAirDrop,
UIActivityTypeAddToReadingList,
UIActivityTypeCopyToPasteboard,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypePostToFlickr,UIActivityTypePostToTencentWeibo // Whatever you want to exclude
];
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")){
activityViewController.popoverPresentationController.sourceView =
self.view;
}
if(isiPhone)
{
[self presentViewController:activityViewController animated:YES completion:nil];
}
else{
popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[popup presentPopoverFromRect:CGRectMake(0, screenheight, 768, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
[activityViewController setCompletionWithItemsHandler:^(NSString *act, BOOL completed, NSArray *returnedItems, NSError *activityError)
{
NSLog(#"act type %#",act);
NSString *ServiceMsg = nil;
if ( [act isEqualToString:UIActivityTypeMail] )
ServiceMsg = #"Mail sent";
NSLog(#"mail sent");
if ( [act isEqualToString:UIActivityTypeAssignToContact] )
ServiceMsg = #"Post on twitter, ok!";
if ( [act isEqualToString:UIActivityTypePostToFacebook] )
ServiceMsg = #"Post on facebook, ok!";
[popup dismissPopoverAnimated:YES];
}];
And in myActivityItemProvide.h
#import "MyActivityItemProvider.h"
#implementation MyActivityItemProvider
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
NSLog(#"mail method called ");
//
if([AppDelegate NetworkRechability])
{
// FrindListVC *leavefeed=[[FrindListVC alloc]initWithNibName:#"FrindListVC" bundle:nil];
// [self.navigationController pushViewController:leavefeed animated:YES];
FBSDKAppInviteContent *content1 =[[FBSDKAppInviteContent alloc] init];
content1.appLinkURL = [NSURL URLWithString:#"https://fb.me/4931252562362626805"];
//optionally set previewImageURL
content1.appInvitePreviewImageURL = [NSURL URLWithString:#"http://files.parsdrterfsdfetfss.com/79580e8563-55656cc-456455f2-a20f-7f69e783b14d/tfss-f9e2626e-dfsdfsfsdfsdf519c-4ca3-ae47-015ffeaad0f7-fbinvite-01.png"];
// present the dialog. Assumes self implements protocol `FBSDKAppInviteDialogDelegate`
[FBSDKAppInviteDialog showWithContent:content1
delegate:self];
// [[[self parentViewController] parentViewController] presentViewController:activityViewController animated:YES completion:nil];
// activityViewController.view.hidden=YES;
}
else
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Alert" message:#"Internet Not Available" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[SVProgressHUD dismiss];
}
}
return #"No provider";
}
- (void)appInviteDialog:(FBSDKAppInviteDialog *)appInviteDialog didFailWithError:(NSError *)error
{
NSLog(#"error %#",error);
}
- (void)appInviteDialog:(FBSDKAppInviteDialog *)appInviteDialog didCompleteWithResults:(NSDictionary *)result
{
// NSLog(#"result %#",result);
}
- (id) activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController { return #""; }
#end
You can exclude or block to open default Facebook sharing in UIActivityViewController using below code:
activityViewController.excludedActivityTypes = [UIActivityTypePostToFacebook]

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.

How to know which icon is clicked in UIActivityViewController before activityController setCompletionHandleris called?

How to know which icon is clicked in UIActivityViewController before activityController setCompletionHandler:^(NSString *activityType, BOOL completed) is called ?
I want to share different text for sharing with twitter and facebook.
-(IBAction)btnSharePressedFromOffersDetail:(id)sender
{
NSString *posturl= #"facebook or twitter";
UISimpleTextPrintFormatter *printData = [[UISimpleTextPrintFormatter alloc]init];
NSArray *Itemsarray=#[posturl,printData];
UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:Itemsarray applicationActivities:nil];
[self presentViewController:activityController
animated:YES completion:nil];
[activityController setCompletionHandler:^(NSString *activityType, BOOL completed)
{
NSLog(#"Activity = %#",activityType);
NSLog(#"Completed Status = %d",completed);
if (completed)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#""
message:#"Thanks for sharing!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
}
}];
}
If your goal is to use a different text for Twitter or Facebook, it's the responsability of your shared Model who implement the UIActivityItemSource protocol.
- (id) activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
if([activityType isEqualToString:UIActivityTypePostToFacebook]) return #"Facebook text";
else if([activityType UIActivityTypePostToTwitter]) return #"Twitter text";
else return #"Default text";
}
You can go with link given below, there is tutorial which might helps you.
https://www.albertopasca.it/whiletrue/objective-c-custom-uiactivityviewcontroller-icons-and-text/
and http://nshipster.com/uiactivityviewcontroller/

Receiving NSString on my apps by using AirDrop

Is there anyone who know how to receive NSString sent over AirDrop on my app? So far, my app can send NSString over AirDrop. But I don't know how to open the NSString I send over Airdrop by using my app.
#interface ComposeViewController ()
#end
#implementation ComposeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
[textField resignFirstResponder];
return YES;
}
- (IBAction)sendButton:(id)sender {
NSString* message= self.inputTextField.text;
NSLog(#"you type: %#", self.inputTextField.text);
// enabling airdrop
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:#[message] applicationActivities:nil];
// Exclude all activities except AirDrop.
NSArray *excludedActivities = #[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
UIActivityTypePostToWeibo,
UIActivityTypeMessage, UIActivityTypeMail,
UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
controller.excludedActivityTypes = excludedActivities;
// Present the controller
[self presentViewController:controller animated:YES completion:nil];
//[self.readTextView setText:self.inputTextField.text];
}
Look into using UIActivityItemSource to send the NSString as NSData + a custom UTI for your app. Basically define a new file format, that could be as simple as NSString encoded as UTF8 data, and then on the receiving side in your app code you convert this back into an NSString. The AirDrop sample code will likely be helpful.

Resources