如何在IOS中将图像的位置发布到Facebook?

时间:2014-06-19 06:06:30

标签: ios facebook location sharing

我正在尝试与Facebook共享位置和图像。我已成功共享图片但无法共享位置。下面是我分享图片的代码。

UIImage *facebookImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",imagesURL,str]]]];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:@"New Happening created on the HappenShare mobile app." forKey:@"message"];
[params setObject:facebookImage forKey:@"picture"];
[FBRequestConnection startWithGraphPath:@"me/photos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection,id result,NSError *error)
{
    if (error)
    {
        NSLog(@"error : %@",error);
    }
    else
    {
        NSLog(@"Result : %@",result);
    }
}];

现在分享位置我应该在上面的代码中添加什么参数。我附加图像也是为了更好地理解共享位置的外观。下图显示带有文本的图像如何指示地图中的位置。请为我建议一个解决方案。 enter image description here

3 个答案:

答案 0 :(得分:2)

与"消息"你还需要"地点"要作为参数发布的ID。

请求" publish_actions"这样你就可以张贴一个地方/位置。

以下是我使用的代码:

NSMutableDictionary *params = [NSMutableDictionary dictionary];
                    [params setObject:@"Hello World" forKey:@"message"];
                    [params setObject:@"110503255682430"/*sample place id*/ forKey:@"place"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/feed" parameters:params HTTPMethod:@"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                        NSLog(@"result %@",result);
                        NSLog(@"error %@",error);
 }];

您也可以使用"管理员/测试人员"开发者页面中提供的帐户 - >你的应用 - >角色。 使用Graph explorer进行更好的练习: https://developers.facebook.com/tools/explorer/108895529478793?method=POST&path=me%2Ffeed%3F&version=v2.5&message=Hello%20world&place=110503255682430

下面的代码可以帮助您获取您所在位置附近的地点ID:

NSMutableDictionary *params2 = [NSMutableDictionary dictionaryWithCapacity:4L];
  [params2 setObject:[NSString stringWithFormat:@"%@,%@",YourLocation latitude,YourLocation longitude] forKey:@"center"]; //Hard code coordinates for test
  [params2 setObject:@"place" forKey:@"type"];
  [params2 setObject:@"100"/*meters*/ forKey:@"distance"];

 [[[FBSDKGraphRequest alloc] initWithGraphPath:@"/search" parameters:params2 HTTPMethod:@"GET"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                    NSLog(@"RESPONSE!!! /search");
}];

OR

[[[FBSDKGraphRequest alloc] initWithGraphPath:@"/search?type=place&center=YourLocationLat,YourLocationLong&distance=500" parameters:nil HTTPMethod:@"GET"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                    NSLog(@"result %@",result);
}];

希望它可以帮助你..

答案 1 :(得分:1)

@Satish的Swift 3.2版本答案。

func getPlaceId() {

    let locManager = CLLocationManager()
    locManager.requestWhenInUseAuthorization()

    var currentLocation = CLLocation()

    if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == .authorizedAlways) {

        currentLocation = locManager.location!

        let param = ["center":"\(currentLocation.coordinate.latitude),\(currentLocation.coordinate.longitude)","type":"place","distance":"100"]

        FBSDKGraphRequest(graphPath: "/search", parameters: param).start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil) {
                guard let data = result as? NSDictionary else {
                    return
                }
                guard let arrPlaceIDs = data.value(forKey: "data") as? [NSDictionary] else {
                    return
                }
                guard let firstPlace = arrPlaceIDs.first else {
                    return
                }
                //First facebook place id.
                print(firstPlace.value(forKey: "id") as! String)
            } else {
                print(error?.localizedDescription ?? "error")
            }
        })
    }
}

使用附加placeId与Facebook共享

 let photo = Photo(image: img, userGenerated: true)
 var content = PhotoShareContent()
 content.photos = [photo]
 content.placeId = id //Facebook placeId
 let sharer = GraphSharer(content: content)
 sharer.failsOnInvalidData = true

 do {
     try sharer.share()
 } catch {
     print("errorrrr")
 }

 sharer.completion = { FBresult in

     switch FBresult {
     case .failed(let error):
         print(error)
         break
     case .success(_):
         //code
         break
     default:
         break
     }
 }

答案 2 :(得分:0)

您需要在place上的POST请求中设置/me/photos(实际上是页面ID)字段。

请参阅https://developers.facebook.com/docs/graph-api/reference/v2.0/user/photos/#publish

在iOS上,您可以使用FB iOS SDK的PlacePicker UI组件,如下所述:https://developers.facebook.com/docs/ios/ui-controls#placepicker