私はついに問題を見つけました。
ソーシャルネットワークにバグがあります。Appleは常に"?access_token=[ACCESS_TOKEN]"
URL文字列の最後に文字列を追加します。
それによると、URL文字列の前にパラメータを入力すると、「?」が2つあるため、URLは無効になります。文字列で。
これを回避するには、NSURLConnectionクラスを使用して、次のように接続を管理します。
NSString *appendChar = [[url absoluteString] rangeOfString:@"?"].location == NSNotFound ? @"?" : @"&";
NSString *finalURL = [[url absoluteString] stringByAppendingFormat:@"%@access_token=%@", appendChar, self.facebookAccount.credential.oauthToken];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:finalURL]];
NSURLResponse *response;
NSError *error;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error)
[self.delegate facebookConnection:self didFailWithError:error];
else
{
NSError *jsonError;
NSDictionary *resultDictionnary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (jsonError)
[self.delegate facebookConnection:self didFailWithError:jsonError];
else if ([resultDictionnary valueForKey:@"error"])
{
NSDictionary *errorDictionary = [resultDictionnary valueForKey:@"error"];
NSError *facebookError = [NSError errorWithDomain:[errorDictionary valueForKey:@"message"] code:[[errorDictionary valueForKey:@"code"] integerValue] userInfo:nil];
[self.delegate facebookConnection:self didFailWithError:facebookError];
}
else
[self.delegate facebookConnection:self didFinishWithDictionary:resultDictionnary httpUrlResponse:response];
}
まず、文字列内のparam文字の存在をテストし、正しい文字を追加します。エラーを処理する方法をボーナスとして提供します。
私はまだソーシャルフレームワークを使用して資格情報を取得し、ユーザーを接続しています。
NSDictionary *accessParams = @{ACFacebookAppIdKey:kFacebookAppID, ACFacebookPermissionsKey:@[@"email", @"user_photos", @"user_activities", @"friends_photos"]};
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType options:accessParams completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *facebookAccounts = [accountStore accountsWithAccountType:accountType];
if ([facebookAccounts count] > 0)
{
self.facebookAccount = [facebookAccounts objectAtIndex:0];
self.accessToken = self.facebookAccount.credential.oauthToken;
[self.delegate facebookConnectionAccountHasBeenSettedUp:self];
}
}
else
[self.delegate facebookConnection:self didFailWithError:error];
}];
このコードでは、複数のFacebookアカウントを処理しませんが、そのスニペットを簡単に変換して、独自の方法で処理できます。また、インターフェイスのブロックを回避するためにGCDを使用しているため、接続は同期的に送信されますが、NSURLConnectionクラスに組み込まれた非同期メソッドを実装できます。
これが誰かを助けることを願っています!