Warning: Undefined array key "custom_message" in /www/wwwroot/bbs.aaronyang.cc/wp-content/plugins/wpcopyrights/index.php on line 105

IOS-Twitter和Facebook


简介

Twitter已经整合到iOS5.0,而Facebook已经被集成在 iOS 6.0中。本教程的重点讲解如何利用苹果提供的类在iOS5.0和iOS6.0中部署Twitter和Facebook。

实例步骤

  1. 创建一个简单View based application

  2. 选择项目文件,然后选择targets(目标),然后在 choose frameworks(选择框架)中添加Social.framework 和 Accounts.framework

  3. 添加两个名为facebookPost 和 twitterPost的按钮,并为他们创建 ibActions。

  4. 更新 ViewController.h 如下

#import <Social/Social.h>
#import <Accounts/Accounts.h>
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

-(IBAction)twitterPost:(id)sender;
-(IBAction)facebookPost:(id)sender;

@end
  1. 更新ViewController.m ,如下所示
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

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

-(IBAction)facebookPost:(id)sender{

   SLComposeViewController *controller = [SLComposeViewController 
   composeViewControllerForServiceType:SLServiceTypeFacebook];
   SLComposeViewControllerCompletionHandler myBlock = 
   ^(SLComposeViewControllerResult result){
          if (result == SLComposeViewControllerResultCancelled)
          {                
            NSLog(@"Cancelled");                
          } 
          else                
          {
            NSLog(@"Done");
          }
          [controller dismissViewControllerAnimated:YES completion:nil];
        };
   controller.completionHandler =myBlock;        
   //Adding the Text to the facebook post value from iOS
   [controller setInitialText:@"My test post"];        
   //Adding the URL to the facebook post value from iOS
   [controller addURL:[NSURL URLWithString:@"http://www.test.com"]];        
   //Adding the Text to the facebook post value from iOS
   [self presentViewController:controller animated:YES completion:nil];    
}

-(IBAction)twitterPost:(id)sender{
    SLComposeViewController *tweetSheet = [SLComposeViewController 
    composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:@"My test tweet"];
    [self presentModalViewController:tweetSheet animated:YES];
}

@end

输出

运行该应用程序并单击 facebookPost 时我们将获得以下输出

FBTwit_Output1当我们单击 twitterPost 时,我们将获得以下输出

FBTwit_Output2

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/ios-twitter%e5%92%8cfacebook/

(0)
lyhlyh会员认证作者
上一篇 2023年 4月 1日 下午9:17
下一篇 2023年 4月 1日 下午9:20

相关推荐

  • getsebool

    文章目录getsebool补充说明语法选项实例 getsebool 查询SElinux策略内各项规则的布尔值 补充说明 getsebool命令 是用来查询SElinux策略内各项规…

    入门教程 2023年 12月 14日
  • linux 字符界面浏览器 w3m

    文章目录使用背景常用操作用快捷键页面操作超链接操作文件/流 操作缓存操作缓存选择模式(也就是按了s以後)书签操作搜索标记杂项行编辑模式 使用背景 linux无UI界面情况下,使用这…

    2023年 2月 17日
  • help

    文章目录help补充说明语法选项参数常见问题实例 help 该命令是bash内建命令,用于显示bash内建命令的帮助信息。 补充说明 help命令 help命令只能显示bash内建…

    入门教程 2023年 12月 15日
  • swapoff

    文章目录swapoff补充说明语法选项参数实例 swapoff 关闭指定的交换空间 补充说明 swapoff命令 用于关闭指定的交换空间(包括交换文件和交换分区)。swapoff实…

    入门教程 2024年 3月 11日
  • jq

    文章目录jq补充说明安装语法选项例子 jq 一个灵活的轻量级命令行JSON处理器 补充说明 jq 是 stedolan 开发的一个轻量级的和灵活的命令行JSON处理器,源码请参考 …

    入门教程 2023年 12月 19日
  • restore

    文章目录restore补充说明语法选项实例 restore 所进行的操作和dump指令相反 补充说明 restore命令 是dump命令的逆过程,用于还原dump命令生成的备份文件…

    入门教程 2024年 3月 4日
  • ifconfig

    文章目录ifconfig补充说明语法参数实例 ifconfig 配置和显示Linux系统网卡的网络参数 补充说明 ifconfig命令 被用于配置和显示Linux内核中网络接口的网…

    入门教程 2023年 12月 19日
  • jed

    文章目录jed补充说明语法选项参数实例 jed 主要用于编辑代码的编辑器 补充说明 jed命令 是由Slang所开发,其主要用用途是编辑程序的源代码。它支持彩色语法加亮显示,可以模…

    入门教程 2023年 12月 19日
  • shopt

    文章目录shopt补充说明语法选项参数实例 shopt 显示和设置shell操作选项 补充说明 shopt命令 用于显示和设置shell中的行为选项,通过这些选项以增强shell易…

    入门教程 2024年 3月 4日
  • rm

    文章目录rm补充说明语法选项参数实例 rm 用于删除给定的文件和目录 补充说明 rm 命令 可以删除一个目录中的一个或多个文件或目录,也可以将某个目录及其下属的所有文件及其子目录均…

    入门教程 2024年 3月 4日
Translate »