标签:
社交网络编程API
社交网络编程主要使用iOS提供的Social框架,目前Social框架主要包含两个类:
iOS6分享列表------UIActivityViewController
在iOS6之后提供了一个分享列表视图,它通过UIActivityViewController管理。苹果设计它的主要目的是替换分享动作选单(ActionSheet),分享动作选单是出于分享目的的动作选单。
内置活动列表项
内置了9个活动列表项目,他们是下面的9个常量定义的:
自定义活动列表项
有时候根据共享内容不同,需要自定义活动列表项。例如一个视频,需要共享到优酷。下面介绍自定义活动列表项。
凡是继承UIActivity抽象类的子类对象都可以放到列表中呈现出来。这包括了系统内置的9个活动列表,也可是是我们自定义的列表项。UIActivity抽象类中需要子类实现的方法如下:
自定义视图的用到的图片的大小
代码展示:
//自定义活动列表
- (IBAction)shareAction:(UIBarButtonItem *)sender { NSURL *urlTbShare = [NSURL URLWithString:@"http://www.notepadapi.sinaapp.com"]; NSArray *activityItems = @[urlTbShare]; BookActivity *bookActivity = [BookActivity new]; NSArray *applicationActivities = @[bookActivity]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities]; [self presentViewController:activityVC animated:YES completion:nil]; }
BookActivity.h
#import <UIKit/UIKit.h> @interface BookActivity : UIActivity @property(nonatomic,strong)NSURL *url; @end
BookActivity.m
#import "BookActivity.h" @implementation BookActivity /*为自定义活动列表项指定一个服务类型标识 */ -(NSString *)activityType { return NSStringFromClass([self class]); } -(NSString *)activityTitle { return @"ActivityTitle"; } /**/ -(UIImage *)activityImage { return [UIImage imageNamed:@"image"]; } -(BOOL)canPerformWithActivityItems:(NSArray *)activityItems { for(id activityItem in activityItems) { if ([activityItem isKindOfClass:[NSURL class]]) { if ([[UIApplication sharedApplication] canOpenURL:activityItem]) { return YES; } } } return NO; } -(void)prepareWithActivityItems:(NSArray *)activityItems { for(id activityItem in activityItems) { if ([activityItem isKindOfClass:[NSURL class]]) { _url = activityItem; } } } -(void)performActivity { BOOL completed = [[UIApplication sharedApplication] openURL:_url]; [self activityDidFinish:completed]; } @end
撰写视图控制器---SLComposeViewController
它的呈现在iPhone采用模态视图,iPad则需要Popover视图呈现
如果发送微博信息并不像弹出分享列表,再去选择,而是在应用中直接进入写评论的地方(iOS系统提供的UI画面)。SLComposeViewController类可以帮助我们实现这个目的,SLComposeViewControlelr不仅可以撰写weibo,还可以撰写Tweet和Facebook.
发送这样的社交网络信息一般会包含3中信息---初始文本、图片和超链接,因此SLComposeViewController类设计3个相对方法:
代码展示
- (IBAction)zhuanxieAction:(UIBarButtonItem *)sender { SLComposeViewController *composeViewControlelr = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) { SLComposeViewControllerCompletionHandler __block completionHandler = ^(SLComposeViewControllerResult result) { [composeViewControlelr dismissViewControllerAnimated:YES completion:nil]; switch (result) { case SLComposeViewControllerResultCancelled: NSLog(@"Canceled...."); break; case SLComposeViewControllerResultDone: NSLog(@"Posted......"); break; default: break; } }; [composeViewControlelr addImage:[UIImage imageNamed:@"image"]]; [composeViewControlelr setInitialText:@"分享文本"]; [composeViewControlelr addURL:[NSURL URLWithString:@"http://www.baidu.com"]]; [composeViewControlelr setCompletionHandler:completionHandler]; [self presentViewController:composeViewControlelr animated:YES completion:nil]; } }
标签:
原文地址:http://www.cnblogs.com/zhanggui/p/4677087.html