iOS:发送 Facebook 请求
Posted
技术标签:
【中文标题】iOS:发送 Facebook 请求【英文标题】:iOS :Sending Facebook Request 【发布时间】:2013-06-27 05:59:48 【问题描述】:我已发送 Facebook 请求。它工作正常,但我只在 iPhone facebook 应用程序中收到请求(通知),而不是在 Facebook 网络应用程序中。我希望 Facebook 本机应用程序和 Web 应用程序都能接收通知。我该怎么做?
#pragma Sending Facebook app request
- (NSDictionary*)parseURLParams:(NSString *)query
NSArray *pairs = [query componentsSeparatedByString:@"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs)
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *val =
[[kv objectAtIndex:1]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[params setObject:val forKey:[kv objectAtIndex:0]];
return params;
- (void)sendRequest
NSError *error;
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:@
@"social_karma": @"5",
@"badge_of_awesomeness": @"1"
options:0
error:&error];
if (!jsonData)
NSLog(@"JSON error: %@", error);
return;
NSString *giftStr = [[NSString alloc]
initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
giftStr, @"data",
nil];
// Display the requests dialog
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:@"Learn how to make your ios apps social."
title:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
if (error)
// Error launching the dialog or sending the request.
NSLog(@"Error sending request.");
else
if (result == FBWebDialogResultDialogNotCompleted)
// User clicked the "x" icon
NSLog(@"User canceled request.");
else
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:@"request"])
// User clicked the Cancel button
NSLog(@"User canceled request.");
else
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:@"request"];
NSLog(@"Request ID: %@", requestID);
];
- (void)sendRequestClicked
// Filter and only show friends using iOS
[self requestFriendsUsingDevice:@"iOS"];
- (void)sendRequest:(NSArray *) targeted
NSError *error;
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:@
@"social_karma": @"5",
@"badge_of_awesomeness": @"1"
options:0
error:&error];
if (!jsonData)
NSLog(@"JSON error: %@", error);
return;
NSString *giftStr = [[NSString alloc]
initWithData:jsonData
encoding:NSUTF8StringEncoding];
NSMutableDictionary* params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:giftStr, @"data",
nil];
// Filter and only show targeted friends
if (targeted != nil && [targeted count] > 0)
NSString *selectIDsStr = [targeted componentsJoinedByString:@","];
[params setObject:selectIDsStr forKey:@"suggestions"];
// Display the requests dialog
[FBWebDialogs
presentRequestsDialogModallyWithSession:nil
message:@"Learn how to make your iOS apps social."
title:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
if (error)
// Error launching the dialog or sending request.
NSLog(@"Error sending request.");
else
if (result == FBWebDialogResultDialogNotCompleted)
// User clicked the "x" icon
NSLog(@"User canceled request.");
else
// Handle the send request callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:@"request"])
// User clicked the Cancel button
NSLog(@"User canceled request.");
else
// User clicked the Send button
NSString *requestID = [urlParams valueForKey:@"request"];
NSLog(@"Request ID: %@", requestID);
];
- (void) requestFriendsUsingDevice:(NSString *)device
NSMutableArray *deviceFilteredFriends = [[NSMutableArray alloc] init];
[FBRequestConnection startWithGraphpath:@"me/friends"
parameters:[NSDictionary
dictionaryWithObjectsAndKeys:
@"id,devices", @"fields",
nil]
HTTPMethod:nil
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
if (!error)
// Get the result
NSArray *resultData = [result objectForKey:@"data"];
// Check we have data
if ([resultData count] > 0)
// Loop through the friends returned
for (NSDictionary *friendObject in resultData)
// Check if devices info available
if ([friendObject objectForKey:@"devices"])
NSArray *deviceData = [friendObject
objectForKey:@"devices"];
// Loop through list of devices
for (NSDictionary *deviceObject in deviceData)
// Check if there is a device match
if ([device isEqualToString:
[deviceObject objectForKey:@"os"]])
// If there is a match, add it to the list
[deviceFilteredFriends addObject:
[friendObject objectForKey:@"id"]];
break;
// Send request
[self sendRequest:deviceFilteredFriends];
];
【问题讨论】:
欢迎来到***。从现在开始在 SO 中发帖时请考虑以下几点 1) 引用自己时使用“I”,而不是“i”。 2) 看来您的母语不是英语,但请确保正确拆分句子并使用“。”在哪里到期。 3) This is how you format code in SO 4) Please don't use 'Hi', 'Thanks' etc tag lines in your post @Krishnabhadra 谢谢 【参考方案1】:只有在您的应用程序实现了 Facebook Canvas 应用程序时,您才能在 Facebook 网络应用程序上收到通知。
invitable_friends API 仅适用于具有 使用 Graph API 2.0 版的 Facebook Canvas 应用实施。
Check here the full documentation
Canvas 是一个框架,您可以在其中直接放置您的应用或游戏 台式机和笔记本电脑上的 Facebook.com。
Details about Canvas
注意:在文档中,您会找到“您的游戏”,它们的意思是“您的游戏或您的应用”。
【讨论】:
以上是关于iOS:发送 Facebook 请求的主要内容,如果未能解决你的问题,请参考以下文章
Facebook SDK 4.0 AppInvite 没有收到任何通知
Facebook iOS SDK - 向 facebook 用户发送应用请求
使用 iOS 中的社交框架向 Facebook 朋友发送消息或应用请求