Facebook Friend Invite的Swift 2.0代码

时间:2015-08-11 14:10:27

标签: ios facebook swift facebook-graph-api swift2

我一直在为iOS应用的Facebook好友邀请寻找一个等效的Swift代码示例。但我无法找到它们。

据我所知,Facebook页面https://developers.facebook.com/docs/app-invites/ios上有Objective-C版本。但是,因为我从Swift开始,我觉得很难翻译。

有人能指出我的消息来源吗?谢谢。

3 个答案:

答案 0 :(得分:11)

代码正常工作:

-In viewDidLoad:

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
    }
}

答案 1 :(得分:7)

Facebook好友邀请加入Swift 3.0

首先,导入 FBSDKCoreKit FBSDKShareKit 并添加委托 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)")
}

答案 2 :(得分:0)

Benobab解决方案是完美的,我只是想在我试图运行的情况下添加它 viewDidAppear上的FBSDKAppInviteDialog.showFromViewController工作得更好。