在 iOS 中访问分页 - 上一个和下一个:在 JSON 中从 facebook 图检索 - api
Access paging in iOS - previous and next : retrieved in JSON from facebook graph-api
在 iOS 代码中访问分页来自 JSON 上一个和下一个 link
在从图 api 检索到的 JSON 对象中,由于信息负载,信息被分页,当我尝试访问上一个和下一个 link 时,它给出了 OAuth 错误。
任何人都可以帮助如何通过 iOS 应用程序中的 objective C 访问 link。
我想知道两件事 - 如何使用 --> limit=N 检索 'N' 项
以及如何打开 FBSDKgraphrequest 并使用分页 link link(包含分页信息)
paging = {
next = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&until=1428241306&__paging_token=enc_AdC9127ZCBVnZACHUlMZBTC39ZC8bSP4ZA8uwQZBdy8xhsZAyKAcxxNdqn48Er3CrVM4DkJPATHhOYBVRm8FuCvYZBU8KSpZA";
previous = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&since=1432299972&__paging_token=enc_AdDp9ZCK2ZBP40AgTi4TCzaB0QFT1Cy7s1R7HLLDDaT7nbnLYDZB4LZBjiONOqG5QR9Q22KY1oU1LzNOwS5uNZBG7uLF4&__previous=1";
};
页面信息有当前的access token,而且根据不同的limits限制分页值会一直变化,所以;最好使用 url 并获取结果。
因为 next 的结果也会有指向 previous 和 next 的指针,解析得到的结果的最好方法是通过递归函数调用它,并将分页 next 值传递给调用函数内外,以遍历所有页面。
不要忘记在您想停止遍历其他节点的地方设置限制。
例如:
NSURL *url = [NSURL URLWithString:PagingNext];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *errorConnection)
{
if(errorConnection == nil)
{
NSError *error;
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (result == nil)
{
NSLog(@"Error parsing JSON:\n%@",error.userInfo);
return;
}
//if no error then extract paging next and do what you want ->
/*
result = {
data[20],
paging={
previous= "URL_of_previous"
next = "URL_of_next"
}
}
*/
if ([result isKindOfClass:[NSDictionary class]])
{
NSArray * data = [result objectForKey:@"data"];
NSDictionary *paging =[result objectForKey:@"paging"];
NSString * resultPagingNext = [paging objectForKey:@"next"];
[self call_method_recursively_with_parameter_of_next_page:data pagingNext:resultPagingNext];
}
}
else
{
NSLog(@"Connection Error:\n%@", errorConnection.userInfo);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Network Issue"
message:@"Check if you are connected to Internet" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
NSLog(@"User has pressed OK."); }];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}];
您仍然可以使用来自 Facebook 的 SDK,您只需将 "after" 字段作为参数提供。然后调用递归函数。
我刚刚在 Swift:
中实施的解决方案
func getFBTaggableFriends(nextCursor : String?, failureHandler: (error: NSError) -> Void) {
var qry : String = "me/taggable_friends"
var parameters = Dictionary<String, String>() as? Dictionary
if nextCursor == nil {
parameters = nil
} else {
parameters!["after"] = nextCursor
}
// Facebook: get taggable friends with pictures
var request = FBSDKGraphRequest(graphPath: qry, parameters: parameters)
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
//println("fetched user: \(result)")
var resultdict = result as! NSDictionary
var data : NSArray = resultdict.objectForKey("data") as! NSArray
for i in 0..<data.count {
let valueDict : NSDictionary = data[i] as! NSDictionary
let id = valueDict.objectForKey("id") as! String
let name = valueDict.objectForKey("name") as! String
let pictureDict = valueDict.objectForKey("picture") as! NSDictionary
let pictureData = pictureDict.objectForKey("data") as! NSDictionary
let pictureURL = pictureData.objectForKey("url") as! String
println("Name: \(name)")
//println("ID: \(id)")
//println("URL: \(pictureURL)")
}
if let after = ((resultdict.objectForKey("paging") as? NSDictionary)?.objectForKey("cursors") as? NSDictionary)?.objectForKey("after") as? String {
self.getFBTaggableFriends(after, failureHandler: {(error) in
println("error")})
} else {
println("Can't read next!!!")
}
}
}
}
然后您将调用此函数:
getFBTaggableFriends(nil, failureHandler: {(error)
in println(error)});
在 iOS 代码中访问分页来自 JSON 上一个和下一个 link
在从图 api 检索到的 JSON 对象中,由于信息负载,信息被分页,当我尝试访问上一个和下一个 link 时,它给出了 OAuth 错误。 任何人都可以帮助如何通过 iOS 应用程序中的 objective C 访问 link。
我想知道两件事 - 如何使用 --> limit=N 检索 'N' 项 以及如何打开 FBSDKgraphrequest 并使用分页 link link(包含分页信息)
paging = {
next = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&until=1428241306&__paging_token=enc_AdC9127ZCBVnZACHUlMZBTC39ZC8bSP4ZA8uwQZBdy8xhsZAyKAcxxNdqn48Er3CrVM4DkJPATHhOYBVRm8FuCvYZBU8KSpZA";
previous = "https://graph.facebook.com/v2.3/897401690298128/inbox?access_token=CAAEsxMhiSe4BACWnj27BT6ZBvj2BAxNZCtCNQyCKQORXyylXXkQy3DLSF75UGSz2FydAkQx6Pj49MOS0Q3SGiU1vkQ1iUEs2fQvvlwW3Wc04DEnXZB4CZCza7tOJfyncIPrkFrudQCeRhWUUREqMpCI8Dnm6Ozc6xmwOlT1uN2ZCgQ91llcVC1kV04fiZCqO6H6edFe2YZAUZBy86pw1p4SWCUvgMshzkvZBGgpG8UWG50ZCShdeQPUc86fsuQGOcAno0ZD&limit=25&since=1432299972&__paging_token=enc_AdDp9ZCK2ZBP40AgTi4TCzaB0QFT1Cy7s1R7HLLDDaT7nbnLYDZB4LZBjiONOqG5QR9Q22KY1oU1LzNOwS5uNZBG7uLF4&__previous=1";
};
页面信息有当前的access token,而且根据不同的limits限制分页值会一直变化,所以;最好使用 url 并获取结果。 因为 next 的结果也会有指向 previous 和 next 的指针,解析得到的结果的最好方法是通过递归函数调用它,并将分页 next 值传递给调用函数内外,以遍历所有页面。 不要忘记在您想停止遍历其他节点的地方设置限制。 例如:
NSURL *url = [NSURL URLWithString:PagingNext];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *errorConnection)
{
if(errorConnection == nil)
{
NSError *error;
id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
if (result == nil)
{
NSLog(@"Error parsing JSON:\n%@",error.userInfo);
return;
}
//if no error then extract paging next and do what you want ->
/*
result = {
data[20],
paging={
previous= "URL_of_previous"
next = "URL_of_next"
}
}
*/
if ([result isKindOfClass:[NSDictionary class]])
{
NSArray * data = [result objectForKey:@"data"];
NSDictionary *paging =[result objectForKey:@"paging"];
NSString * resultPagingNext = [paging objectForKey:@"next"];
[self call_method_recursively_with_parameter_of_next_page:data pagingNext:resultPagingNext];
}
}
else
{
NSLog(@"Connection Error:\n%@", errorConnection.userInfo);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Network Issue"
message:@"Check if you are connected to Internet" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
{
NSLog(@"User has pressed OK."); }];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}];
您仍然可以使用来自 Facebook 的 SDK,您只需将 "after" 字段作为参数提供。然后调用递归函数。 我刚刚在 Swift:
中实施的解决方案func getFBTaggableFriends(nextCursor : String?, failureHandler: (error: NSError) -> Void) {
var qry : String = "me/taggable_friends"
var parameters = Dictionary<String, String>() as? Dictionary
if nextCursor == nil {
parameters = nil
} else {
parameters!["after"] = nextCursor
}
// Facebook: get taggable friends with pictures
var request = FBSDKGraphRequest(graphPath: qry, parameters: parameters)
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
//println("fetched user: \(result)")
var resultdict = result as! NSDictionary
var data : NSArray = resultdict.objectForKey("data") as! NSArray
for i in 0..<data.count {
let valueDict : NSDictionary = data[i] as! NSDictionary
let id = valueDict.objectForKey("id") as! String
let name = valueDict.objectForKey("name") as! String
let pictureDict = valueDict.objectForKey("picture") as! NSDictionary
let pictureData = pictureDict.objectForKey("data") as! NSDictionary
let pictureURL = pictureData.objectForKey("url") as! String
println("Name: \(name)")
//println("ID: \(id)")
//println("URL: \(pictureURL)")
}
if let after = ((resultdict.objectForKey("paging") as? NSDictionary)?.objectForKey("cursors") as? NSDictionary)?.objectForKey("after") as? String {
self.getFBTaggableFriends(after, failureHandler: {(error) in
println("error")})
} else {
println("Can't read next!!!")
}
}
}
}
然后您将调用此函数:
getFBTaggableFriends(nil, failureHandler: {(error)
in println(error)});