解析iOS PFTwitterUtils linkUser:没有做任何事情

时间:2014-09-18 09:43:28

标签: ios twitter parse-platform twitter-oauth

所以我使用Parse将用户与他们的Twitter帐户相关联。在app委托中,我有以下内容:

[PFTwitterUtils initializeWithConsumerKey:CONSUMER_KEY consumerSecret:CONSUMER_SECRET];

然后,用户点击将用户链接到Facebook的按钮调用以下内容:

-(IBAction)twitterConnectPressed{
    NSLog(@"twitter");
    [PFTwitterUtils linkUser:[PFUser currentUser] block:^(BOOL succeeded, NSError* error){
        NSLog(@"haha");
        if(succeeded){
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Done!" message:@"Connected with Twitter!" delegate:self cancelButtonTitle:@"okay" otherButtonTitles: nil];
            [alert show];
            self.fbButton.backgroundColor = [TGAPublic grey];

        }else{
            UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Oops" message:error.userInfo[@"error"] delegate:self cancelButtonTitle:@"okay" otherButtonTitles: nil];
            [alert show];
        }
    }];
}

然而,即使linkUser:block:被调用,它根本不做任何事情。它没有像[PFFacebookUtils linkUser:]那样创建一个弹出登录到Twitter的弹出窗口,因此不会最终调用该块

2 个答案:

答案 0 :(得分:0)

PFTwitterUtils似乎无法处理iOS上的所有情况。特别是,如果您没有帐户设置(设置 - > Twitter),它不会启动Web视图并尝试使用Web oauth。相反,如果您配置了多个Twitter帐户(再次在“设置”中),则它似乎不会启动操作表,以允许您选择要链接的帐户。

有一个关于如何做这些事情的很好的教程,它在这里公开了对PFFacebookUtils的扩展:http://natashatherobot.com/ios-twitter-login-parse/

它不会进行链接,只是登录,但应该是添加链接的良好基础。

答案 1 :(得分:0)

我对PFFacebookUtils和PFTwitterUtils(v.7.7.4)的链接/取消链接方法都有类似的问题。

我设法让它工作的唯一方法是替换它们,遗憾的是,它们弄乱了authData的内部Parse实现:

#import "TwitterAuthProvider.h"
#import "PFTwitterUtils.h"
#import "PFUser.h"

static NSString * const kTwitterKey = @"XXX";
static NSString * const kTwitterSecret = @"XXX";

@implementation TwitterAuthProvider    

- (instancetype)init {
    if ((self = [super init])) {
        [PFTwitterUtils initializeWithConsumerKey:kTwitterKey consumerSecret:kTwitterSecret];
    }
    return self;
}

- (void)setAuthData:(id)twAuthData forUser:(PFUser *)user {    
    static NSString * const kParseAuthDataKey = @"authData";
    static NSString * const kParseLinkedServiceNamesKey = @"linkedServiceNames";    
    static NSString * const kParseAuthProviderName = @"twitter";

    NSMutableDictionary *authData = [[user valueForKey:kParseAuthDataKey] mutableCopy] ?: [NSMutableDictionary dictionary];
    authData[kParseAuthProviderName] = twAuthData ?: [NSNull null];
    [user setObject:authData forKey:kParseAuthDataKey];
    [user setValue:authData forKey:kParseAuthDataKey];

    NSMutableSet *linkedServices = [[user valueForKey:kParseLinkedServiceNamesKey] mutableCopy] ?: [NSMutableSet set];
    if (twAuthData) {
        [linkedServices addObject:kParseAuthProviderName];
    } else {
        [linkedServices removeObject:kParseAuthProviderName];
    }
    [user setValue:linkedServices forKey:kParseLinkedServiceNamesKey];
}

- (void)linkWithCompletion:(PFBooleanResultBlock)completion {
    NSParameterAssert(completion != nil);

    PFUser *user = [PFUser currentUser];

    __weak typeof(self) weakSelf = self;
    PF_Twitter *twitter = [PFTwitterUtils twitter];
    [twitter authorizeWithSuccess:^(void) {
        [weakSelf setAuthData:[self twitterAuthData] forUser:user];
        [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (!succeeded) {
                //revert
                [weakSelf setAuthData:nil forUser:user];
            }
            completion(succeeded, error);
        }];
    } failure:^(NSError *error) {
        completion(NO, error);
    } cancel:^(void) {
        completion(NO, nil);
    }];
}

- (void)unlinkWithCompletion:(PFBooleanResultBlock)completion {
    NSParameterAssert(completion != nil);
    PFUser *user = [PFUser currentUser];

    [self setAuthData:nil forUser:user];
    [user saveInBackgroundWithBlock:completion];
}

- (NSDictionary *)twitterAuthData {
    PF_Twitter *twitter = [PFTwitterUtils twitter];
    return @{
        @"auth_token" : twitter.authToken,
        @"auth_token_secret": twitter.authTokenSecret,
        @"consumer_key": kTwitterKey,
        @"consumer_secret": kTwitterSecret,
        @"id": twitter.userId,
        @"screen_name": twitter.screenName,
    };
}

@end