Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post location with image to facebook in IOS?

I am trying to share location along with image to facebook. I have successfully shared image but unable to share location. Below is my code of sharing image.

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);
    }
}];

Now for sharing location what parameter should I add in above code. I am attaching an image also to understand better that how the shared location will look like.Below image shows that how the image with text will indicate a location into map. Please suggest me a solution for that. enter image description here

like image 656
rahul Avatar asked Jun 19 '14 06:06

rahul


2 Answers

Along with "message" and you also need "place" ID to post as param.

Request for a "publish_actions" so that you can post a place/location.

Below is the code i've used:

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);
 }];

You can also check this with the "administrator/tester" accounts given in Developer page -> your app -> Roles. Use Graph explorer for better practice: https://developers.facebook.com/tools/explorer/108895529478793?method=POST&path=me%2Ffeed%3F&version=v2.5&message=Hello%20world&place=110503255682430

Below code may help you in getting place id near your location:

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);
}];

Hope it helps you..

like image 190
Satish A Avatar answered Nov 08 '22 15:11

Satish A


Swift 3.2 version of @Satish A answer.

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")
            }
        })
    }
}

For Facebook sharing with attaching placeId

 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
     }
 }
like image 36
Jay Patel Avatar answered Nov 08 '22 14:11

Jay Patel