如何實現社交分享:
在iOS中實現社交分享的方法有三種:
->自己編寫各個平臺的分享代碼(代碼量較多)
->利用ios自帶的Social.framework
->利用第三方的分享框架
(百度還有個“社會化登錄組件”:http://developer.baidu.com/soc/login)
Social.framework
#import "ViewController.h"
#import <Social/Social.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 1.判斷平臺是否可用
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
NSLog(@"查看您是否設置了新浪微博帳號,設置界面-->新浪微博-->配置帳號");
}
// 2.創建SLComposeViewController
SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
// 2.1.添加分享文字
[composeVc setInitialText:@"做人如果沒有夢想,跟鹹魚有什麼區別"];
// 2.2.添加分享圖片
[composeVc addImage:[UIImage imageNamed:@"xingxing"]];
// 3.彈出分享界面
[self presentViewController:composeVc animated:YES completion:nil];
// 4.設置block屬性(監聽用戶是點擊了取消分享還是分享按鈕)
composeVc.completionHandler = ^ (SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultCancelled) {
NSLog(@"用戶點擊了取消");
} else {
NSLog(@"用戶點擊了發送");
}
};
}
@end
友盟分享
oauth2.0授權
SSO - 目前在國內使用比較多,如果本機安裝了某個應用程序,會直接進入該應用程序獲得授權。
URL Schemes填"sina."+你的友盟AppKey.
</pre></p><pre name="code" class="objc"><span style="font-size:18px;"></span>
<span style="font-size:18px;">#import "AppDelegate.h"
#import "UMSocial.h"
#import "UMSocialSinaHandler.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// appKey
[UMSocialData setAppKey:@"54ced694fd98c588d2000210"];
// sso授權地址
[UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSLog(@"%@", url);
return [UMSocialSnsService handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [UMSocialSnsService handleOpenURL:url];
}
@end
</span>
ViewController:
<span style="font-size:18px;">#import "ViewController.h"
#import "UMSocial.h"
@interface ViewController ()
- (IBAction)shareClick;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)shareClick {
NSString *shareText = @"出任CEO,贏取白富美,走向人生巔峯";
UIImage *shareImage = [UIImage imageNamed:@"xingxing"];
// [UMSocialSnsService presentSnsIconSheetView:self
// appKey:@"54ced694fd98c588d2000210"
// shareText:shareText
// shareImage:shareImage
// shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,nil]
// delegate:nil]; // 分享平臺
[UMSocialSnsService presentSnsController:self appKey:@"54ced694fd98c588d2000210" shareText:shareText shareImage:shareImage shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToEmail,nil] delegate:nil];
}
@end
</span>