2015-08-16 48 views
0

工作作爲一個新手,我想嘗試Facebook和Twitter發佈使用Objective-C的Social.framework不適合的Xcode 6.3.2

所以我加了2個按鈕,加入行動吧,還增加了社會框架。當我運行代碼並單擊Tweet或Facebook按鈕時,什麼都沒有發生,我是否缺少了某些東西。我在模擬器上測試它,這是否有所作爲。

這裏是我的頭文件SocialSharingViewController.h

#import <UIKit/UIKit.h> 

@interface SocialSharingViewController : UIViewController 

- (IBAction)postToTwitter:(id)sender; 
- (IBAction)postToFacebook:(id)sender; 

@end 

這裏是我的.m文件SocialSharingViewController.m

#import "SocialSharingViewController.h" 
#import <Social/Social.h> 

@interface SocialSharingViewController() 

@end 

@implementation SocialSharingViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (IBAction)postToTwitter:(id)sender { 
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) 
    { 
     SLComposeViewController *tweetSheet = [SLComposeViewController 
               composeViewControllerForServiceType:SLServiceTypeTwitter]; 
     [tweetSheet setInitialText:@"Great fun to learn iOS programming"]; 
     [self presentViewController:tweetSheet animated:YES completion:nil]; 
    } 
} 

- (IBAction)postToFacebook:(id)sender { 
    NSLog(@"clicked facebook button"); 
    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { 
     SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; 

     [controller setInitialText:@"First post from my iPhone app"]; 
     [self presentViewController:controller animated:YES completion:Nil]; 
    } 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 
+0

要測試iPhone或模擬器嗎? –

+0

@YunusErenGüzel我正在模擬器上測試它 – user580950

+0

如果您確定'isAvailable'方法可達,您應該在實際的手機上進行測試。 –

回答

1

有幾件事情浮現在腦海中:

  • 你有沒有將.xib中的按鈕連接到代碼中的IBAction方法?
  • -isAvailableForServiceType:合法返回NO,阻止它們顯示?
+0

1)是按鈕連接在故事板中。 (2)我如何檢查-isAvailableForServiceType:是否合法返回NO? – user580950

+0

設置一個斷點,看看你的if語句是否被輸入。該方法可能不適用於模擬器或其他東西。 –