2016-10-07 7 views
1

맞춤 버튼을 클릭 할 때 사진을 공유하고 싶지만 링크 및 메시지를 공유 할 수 있지만 사진 공유는 할 수 없습니다.Objective C에서 Facebook에 사진을 공유하는 방법은 무엇입니까?

저는 최신 Facebook SDK 프레임 워크 (4.15.1)를 사용하고 있습니다.

이것은 내 버튼 클릭 내 코드입니다.

- (IBAction)btnsharecustom:(id)sender { 

//Get aws s3 Image. 
NSString* strS3ImageURL = @"https://TEST_IMAGE_URL/1473497380077.jpg"; 

//Convert to URL. 
NSURL* url1 = [NSURL URLWithString:strS3ImageURL]; 

//Convert it into data. 
NSData* data = [NSData dataWithContentsOfURL:url1]; 

//Convert it to image. 
UIImage* image = [UIImage imageWithData:data];  

FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc]init]; 

photo.image = image; 
photo.caption = @"Test Caption"; 
photo.userGenerated = YES; 

FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init]; 
content.photos = @[photo]; 
} 

이 코드를 앱 대리인에 포함 시켰습니다. 나는 내 사용자 지정 버튼 페이스 북 앱이나 사파리 브라우저를 실행하지 않습니다에 클릭, 그래서 누군가가 여기에 놓치고 무엇을 말해 줄 수있을 때

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Override point for customization after application launch. 

[[FBSDKApplicationDelegate sharedInstance] application:application 
         didFinishLaunchingWithOptions:launchOptions];  

return YES; 
} 

//FaceBook URLs. 
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { 

BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application 
                   openURL:url 
                sourceApplication:sourceApplication 
                  annotation:annotation 
       ]; 
// Add any custom logic here. 
return handled; 
} 

또한의 Info.plist

<dict> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>fb15771408xxx409xx</string> 
     </array> 
    </dict> 

<key>FacebookAppID</key> 
<string>15771408xxx409xx</string> 
<key>FacebookDisplayName</key> 
<string>Cedar iOS</string> 
<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>fbapi</string> 
    <string>fb-messenger-api</string> 
    <string>fbauth2</string> 
    <string>fbshareextension</string> 
    <string>fbapi20130214</string> 
    <string>fbapi20130410</string> 
    <string>fbapi20130702</string> 
    <string>fbapi20131010</string> 
    <string>fbapi20131219</string> 
    <string>fbapi20140410</string> 
    <string>fbapi20140116</string> 
    <string>fbapi20150313</string> 
    <string>fbapi20150629</string> 
    <string>fbapi20160328</string> 
    <string>fbauth</string> 
    <string>fb-messenger-api20140430</string> 
</array> 

이 코드를 포함?

+0

FBSDKSharePhotoContent은 그래서 당신은 응용 프로그램이 있는지 확인하고. –

+0

@SanketBhavsar 그래, 내가 설치 한 FB 앱에 로그인하고 또한 로그인 ... FB 응용 프로그램에서 작동합니다. – Ganesh

+0

응용 프로그램을 통해 페이스 북에 로그인했는지 확인하고 페이스 북에서 컨텐츠를 공유 할 수있는 권한을 부여하십시오. 코드를 원한다면 코드를 게시 할 것입니다. –

답변

2

콘텐츠를 모델링 한 후에 대화 상자를 직접 제시해야합니다.

- (IBAction)btnsharecustom:(id)sender 방법의 끝이

[FBSDKShareDialog showFromViewController:self 
          withContent:content 
          delegate:nil]; 

를 추가합니다.


참고 : 동기 요청을 사용하기 때문에 이미지를 다운로드하는 데 시간이 오래 걸릴 수 있습니다.

//Convert it into data. 
NSData* data = [NSData dataWithContentsOfURL:url1]; 

은 (업로드 할 이미지가 예측 가능한 경우) 사용자 탭 버튼 전에 비동기 적으로 이미지를 다운로드 할 수 있습니다 여기에 https://developer.apple.com/reference/foundation/nsdata/1547245-datawithcontentsofurl

Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

문서이 문제에 대한 해결책을 읽으십시오. 보통 viewDidLoad 방법으로이 작업을 수행 할 수 있습니다. 이 https://github.com/rs/SDWebImage 라이브러리를 사용해 비동기 요청 이미지를 쉽게 만들 수 있습니다.

+0

답변 해 주셔서 감사합니다. 2 가지 의문점이 있습니다. 예를 들어 코드를 추가 할 때 대화 상자가 나타납니다. 내 2 의심은 1입니다. 캡션 (내 코드에서 볼 수 있음)을 언급했지만 시작한 대화 상자에는 여전히 존재하지 않습니다. 두 번째는 Facebook 앱이 설치되어 있지 않은 경우 사파리와 같은 브라우저를 통해 공유 할 코드가 무엇인가요? – Ganesh

+0

1. 캡션은 사진마다 설명이 있으므로 사진이 두 개 이상있을 수 있으므로 시작 대화 상자에 표시되지 않습니다. 그러나, 나는 여전히 대화 상자에 텍스트를 추가하는 방법을 모른다. 2. 자동으로 감지합니다. 여기에서 설명서를 볼 수 있습니다. https://developers.facebook.com/docs/sharing/ios – ramacode

+0

예. 설명서가별로 도움이되지 않았습니다. 그들은 상세하게 언급하지 않았고 허가 & 캡션을 설정하지 못했습니다. – Ganesh

0

이 코드를 시도해보십시오.

- (IBAction)btnsharecustom:(id)sender { 

    //Get aws s3 Image. 
    NSString* strS3ImageURL = @"https://placeholdit.imgix.net/~text?txtsize=15&txt=image1&w=120&h=120"; 

    //Convert to URL. 
    NSURL* url1 = [NSURL URLWithString:strS3ImageURL]; 

    //Convert it into data. 
    NSData* data = [NSData dataWithContentsOfURL:url1]; 

    //Convert it to image. 
    UIImage* image = [UIImage imageWithData:data]; 

    FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc]init]; 

    photo.image = image; 
    photo.caption = @"Test Caption"; 
    photo.userGenerated = YES; 

    FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init]; 
    content.photos = @[photo]; 
    [FBSDKShareAPI shareWithContent:content delegate:self]; 
; 
} 
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{ 

} 
+0

이것은 대화를 표시하지 않고 직접 게시물을 공유하므로 직접 게시 권한을 연장해야하는 사용자가 필요합니다. https : //developers.facebook.co.kr/docs/facebook-login/permissions/review # reference-extended – ramacode

+0

위 코드에서 예외가 발생하고 확장 권한이 필요합니까 아니면 다른 것이 있습니까? FBShareObj1 FBSDKLog : 경고 : 액세스 토큰은 publish_actions 권한 FBShareObj1없는 - [의 ViewController 공유기 : didFailWithError :] : 인식 할 수없는 선택기 응용 프로그램을 종료 0x175ce3d0 FBShareObj1 *** 인해 'NSInvalidArgumentException'캐치되지 않는 예외에 인스턴스로 전송, 이유 : '- [ ViewController sharer : didFailWithError :] : 인스턴스 0x175ce3d0에 전송 된 인식 할 수없는 선택자 *** 우선 스택 호출 : libC++ abi.dylib : NSException 유형의 캐치되지 않은 예외로 종료 – Ganesh

+0

공유를위한 다음 대리인 메소드 추가 : - (void) 공유자 : (id ) 공유자 didFailWithError : (NSError *) 오류 { } – KKRocks

관련 문제