1.facebook ,twitter等
通过iphone自带的分享,首先导入Social.framework,然后导入
#import <Social/Social.h>
最后通过以下代码完成分享:
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:sharedName];
BOOL success = [composeVc setInitialText:self.sharedContent];//分享内容
BOOL imageSuccess = [composeVc addImage:self.sharedImage];//分享的图片
//回调
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(@"Cancelled");
}else{
NSLog(@"Post");
}
[composeVc dismissViewControllerAnimated:YES completion:Nil];
};
pletionHandler = myBlock;
if(success && imageSuccess)
[self presentViewController:composeVcanimated:YES completion:nil];
}
2.Instagram稍微麻烦一点,通过 UIDocumentInteractionController完成分享,首先还是要先判断Instagram是否存在,如果存在再进行分享的操作。 代码如下:
- (void)instagramShare:(UIView*)view{
UIImage* image = [UIImage imageNamed:@"csh.jpg"];//分享的图片
CGFloat cropVal = (image.size.height > image.size.width ? image.size.width : image.size.height);
cropVal *= [image scale];
CGRect cropRect = (CGRect){.size.height = cropVal, .size.width = cropVal};
CGImageRef imageRef = CGImageCreateWithImageInRect([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]) {
// failure
NSLog(@"image save failed to path %@", writePath);
return;
} else {
// success.
}
// send it to instagram.
NSURL *fileURL = [NSURL fileURLWithPath:writePath];
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
self.documentController.delegate = self;
[self.documentController setUTI:@"com.instagram.exclusivegram"];
[self.documentController setAnnotation:@{@"InstagramCaption" : @"My love dyl"}];
CGRect rect = CGRectMake(0 ,0 , 0, 0);
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIGraphicsEndImageContext();
[self.documentController presentOpenInMenuFromRect:rect inView:view animated:YES];
}