官方FB分享Sample,很不錯直接執行就可運作,但是在iOS 5的平台下,Share Photo功能
[FBDialogscanPresentShareDialogWithPhotos] 會return No,
官方有兩個建議,其中一個是使用 [FBRequest requestForUploadPhoto:img];
但怎麼試都會出現Http 403或是400錯誤,最後東拼西湊下..,終於成功了!!
- (IBAction)SharePhotoWithShareDialog:(id)sender {
// If the Facebook app is installed and we can present the share dialog
if([FBDialogscanPresentShareDialogWithPhotos]) {
NSLog(@"canPresent");
// 這裡與官方Sample一致
} else
{
// We will post on behalf of the user, these are the permissions we need:
NSArray *permissionsNeeded = @[@"publish_actions"];
// Request the permissions the user currently has
[FBRequestConnectionstartWithGraphPath:@"/me/permissions"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error){
NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];
// Check if all the permissions we need are present in the user's current permissions
// If they are not present add them to the permissions to be requested
for (NSString *permission in permissionsNeeded){
if (![currentPermissions objectForKey:permission]){
[requestPermissions addObject:permission];
}
}
// If we have permissions to request
if ([requestPermissions count] > 0){
// Ask for the missing permissions
[FBSession.activeSession requestNewPublishPermissions:requestPermissions
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// Permission granted, we can request the user information
[self makeRequestToSharePhoto];
} else {
// An error occurred, handle the error
// See our Handling Errors guide: https://developers.facebook.com/docs/ios/errors/
NSLog(@"%@", error.description);
}
}];
} else {
// Permissions are present, we can request the user information
[self makeRequestToSharePhoto];
}
} else {
// There was an error requesting the permission information
// See our Handling Errors guide: https://developers.facebook.com/docs/ios/errors/
NSLog(@"%@", error.description);
}
}];
}
}
/**
* 上傳核心程式
*/
- (void)makeRequestToSharePhoto
{
UIImage *img = [UIImageimageNamed:@"B04.gif"];
[[FBRequestrequestForUploadPhoto:img] startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"error is %d", error != nil);
if (error != nil){
NSLog(@"%@", error.userInfo);
}
}];
}
會出現403或400的問題,主因是我原本以為直接呼叫 [self makeRequestToSharePhoto] 就可以,後來參考其它程式,先取得權限的作法,之後再將核心的部份換成呼叫[self makeRequestToSharePhoto],就成功了! (iOS 5平台才會有這個問題,iOS6以上直接使用官方Sample即可 )
參考資料:
https://developers.facebook.com/docs/ios/sample-apps#tutorials
留言列表