简体   繁体   中英

Confused using UIActivityViewController

Can you help me to understand when should I use 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. 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). 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? There is a method in ACAccount store class – requestAccessToAccountsWithType:options:completion: to access an account. But If an account does not exists it returns an error. Many thanks in advance.

You first need to subclass UIActivity.

Then you need to override certain methods, including activityImage for setting the icon and performActivity for performing the action .

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.

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:.

Have a look at the documentation for UIActivity for exactly what you need to override when subclassing and for icon requirements.

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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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