嘿,我的应用程序几乎已准备好发布,但我想添加一个Facebook分享按钮。事情是我不知道场景和viewcontroler之间的通信是如何工作的。我做了我的研究,但只在obj-c中找到了像这样的代码
- (void)lkFaceBookShare {
NSString *serviceType = SLServiceTypeFacebook;
if (![SLComposeViewController isAvailableForServiceType:serviceType])
{
[self showUnavailableAlertForServiceType:serviceType];
}
else
{
SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:serviceType];
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.5f);
[self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[composeViewController addImage:viewImage];
NSString *initalTextString = [NSString stringWithFormat:@"Let's join together in the form of underground catch word go along with me!! Link: https://itunes.apple.com/us/app/uoi-hinh-bat-chu-gioi-duoi/id907330926?ls=1&mt=8"];
[composeViewController setInitialText:initalTextString];
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController:composeViewController animated:YES completion:nil];
}
}
- (void)showUnavailableAlertForServiceType:(NSString *)serviceType
{
NSString *serviceName = @"";
if (serviceType == SLServiceTypeFacebook)
{
serviceName = @"Facebook";
}
else if (serviceType == SLServiceTypeSinaWeibo)
{
serviceName = @"Sina Weibo";
}
else if (serviceType == SLServiceTypeTwitter)
{
serviceName = @"Twitter";
}
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Account"
message:[NSString stringWithFormat:@"Please go to the device settings and add a %@ account in order to share through that service", serviceName]
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];
[alertView show];
}
我的经验和知识太低,太过迅速,所以我需要一些帮助这个D:
由于
答案 0 :(得分:13)
这是我之前为twitter做过的一些代码,它仍然可以在swift中运行。我将向您展示如何将其转换为Facebook。把它作为你的viewController:
func showTweetSheet() {
let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetSheet.completionHandler = {
result in
switch result {
case SLComposeViewControllerResult.Cancelled:
//Add code to deal with it being cancelled
break
case SLComposeViewControllerResult.Done:
//Add code here to deal with it being completed
//Remember that dimissing the view is done for you, and sending the tweet to social media is automatic too. You could use this to give in game rewards?
break
}
}
tweetSheet.setInitialText("Test Twitter") //The default text in the tweet
tweetSheet.addImage(UIImage(named: "TestImage.png")) //Add an image if you like?
tweetSheet.addURL(NSURL(string: "http://twitter.com")) //A url which takes you into safari if tapped on
self.presentViewController(tweetSheet, animated: false, completion: {
//Optional completion statement
})
}
要将其转换为Facebook,只需将SLServiceTypeTwitter
交换为SLServiceTypeFacebook
并重命名变量以提高可读性。如果你想从SKScene调用这个方法,你必须以某种方式提醒你想要它调用方法的viewController。
我首选的方法是使用NSNotificationCenter,以便我从场景发布一个警报,并由viewController接收,以便它触发一个方法。这也非常容易设置。在场景中,您需要将此行代码放在您想要调用Facebook弹出窗口的任何位置:
NSNotificationCenter.defaultCenter().postNotificationName("WhateverYouWantToCallTheNotification", object: nil)
这会发出一个带有名字的通知。现在,在viewController中,您需要通过将以下代码放在viewDidLoad,viewDidAppear或类似的东西中来订阅此警报。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "ThisIsTheMethodName", name: "WhateverYouCalledTheAlertInTheOtherLineOfCode", object: nil)
现在场景将与viewController通信,您将能够显示Facebook表格。请记住用相对于项目的字符串替换我的字符串。希望这会有所帮助 - 对不起,这是一个很长的答案!
答案 1 :(得分:1)
callBack
一个非常有用的用户发布的Obj-C答案的快速转换...... original post