gpt4 book ai didi

ios - 必须指定打开的 FBSession 才能在 iOS 中调用此端点

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:42 26 4
gpt4 key购买 nike

我是 iOS 开发新手。我想将图像上传到 Facebook 页面。当从相机中拾取图像时。为此,我编写了如下代码,但出现错误“必须指定 FBSession 才能调用此端点”。请给我解决方案。

我的代码就像

-(IBAction)cameraButtonPressed:(id)sender
{
if (FBSession.activeSession.isOpen)
{
UIImage *uploadImage=[UIImage imageNamed:@"2.png"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:accesstoken, @"CAACEdEose0cBAAFiJ51qowOOBfOcobm1E3v1ZBZCNYBSZAyspbQCXPUZCdZC620bRWYvChxGd96ZAOSMNyRZA9TYd9teQ9Q1m67wKx0n66YjTiYZBxczKIZB7t3tuLO5RYsNbZBfGweZCenMXRF7xOBE6DQ6450nmB0hRJzI8PrT18WZAfU878GhNryp5Vw4Up3jGZCwPWmuMxDZAoZAXVosyZBNKQ6i",
postingString, @"message",uploadImage,@"source",
nil];
FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil
graphPath:@"/me/feed"
parameters:params
HTTPMethod:@"POST"];
FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init];
[requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(@"facebook result >> %@", result);
}];
[requestToPostConnection start];
}
else
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
@"user_likes",
@"read_stream",
@"publish_actions",
nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:session];
}
if([FBSession openActiveSessionWithAllowLoginUI:NO])
{
UIImage *uploadImage=[UIImage imageNamed:@"2.png"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:accesstoken, @"CAACEdEose0cBAAFiJ51qowOOBfOcobm1E3v1ZBZCNYBSZAyspbQCXPUZCdZC620bRWYvChxGd96ZAOSMNyRZA9TYd9teQ9Q1m67wKx0n66YjTiYZBxczKIZB7t3tuLO5RYsNbZBfGweZCenMXRF7xOBE6DQ6450nmB0hRJzI8PrT18WZAfU878GhNryp5Vw4Up3jGZCwPWmuMxDZAoZAXVosyZBNKQ6i",
postingString, @"message",uploadImage,@"source",
nil];
FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil
graphPath:@"/me/feed"
parameters:params
HTTPMethod:@"POST"];
FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init];
[requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(@"facebook result >> %@", result);
}];
[requestToPostConnection start];

}
else
{
NSLog(@"Nothing to DO BYE");
}

这里是我的 PList 文件,如下所示

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb74549074886xxxx</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>FacebookAppID</key>
<string>7454907xxxxxx</string>
<key>FacebookDisplayName</key>
<string>Cele­b­r­a­t­e­C­h­r­i­s­tmas</string>

最佳答案

-(IBAction)cameraButtonPressed:(id)sender
{
// here add your image did finish method

if ([[FBSession activeSession] isOpen]) {
/*
* if the current session has no publish permission we need to reauthorize
*/
if ([[[FBSession activeSession] permissions]indexOfObject:@"publish_stream"] == NSNotFound) {

[[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObjects: @"publish_stream",nil] defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session,NSError *error){
[self postData];
}];

}else{
[self postData];
}
}else{

/*
* open a new session with publish permission
*/

[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObjects: @"publish_stream", nil]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {
[self postData];
}else{
NSLog(@"error");
}
}];
}


}



-(void)postData
{
UIImage *uploadImage=[UIImage imageNamed:@"2.png"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:accesstoken, @"CAACEdEose0cBAAFiJ51qowOOBfOcobm1E3v1ZBZCNYBSZAyspbQCXPUZCdZC620bRWYvChxGd96ZAOSMNyRZA9TYd9teQ9Q1m67wKx0n66YjTiYZBxczKIZB7t3tuLO5RYsNbZBfGweZCenMXRF7xOBE6DQ6450nmB0hRJzI8PrT18WZAfU878GhNryp5Vw4Up3jGZCwPWmuMxDZAoZAXVosyZBNKQ6i",
postingString, @"message",uploadImage,@"source",
nil];
FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil
graphPath:@"/me/feed"
parameters:params
HTTPMethod:@"POST"];
FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init];
[requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(@"facebook result >> %@", result);
}];
[requestToPostConnection start];
}

关于ios - 必须指定打开的 FBSession 才能在 iOS 中调用此端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27328332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com