我正在使用UIActivityViewController来显示共享。在列表中,当我选择“邮件”应用程序时,主题和正文已正确设置,而在Gmail应用程序中则为空。

- (void)shareAVideoWithSubject:(NSString*)subject Link:(NSString *)string onViewController:(UIViewController *)viewController fromView:(UIView *)view {

    _activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:@[string]
                                      applicationActivities:nil];
    _activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypeAirDrop];
    [_activityViewController setValue:subject forKey:@"subject"];


    UIWindow *window = [[[UIApplication sharedApplication] delegate]window];

    //if iPhone
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        if(!viewController){
            viewController = ((SWRevealViewController*)window.rootViewController).presentedViewController;
        }
        [viewController presentViewController:_activityViewController
                                     animated:YES
                                   completion:nil];

    }
    //if iPad
    else
    {
        // Change Rect to position Popover
        popup = [[UIPopoverController alloc] initWithContentViewController:_activityViewController];
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:view];
        [popup presentPopoverFromBarButtonItem:barButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

我在StackOverFlow上检查了以下两个问题。
  • UIActivityViewController not showing body text in gmail
  • UIActivityViewController not showing body text in gmail

  • 这些不是我的问题的答案,因为它们在列表中添加了新的 Activity ,而是我希望iOS显示所有可共享的应用程序。在这种情况下,Gmail共享正文将为空。

    提前致谢。
    添加屏幕截图,

    最佳答案

    [_activityViewController setValue:subject forKey:@"subject"];是设置电子邮件主题的未公开方法。
    设置正文和主题的正确方法(iOS 7.0及更高版本)-在要共享的项目上实现UIActivityItemSource协议(protocol)。

    //  EmailItemProvider.h
    @interface EmailItemProvider : NSObject <UIActivityItemSource>
    @property (nonatomic, strong) NSString *subject;
    @property (nonatomic, strong) NSString *body;
    @end
    
    //  EmailItemProvider.m
    #import "EmailItemProvider.h"
    
    @implementation EmailItemProvider
    
    - (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
        return _body;
    }
    
    - (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
        return _body;
    }
    
    - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
        return _subject;
    }
    @end
    

    比介绍它:
    EmailItemProvider *emailItem = [EmailItemProvider new];
    emailItem.subject = @"Subject";
    emailItem.body = @"Body";
    
    UIActivityViewController *activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:@[emailItem]
                                      applicationActivities:nil];
    [self presentViewController:activityViewController animated:YES completion:nil];
    

    这将在邮件应用程序上设置正文和主题,但似乎Gmail应用程序会忽略主题并将其设置为正文。



    重要:Gmail应用程序似乎有一个错误。传递&字符会使消息主题和正文为空。请改用&amp;。其他特殊字符未经测试。

    关于ios - UIActivityViewController Gmail共享主题和正文会变空吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29765806/

    10-12 06:02