2014-08-31 78 views
3

嘿那裏我的應用程序幾乎準備好發佈,但我想添加Facebook分享按鈕。事情是我不知道場景和viewcontroler之間的溝通是如何工作的。我做了我的研究,但只發現在OBJ-C代碼這樣一個swift spritekit Facebook的分享按鈕

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

[夫特和在同一項目中的Objective-C](https://developer.apple。 com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html),他們很容易在Swift中使用現有的Objective-C類。 – chuckus 2014-08-31 19:57:02

回答

13

這是我前段時間爲twitter所做的一些代碼,它仍然可以快速運行。我向你展示如何將它轉換爲下面的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,只需簡單更換SLServiceTypeTwitterSLServiceTypeFacebook並重命名變量可讀性。如果你想從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工作表。請記住將我的字符串替換爲與您的項目相關的字符串。希望這會有所幫助 - 對不起,這是一個很長的答案!

+0

我可以使用Facebook徽標作爲分享按鈕嗎?非常有用的瞭解了我需要的所有東西。thx – 2014-09-02 18:43:51

+0

@BasileBerckmoes我查了一下,你可以在4個(也只有4個)場景中使用「f」標誌。如果你將它用於別的東西,你會違反規則。製作你想要的內容: 1.指您在Facebook上的存在,如您的Page,時間軸,羣組,應用程序或事件 2.在您的網站/應用程序中暗示您實施Facebook 3.您的產品與Facebook,如'用於Facebook' 4.來自Facebook的內容 – 2014-09-02 18:47:35

+0

@BasileBerckmoes然而,如果您使用多個社交媒體(如twitter)並將徽標並排放置,那麼您可以做你喜歡什麼圖標 - 並且規則不再適用。不知道爲什麼這樣,但我個人會利用它,並使用我發佈的相同代碼添加到twitter中 - 這樣您就不會違反任何版權法。希望能夠解決問題。查看這個網站,瞭解幾乎所有可能合併的公司的信息。http://www.qualitylogoproducts.com/blog/social-media-logos-promotional-items/ – 2014-09-02 18:52:32

1
func lkFaceBookShare() { 
    var serviceType: String = SLServiceTypeFacebook 
    if !SLComposeViewController.isAvailableForServiceType(serviceType) { 
     self.showUnavailableAlertForServiceType(serviceType) 
    } 
    else { 
     var composeViewController: SLComposeViewController = SLComposeViewController.composeViewControllerForServiceType(serviceType) 
     var keyWindow: UIWindow = UIApplication.sharedApplication().keyWindow 
     var rect: CGRect = keyWindow.bounds 
     UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0.5) 
     self.view!.drawViewHierarchyInRect(rect, afterScreenUpdates: true) 
     var viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     composeViewController.addImage(viewImage) 
     var initalTextString: String = String(format: "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.initialText = initalTextString 
     var vc: UIViewController = self.view.window.rootViewController 
     vc.presentViewController(composeViewController, animated: true, completion: { _ in }) 
    } 
} 

func showUnavailableAlertForServiceType(serviceType: String) { 
    var serviceName: String = "" 
    if serviceType == SLServiceTypeFacebook { 
     serviceName = "Facebook" 
    } 
    else if serviceType == SLServiceTypeSinaWeibo { 
     serviceName = "Sina Weibo" 
    } 
    else if serviceType == SLServiceTypeTwitter { 
     serviceName = "Twitter" 
    } 

    var alertView: UIAlertView = UIAlertView(title: "Account", message: "Please go to the device settings and add a \(serviceName) account in order to share through that service", delegate: nil, cancelButtonTitle: "Dismiss", otherButtonTitles: "") 
    alertView.show() 
} 

張貼由非常有幫助用戶的OBJ-C答案的夫特轉換...... original post