简体   繁体   English

使用 UIActivityViewController 感到困惑

[英]Confused using UIActivityViewController

Can you help me to understand when should I use UIActivityViewController .你能帮我理解什么时候应该使用UIActivityViewController I have a button which shares common information about my app (something like "I like this app" with link and image).我有一个按钮,用于共享有关我的应用程序的通用信息(类似于“我喜欢这个应用程序”,带有链接和图像)。 My old code was:我的旧代码是:

NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setObject:picture forKey:@"picture"];
[params setObject:link forKey:@"link"];
[params setObject:@"I like MY app!" forKey:@"caption"];
[params setObject:@"I am now using MY iPhone app." forKey:@"description"];
[params setObject:linkToTheIcon forKey:@"icon"];
[params setObject:@"including link" forKey:@"type"];
[[FacebookConnection instance] feedLink:params andDelegate:self];

Now I want to use UIActivityViewController but I'm a bit confused how to pass all those parameters to it.现在我想使用UIActivityViewController但我有点困惑如何将所有这些参数传递给它。 Or should I do things in other way?或者我应该以其他方式做事?

ADDED: So I understood that I need silent posting procedure.补充:所以我明白我需要静默发布程序。 Could you please guide me through silent post procedure using iOS 6 features (ed using built-in FB account).您能否指导我使用 iOS 6 功能(使用内置 FB 帐户编辑​​)完成静默发布过程。 For now I can't understand how to check if FB account exists on the device and if it is not how to prompt to create it?现在我不明白如何检查设备上是否存在 FB 帐户,如果不是如何提示创建它? There is a method in ACAccount store class – requestAccessToAccountsWithType:options:completion: to access an account. ACAccount 存储类中有一个方法– requestAccessToAccountsWithType:options:completion:访问帐户。 But If an account does not exists it returns an error.但是,如果帐户不存在,则会返回错误。 Many thanks in advance.提前谢谢了。

You first need to subclass UIActivity.您首先需要继承 UIActivity。

Then you need to override certain methods, including activityImage for setting the icon and performActivity for performing the action .然后你需要覆盖某些方法,包括用于设置图标的 activityImage 和用于执行操作的 performActivity 。

If instead of performing the action silently, you first need further user interaction and info for your custom activity (eg, like the Twitter post for the standard UIActivity), you should override activityViewController rather than performActivity.如果不是静默执行操作,您首先需要自定义活动的进一步用户交互和信息(例如,标准 UIActivity 的 Twitter 帖子),您应该覆盖 activityViewController 而不是 performActivity。

After you have subclassed UIActivity (as, eg, MyActivity), you should create an instance of MyActivity and make it an element of the applicationActivities array that you pass to initWithActivityItems:applicationActivities:.在您对 UIActivity 进行子类化(例如 MyActivity)之后,您应该创建 MyActivity 的一个实例,并将其设置为您传递给 initWithActivityItems:applicationActivities: 的 applicationActivities 数组的一个元素。

Have a look at the documentation for UIActivity for exactly what you need to override when subclassing and for icon requirements.查看UIActivity 的文档,了解在子类化和图标要求时需要覆盖的内容。

Hope this helps a little希望这有所帮助

In your situation it seems obvious that you should not use UIActivityViewController because you want to post on Facebook and not on twitter or anywhere else, right?在您的情况下,很明显您不应该使用UIActivityViewController因为您想在 Facebook 上而不是在 twitter 或其他任何地方发帖,对吧? Firstly you need to get access to user's account.首先,您需要访问用户的帐户。 You do this like this:你这样做:

-(void)requestBasicPermissionsForFacebookAccount {
    ACAccountType * facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSArray * permissions = @[@"email"];
    NSDictionary * options = @{ACFacebookAppIdKey : kFacebookAppId, ACFacebookPermissionsKey : permissions, ACFacebookAudienceKey : ACFacebookAudienceEveryone};
    FacebookAccountManager * fbMgr = [[FacebookAccountManager alloc] init];
    [self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
        if (granted) {
            NSArray * accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
            fbMgr.account = [accounts lastObject];
            fbMgr.isBasicPermissionsGranted = YES;
            [self.accountManagers addObject:fbMgr];
            NSLog(@"granted!");
        }
        else {
            fbMgr.account = nil;
            fbMgr.isBasicPermissionsGranted = NO;
            switch ([error code]) {
                case 1:
                    [self showErrorAlertWithMessage:@"Unknown error occured, try again later!"];
                    break;
                case 3:
                    [self showErrorAlertWithMessage:@"Authentication failed, try again later!"];
                     break;
                case 6:
                    [self showErrorAlertWithMessage:@"Facebook account does not exists. Please create it in Settings and come back!"];
                     break;
                 case 7:
                    [self showErrorAlertWithMessage:@"Permission request failed. You won't be able to share information to Facebook"];
                     break;
                default:
                    break;
            }
            NSLog(@"error is: %@", error);
        }
    }];
}

If an account does not exists you should prompt user to create it in settings and then try to to obtain basic permissions again.如果帐户不存在,您应该提示用户在设置中创建它,然后再次尝试获取基本权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM