gpt4 book ai didi

ios - 如何在 IOS 中使用 Swift 在 Facebook 上分享图片

转载 作者:搜寻专家 更新时间:2023-11-01 06:48:03 24 4
gpt4 key购买 nike

我想使用快速语言在 Facebook 上分享一张图片。我可以使用 Objective C 共享图像。我尝试使用

1) How to Share image + text with facebook in swift iOS?

但不起作用,然后我尝试使用其他选项但无法使用快速语言共享图像。然后我试了这个

2) https://github.com/rajeshsegu/facebook-ios-swift/tree/master/FBApp

我复制了 Facebook.swift 并为 Facebook.swift 编写了另一个共享图像的功能

import Foundation
import Social

let FB = Facebook();

class Facebook {

var fbSession:FBSession?

init(){
self.fbSession = FBSession.activeSession();
}

func hasActiveSession() -> Bool{
let fbsessionState = FBSession.activeSession().state;
if ( fbsessionState == FBSessionState.Open
|| fbsessionState == FBSessionState.OpenTokenExtended ){
self.fbSession = FBSession.activeSession();
return true;
}
return false;
}

func login(callback: () -> Void){

let permission = ["publish_actions","email","user_location","user_birthday","user_hometown","user_photos","user_about_me"];
let activeSession = FBSession.activeSession();
let fbsessionState = activeSession.state;
var showLoginUI = true;

if(fbsessionState == FBSessionState.CreatedTokenLoaded){
showLoginUI = false;
}

if(fbsessionState != FBSessionState.Open
&& fbsessionState != FBSessionState.OpenTokenExtended){

FBSession.openActiveSessionWithPublishPermissions(permission, defaultAudience: FBSessionDefaultAudience.Friends, allowLoginUI: showLoginUI, completionHandler: { (session:FBSession!, state:FBSessionState, error:NSError!) -> Void in

if(error != nil){
println("Session Error: \(error)");
}
self.fbSession = session;
// println("Session : \(self.fbSession?.permissions)");
callback();

})

// FBSession.openActiveSessionWithReadPermissions(
// permission,
// allowLoginUI: showLoginUI,
// completionHandler: { (session:FBSession!, state:FBSessionState, error:NSError!) in
//
// if(error != nil){
// println("Session Error: \(error)");
// }
// self.fbSession = session;
// println("Session : \(self.fbSession?.permissions)");
// callback();
//
// }
// );
return;
}

callback();

}

func logout(){
self.fbSession?.closeAndClearTokenInformation();
self.fbSession?.close();
}

func getInfo(){
FBRequest.requestForMe()?.startWithCompletionHandler({(connection:FBRequestConnection!, result:AnyObject!, error:NSError!) in

if(error != nil){
println("Error Getting ME: \(error)");
}

println("\(result)");
var dictData:NSDictionary!=result as? NSDictionary
});
}

func handleDidBecomeActive(){
FBAppCall.handleDidBecomeActive();
}

func shareImage (imageName:UIImageView){
let fbsessionState = FBSession.activeSession().state;
if(fbsessionState == FBSessionState.Open)
{
//var arr : NSArray=NSArray(array: ["publish_actions"])
self.fbSession?.requestNewPublishPermissions(["publish_actions"], defaultAudience:FBSessionDefaultAudience.Friends, completionHandler: { (session:FBSession!, error:NSError!) -> Void in
if(error == nil){
var requestConneciton:FBRequestConnection=FBRequestConnection()
requestConneciton.errorBehavior=FBRequestConnectionErrorBehavior.None

requestConneciton.addRequest(FBRequest(forUploadPhoto:imageName.image)) { (connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in
println("\(error)");
println("\(result)");
//[self showAlert:@"Photo Post" result:result error:error];
}
requestConneciton.start()

}
else if(error.fberrorCategory == FBErrorCategory.UserCancelled){
var alt:UIAlertView!=UIAlertView(title:"Permission denied", message:"Unable to get permission to post", delegate:nil, cancelButtonTitle:"Ok")
alt.show()
}

})
}
}

func showAlert(msg:NSString!,result:AnyObject,error:NSError!) {
var alertTitle:NSString!
var alertMsg:NSString!;
if (error == nil) {
if((error.fberrorUserMessage != nil && FBSession.activeSession().isOpen) ) {
alertTitle = "";
}
else{
// Otherwise, use a general "connection problem" message.
alertMsg = "Operation failed due to a connection problem, retry later.";
}
}
else {
//var dictResult:NSDictonary = result as NSDictionary
alertMsg="Successfully posted "
var alertObj:UIAlertView!=UIAlertView(title:"Demo App", message:alertMsg, delegate:nil, cancelButtonTitle:"Ok");
alertObj.show();
}
}

func performPublishAction(action:() -> Void){

var arrP:NSArray!=NSArray(array: ["publish_actions"]);

fbSession?.requestNewPublishPermissions(arrP, defaultAudience:FBSessionDefaultAudience.Friends, completionHandler: { (session:FBSession!, error:NSError!) -> Void in

if(error == nil){
action()
}
else if(error.fberrorCategory == FBErrorCategory.UserCancelled){
var alt:UIAlertView!=UIAlertView(title:"Permission denied", message:"Unable to get permission to post", delegate:nil, cancelButtonTitle:"Ok")
alt.show()
}

})
}
}

在 ViewController.swift 中

import UIKit

class ViewController: UIViewController,FBLoginViewDelegate {

@IBOutlet var imageObj:UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func btnFBLoginClick(sender: UIButton) {
FB.login(self.handleLogin);
}

func handleLogin(){
println("SUCCESS");
FB.getInfo();
}

@IBAction func btnShareclick(sender: UIButton) {
FB.shareImage(imageObj)
}

}

登录按钮点击工作完美,它可以获取登录用户的所有数据,但是当我使用 FB.shareImae(imageObj) 共享图像时,它给我一个权限错误,我正在处理这个从过去 2 天开始,现在我被困住了。如果我在 Objective C 中编写相同的代码,它工作正常。

错误:

permissions:(
"public_profile",
email,
"user_friends"
)>, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 200;
message = "(#200) Permissions error";
type = OAuthException;
};
};
code = 403;
}}

谁能帮帮我?找出这个问题...

我不想使用 SLComposeViewController,我想使用 Facebook 框架。

提前致谢!

最佳答案

这是我在旧项目中创建的代码。试一试 ;)

  @IBAction func btn_Share(sender: AnyObject) {

let facebookPost = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
facebookPost.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
//Code to deal with it being cancelled
break

case SLComposeViewControllerResult.Done:
//Code here to deal with it being completed
break
}
}

facebookPost.setInitialText("Test Facebook") //The default text in the tweet
facebookPost.addImage(masked_image) //Add an image
facebookPost.addURL(NSURL(string: "http://facebook.com")) //A url which takes you into safari if tapped on

self.presentViewController(facebookPost, animated: false, completion: {
//Optional completion statement
})
}`

关于ios - 如何在 IOS 中使用 Swift 在 Facebook 上分享图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27717709/

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