swift2 Facebook好友邀请的Swift 2.0代码

um6iljoc  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(249)

我一直在为iOS应用的Facebook好友邀请寻找一个类似的Swift代码示例,但是我找不到。
我知道在Facebook页面https://developers.facebook.com/docs/app-invites/ios上有Objective-C版本,但是,因为我是从Swift开始的,所以我发现很难翻译。
谁能告诉我消息来源?谢谢。

lc8prwob

lc8prwob1#

工作代码:

  • 在视图DidLoad中:
let content = FBSDKAppInviteContent()
content.appLinkURL = NSURL(string: "https://test/myapplink")
content.appInvitePreviewImageURL = NSURL(string: "https://test/myapplink")
// Old Way, now depreciated :
//FBSDKAppInviteDialog.showFromViewController(self, withContent: content, delegate: self)
//New way : 
        FBSDKAppInviteDialog.showFromViewController(self, withContent: content, delegate: self)
// Do any additional setup after loading the view.
  • 在您的viewController中,以符合协议委托:
extension InviteFriendsViewController: FBSDKAppInviteDialogDelegate{
    func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [NSObject : AnyObject]!) {
        //TODO
    }
    func appInviteDialog(appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: NSError!) {
        //TODO
    }
}
h43kikqp

h43kikqp2#

Swift 3.0中的Facebook好友邀请

首先导入FBSDKCoreKitFBSDKShareKit并添加委托人FBSDKAppInviteDialogDelegate,然后在邀请好友按钮上点击,添加下面的代码:

let inviteDialog:FBSDKAppInviteDialog = FBSDKAppInviteDialog()
if(inviteDialog.canShow()){
    let appLinkUrl:NSURL = NSURL(string: "http://yourwebpage.com")!
    let previewImageUrl:NSURL = NSURL(string: "http://yourwebpage.com/preview-image.png")!

    let inviteContent:FBSDKAppInviteContent = FBSDKAppInviteContent()
    inviteContent.appLinkURL = appLinkUrl as URL!
    inviteContent.appInvitePreviewImageURL = previewImageUrl as URL!

    inviteDialog.content = inviteContent
    inviteDialog.delegate = self
    inviteDialog.show()
}

然后,添加FBSDKAppInviteDialogDelegate的以下方法:

func appInviteDialog (_ appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [AnyHashable : Any]!) {
    let resultObject = NSDictionary(dictionary: results)

    if let didCancel = resultObject.value(forKey: "completionGesture")
    {
        if (didCancel as AnyObject).caseInsensitiveCompare("Cancel") == ComparisonResult.orderedSame
        {
            print("User Canceled invitation dialog")
        } 
    } 
} 
func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: Error!) {
    print("Error tool place in appInviteDialog \(error)")
}
qlzsbp2j

qlzsbp2j3#

Benobab解决方案是完美的,我只想补充一点,在我的情况下,试图运行FBSDKAppInviteDialog。

相关问题