您的位置:首页 > 理论基础 > 计算机网络

iOS社交网络编程——分享

2014-08-26 11:42 295 查看
iOS6之后提供了一个分享列表视图,它通过UIActivityViewController管理。

NSString *const UIActivityTypePostToFacebook;
NSString *const UIActivityTypePostToTwitter;
NSString *const UIActivityTypePostToWeibo;
NSString *const UIActivityTypeMessage;
NSString *const UIActivityTypeMail;
NSString *const UIActivityTypePrint;
NSString *const UIActivityTypeCopyToPasteboard;
NSString *const UIActivityTypeAssignToContact;
NSString *const UIActivityTypeSaveToCameraRoll;
NSString *const UIActivityTypeAddToReadingList;
NSString *const UIActivityTypePostToFlickr;
NSString *const UIActivityTypePostToVimeo;
NSString *const UIActivityTypePostToTencentWeibo;
NSString *const UIActivityTypeAirDrop;

内置的活动列表项如上,详细的内容请参考文档。
- (IBAction)shareAction:(id)sender {

NSString *textToShare = @"Test Share, 分享测试";
UIImage *imageToShare = [UIImage imageNamed:@"image.jpg"];
NSURL *urlToShare = [NSURL URLWithString:@"http://www.baidu.com"];

NSArray *activityItems = @[textToShare, imageToShare, urlToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
//不出现的活动项目
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];

UIActivityViewControllerCompletionHandler myBlock = ^(NSString *activityType,BOOL completed) {
NSLog(@"%@", activityType);
if(completed) {
NSLog(@"completed");
} else
{
NSLog(@"cancled");
}
[activityVC dismissViewControllerAnimated:YES completion:Nil];
};
activityVC.completionHandler = myBlock;
[self presentViewController:activityVC animated:YES completion:nil];
}运行效果如下;





如果发送微博并不想弹出分享列表,而是直接进入iOS系统提供的UI画面。SLComposeViewController类可以实现这个目的。

SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
SLComposeViewControllerCompletionHandler __block completionHandler = ^(SLComposeViewControllerResult result){
[composeVC dismissViewControllerAnimated:YES completion:nil];
switch (result) {
case SLComposeViewControllerResultCancelled:
default:
NSLog(@"Canceled...");
break;
case SLComposeViewControllerResultDone:
NSLog(@"Posted...");
break;
}
};
//添加图片
[composeVC addImage:[UIImage imageNamed:@"image.jpg"]];
//设置初始文本内容
[composeVC setInitialText:@"Test Share, 分享测试"];
//添加超链接信息
[composeVC addURL:[NSURL URLWithString:@"http://www.sina.com"]];
[composeVC setCompletionHandler:completionHandler];
[self presentViewController:composeVC animated:YES completion:nil];isAvailableForServiceType: 用来判断指定的社交网络服务是否可以使用,如果在系统中设置了社交网络账户信息,并且网络没问题的话,将返回YES。



如果要分享到Twitter和Facebook,把服务类型SLServiceTypeSinaWeibo 修改为SLServiceTypeSinaTwitter 或SLServiceTypeSinaFacebook 就可以了。

分享图片到Instagram

@property(nonatomic,retain)UIDocumentInteractionController *documentCtl;
-(IBAction)saveToInstagram:(id)sender {
NSURL *instagramURL = [NSURL URLWithString:@"instagram://app"];
if([[UIApplication sharedApplication] canOpenURL:instagramURL])
{
CGFloat cropVal = (imageView.image.size.height > imageView.image.size.width ? imageView.image.size.width : imageView.image.size.height);
cropVal *= [imageView.image scale];
CGRect cropRect = (CGRect){.size.height = cropVal, .size.width = cropVal};
CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], cropRect);
NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:imageRef], 1.0);
CGImageRelease(imageRef);

NSString *writePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"instagram.igo"];
if (![imageData writeToFile:writePath atomically:YES]) {
//保存图片失败
NSLog(@"image save failed to path %@", writePath);
return;
} else {
//保存图片成功
}

NSURL *fileURL = [NSURL fileURLWithPath:writePath];
self.documentCtl = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
self.documentCtl.delegate = self;
[self.documentCtl setUTI:@"com.instagram.exclusivegram"];
[self.documentCtl setAnnotation:@{@"InstagramCaption" : @"We are making fun"}];
[self.documentCtl presentOpenInMenuFromRect:CGRectMake(0, 0, 320, 480) inView:self.view animated:YES];
}
else
{
NSLog (@"Instagram not found");

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息