UIActivityViewController for Facebook not Showing Default Text

后端 未结 6 1341
心在旅途
心在旅途 2020-12-02 13:04

I am using a UIActivityViewController which provides some default text and a link. With all social mediums (sms, email, twitter) the default text and URL are shown. Howeve

6条回答
  •  抹茶落季
    2020-12-02 14:00

    I have done one trick to share text and image from UIActivityViewController in iOS. First I need to find if user have clicked on facebook icon from UIActivityView and then open the share dialog of facebook with text and image. This is the only way to share for both text and image. Below is my code. I hope this can save time for the developers. Since Facebook have changed there policies and its really hard to share pre fill text.

    Please add UIActivityItemSource in .h file as Delegate

    This will be called when user click on share button to show UIActivityViewController.

    NSString *textObject = [NSString stringWithFormat:@"%@, %@",shareString,timeDuration];
    NSMutableArray *activityItems = [NSMutableArray arrayWithObjects:self, textObject, shareImage, nil];
    activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];
    

    And When user click on facebook icon from it.

    pragma mark - UIActivityItemSource Protocol

    - (id)activityViewController:(UIActivityViewController )activityViewController itemForActivityType:(NSString )activityType {
        
        if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
        
            [self dismissViewControllerAnimated:activityController completion:nil];
            [self performSelector:@selector(postToFacebook) withObject:self afterDelay:1.0];}
        return nil;
    }
    
    -(void)postToFacebook {
        
        [FBSDKSettings setAppID:@""];
        NSArray *imagesListingArray = [[item objectForKey:@"imgs"] valueForKey:@"img"];
        NSString * shareImage = [imagesListingArray objectAtIndex:0];
        
        FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
        content.contentTitle = @"F-Spot";
        content.contentDescription = [NSString stringWithFormat:@"%@  %@", [item valueForKey:@"event_name"],[item valueForKey:@"duration"]];
        content.imageURL = [NSURL URLWithString:shareImage];
        
        FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init];
        shareDialog.mode = FBSDKShareDialogModeNative;
        if(shareDialog.canShow) {
            shareDialog.mode = FBSDKShareDialogModeFeedBrowser;
        }
        shareDialog.shareContent = content;
        shareDialog.delegate = self;
        [shareDialog show];
    
    }
    

提交回复
热议问题