ios 中第三方分享 是怎麽 實現的
(1)官方下載ShareSDK?iOS?2.8.8?
(2)根據實際情況,引入相關的庫,參考官方文檔。
(3)在項目的AppDelegate中壹般情況下有三個操作,第壹是註冊ShareSDK,第二是註冊各個平臺的賬號,第三是關於微信等應用的回調處理。
(4)信息分享。
-(IBAction)share:(id)sender{
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"card" ?ofType:@"png"];
//構造分享內容
id<ISSContent> publishContent = [ShareSDK content:@"分享內容測試"
defaultContent:@"默認分享內容測試,沒內容時顯示"
image:[ShareSDK imageWithPath:imagePath]
title:@"pmmq"
? url:@""
? description:@"這是壹條測試信息"
mediaType:SSPublishContentMediaTypeNews];
[ShareSDK showShareActionSheet:nil
?shareList:nil
content:publishContent
?statusBarTips:YES
authOptions:nil
? shareOptions: nil
result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (state == SSResponseStateSuccess)
{
NSLog(@"分享成功");
}
else if (state == SSResponseStateFail)
{
NSLog(@"分享失敗");
}
}];
}
(5)登錄、登出、獲取授權信息、關註制定微博
//
// ?LoginViewController.m
// ?ShareSDKTest
//
// ?Created by wangdalei on 14-6-23.
// ?Copyright (c) 2014年 王大雷. All rights reserved.
//
#import "LoginViewController.h"
#import <ShareSDK/ShareSDK.h>
@interface LoginViewController ()
-(IBAction)loginWithSina:(id)sender;
-(IBAction)loginWithQQ:(id)sender;
-(IBAction)loginoutWithSina:(id)sender;
-(IBAction)loginoutWithQQ:(id)sender;
-(IBAction)guanzhuUs:(id)sender;
-(void)reloadStateWithType:(ShareType)type;
@end
@implementation LoginViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)loginWithSina:(id)sender {
[ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
NSLog(@"%d",result);
if (result) {
//成功登錄後,判斷該用戶的ID是否在自己的數據庫中。
//如果有直接登錄,沒有就將該用戶的ID和相關資料在數據庫中創建新用戶。
[self reloadStateWithType:ShareTypeSinaWeibo];
}
}];
}
-(IBAction)loginWithQQ:(id)sender{
[ShareSDK getUserInfoWithType:ShareTypeQQSpace authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
NSLog(@"%d",result);
if (result) {
//成功登錄後,判斷該用戶的ID是否在自己的數據庫中。
//如果有直接登錄,沒有就將該用戶的ID和相關資料在數據庫中創建新用戶。
[self reloadStateWithType:ShareTypeQQSpace];
}
}];
}
-(IBAction)loginoutWithSina:(id)sender{
[ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
[self reloadStateWithType:ShareTypeSinaWeibo];
}
-(IBAction)loginoutWithQQ:(id)sender{
[ShareSDK cancelAuthWithType:ShareTypeQQSpace];
[self reloadStateWithType:ShareTypeQQSpace];
}
-(void)reloadStateWithType:(ShareType)type{
//現實授權信息,包括授權ID、授權有效期等。
//此處可以在用戶進入應用的時候直接調用,如授權信息不為空且不過期可幫用戶自動實現登錄。
id<ISSPlatformCredential> credential = [ShareSDK getCredentialWithType:type];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示")
message:[NSString stringWithFormat:
?@"uid = %@\ntoken = %@\nsecret = %@\n expired = %@\nextInfo = %@",
?[credential uid],
?[credential token],
?[credential secret],
?[credential expired],
?[credential extInfo]]
delegate:nil
? cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了")
? otherButtonTitles:nil];
[alertView show];
}
//關註用戶
-(IBAction)guanzhuUs:(id)sender{
[ShareSDK followUserWithType:ShareTypeSinaWeibo //平臺類型
field:@"ShareSDK" //關註用戶的名稱或ID
fieldType:SSUserFieldTypeName //字段類型,用於指定第二個參數是名稱還是ID
?authOptions:nil //授權選項
viewDelegate:nil //授權視圖委托
? result:^(SSResponseState state, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
? if (state == SSResponseStateSuccess) {
? NSLog(@"關註成功");
? } else if (state == SSResponseStateFail) {
? NSLog(@"%@", [NSString stringWithFormat:@"關註失敗:%@", error.errorDescription]);
? }
? }];
}
@end
(5)妳可能會看到壹些應用需要第三方登錄的,壹種是彈出webView加載的新浪微博或者qq的網頁授權,還有壹種是跳轉到本地的已經安裝的新浪微博應用或者qq應用進行授權。第二種授權方式較SSO授權,體驗會比較好壹些,因為不需要用戶輸入新浪微博或QQ的用戶名與密碼。
第二種授權方式需要在plist中配置Scheme。SSO默認是打開的不需要配置。在AppDelegate中實現回調。