2014-06-19 48 views
6

Tôi đang cố gắng chia sẻ vị trí cùng với hình ảnh lên facebook. Tôi đã chia sẻ thành công hình ảnh nhưng không thể chia sẻ vị trí. Dưới đây là mã chia sẻ hình ảnh của tôi.Cách đăng vị trí với hình ảnh lên facebook trong IOS?

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

Bây giờ để chia sẻ vị trí tôi nên thêm thông số nào vào mã ở trên. Tôi đính kèm một hình ảnh cũng để hiểu rõ hơn về cách vị trí được chia sẻ trông giống như thế. Hình ảnh gần đây cho thấy cách hình ảnh có văn bản sẽ cho biết vị trí trong bản đồ. Xin đề nghị tôi một giải pháp cho điều đó. enter image description here

Trả lời

2

Cùng với "thông báo" và bạn cũng cần "nơi" ID để đăng bài như param.

Yêu cầu "publish_actions" để bạn có thể đăng địa điểm/vị trí.

Dưới đây là đoạn code tôi đã sử dụng:

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

Bạn cũng có thể kiểm tra điều này với các tài khoản "quản trị/kiểm tra" được đưa ra trong trang Developer -> ứng dụng của bạn -> Roles. Sử dụng Graph nhà thám hiểm thực hành tốt hơn: https://developers.facebook.com/tools/explorer/108895529478793?method=POST&path=me%2Ffeed%3F&version=v2.5&message=Hello%20world&place=110503255682430

Dưới mã có thể giúp bạn trong việc nơi id gần vị trí của bạn:

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

HOẶC

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

Hy vọng nó sẽ giúp bạn ..

0

Phiên bản Swift 3.2 của @Satish Câu trả lời.

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

Đối với Facebook chia sẻ với gắn 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 
    } 
} 
Các vấn đề liên quan