用shareSDK實現的簡單分享


第一步:將ShareSDK導入到你的工程中

然后需要在工程的AppDelegate.m中導入所需要的頭文件
比如:
#import <ShareSDK/ShareSDK.h>
#import "WeiboApi.h"
#import  “WXApi.h"

第二步:在下面方法中添加如下代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [ShareSDK registerApp:@"2864c8d42dd3 ];  // @“2864c8d42dd3” ShareSDK官網中添加應用后得到的AppKey
    
   添加相應的應用需要獲得相應應用的APPkey和APPSecret

   
 //添加新浪微博應用 注冊網址  http://open.weibo.com
    [ShareSDK connectSinaWeiboWithAppKey:@"1028290959"
                               appSecret:@"337c15ce9b1885cf307fd4f5bc5a9594"
                             redirectUri:@" http://www.sina.com"];
   
//    //添加騰訊微博應用 注冊網址  http://dev.t.qq.com
    [ShareSDK connectTencentWeiboWithAppKey:@"801528473"
                                  appSecret:@"9101e92b599a40712212547be6977c2a"
                                redirectUri:@" http://www.shishangmao.com"
                                   wbApiCls:[WeiboApi class]];
   
    //添加微信應用 注冊網址  http://open.weixin.qq.com
    [ShareSDK connectWeChatWithAppId:@"wx8216675fb452a3d6"
                           wechatCls:[WXApi class]];
    //連接郵件
    [ShareSDK connectMail];
 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.viewController = [[ViewController alloc] init];
    self.window.rootViewController = self.viewController;
   
    [self.window makeKeyAndVisible];
    return YES;
}
 
注:以下代碼為使用sso授權時添加(若不使用程序運行后將會跳到相應的web頁進行授權提示)

使用SSO授權不需要重復輸入新浪微博用戶名、密碼,只需要一步操作,直接點擊授權按鈕即可完成授權,增強了操作簡便性及帳號安全性。SDK中默認是開啟SSO授權的,對需要實現SSO的平台進行一下配置就可以使用了。(就是說如果你安裝了相應應用的客戶端軟件之后,只需要登錄一次 ,程序運行經過一次授權之后,再運行的話就不會重新提示授權,而是可直接發布(即使工程卸載重裝也會記錄相應授權的信息))

由於SSO需要跳轉到客戶端進行授權驗證,因此需要處理返回消息。在*AppDelegate.m(*代表你的工程名字)文件中加入如下方法: 

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url
                        wxDelegate:self];
}


- (BOOL)application:(UIApplication *)application
         openURL:(NSURL *)url
         sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [ShareSDK handleOpenURL:url
                   sourceApplication:sourceApplication
                   annotation:annotation
                   wxDelegate:self];
}
 
使用sso授權也需要添加相應應用的URL Schemes 如:新浪微博。。。。。

新浪微博

打開*-Info.plist(*代表你的工程名字)。找到URL types配置項(如果沒有則新增),展開URL types – URL Schemes,在URL Schemes下分別各新增一項用於新浪微博的Scheme(如果不添加則會導致法返回應用)。其填寫格式為:wb+appKey(你在新浪微博申請的AppKey), 如:wb2279784657。如圖所示:

ios doc 039.jpg

 
 
 

第三步:分享內容

進行內容分享首選需要構造一個內容對象,該對象用於指定哪些內容需要分享到社會化平台中。然后再通過showShareActionSheet方法進行分享。 分享內容一般在 viewDidAppear 事件之后、響應用戶動作的方法中進行,如:響應按鈕按下的方法中。 代碼如下:

NSString  * imagePath =  [[NSBundle  mainBundle]  pathForResource:@ “1"   ofType:@"jpg"] ;

    

//構造分享內容
///#begin zh-cn
/**
 *@brief創建分享內容對象,根據以下每個字段適用平台說明來填充參數值
 *
 *@param content 分享內容(新浪、騰訊、網易、搜狐、豆瓣、人人、開心、有道雲筆記、facebook、twitter、郵件、打印、短信、微信、QQ、拷貝)
 *@param defaultContent 默認分享內容(新浪、騰訊、網易、搜狐、豆瓣、人人、開心、有道雲筆記、facebook、twitter、郵件、打印、短信、微信、QQ、拷貝)
 *@param image 分享圖片(新浪、騰訊、網易、搜狐、豆瓣、人人、開心、facebook、twitter、郵件、打印、微信、QQ、拷貝)
 *@param title 標題(QQ空間、人人、微信、QQ)
 *@param url 鏈接(QQ空間、人人、instapaper、微信、QQ)
 *@param description 主體內容(人人)
 *@param mediaType 分享類型(QQ、微信)
 *
 *@return分享內容對象
 */
///#end
///#begin en
/**
 *@briefCreate share content object. based on the following description of each field to fill the parameter values
 *
 *@param content Share content string.(Sina Weibo、Tencent Weibo、NetEase Weibo、Sohu Weibo、Douban、RenRen、KaiXin、YouDaoNote、Facebook、Twitter、Mail、Print、SMS、WeChat、QQ、Copy)
 *@param defaultContent Default share content string.(Sina Weibo、Tencent Weibo、NetEase Weibo、Sohu Weibo、Douban、RenRen、KaiXin、YouDaoNote、Facebook、Twitter、Mail、Print、SMS、WeChat、QQ、Copy)
 *@param image Image attachment object.(Sina Weibo、Tencent Weibo、NetEase Weibo、Sohu Weibo、Douban、RenRen、KaiXin、Facebook、Twitter、Mail、Print、WeChat、QQ、Copy)
 *@param title Title string.(QZone、RenRen、WeChat、QQ)
 *@param url Url string.(QZone、RenRen、Instapaper、WeChat、QQ)
 *@param description Description string(RenRen)
 *@param mediaType MediaType(QQ、WeChat)
 *
 *@returnShare content object.
 */
///#end
id<ISSContent> publishContent =  [ ShareSDK content:@"分享內容"

                                          defaultContent:@"默認分享內容,沒內容時顯示"

                                          image:[ ShareSDK imageWithPath: imagePath]

                                          title: @"北京時尚貓科技有限公司"

                                          url: @" http://www.shishangmao.cn/"

                                          description:@"這是一條測試信息"

                                          mediaType: SSPublishContentMediaTypeNews] ;
////********************************************************************************************************************
若想要自定義授權界面可實現如下
    //創建容器
    id<ISSContainer> container = [ShareSDK container];
    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
   
    //通過viewDelegate:參數修改授權界面的導航欄背景
    //    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
    //                                                         allowCallback:YES
    //                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
    //                                                          viewDelegate:_appDelegate.viewDelegate
    //                                               authManagerViewDelegate:nil];
   
    //powerByHidden:這個參數是去掉授權界面Powered by ShareSDK的標志
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES      allowCallback:NO
                                                                scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleFullScreenPopup viewDelegate:nil authManagerViewDelegate:nil];
   
    //通過shareViewDelegate:參數修改分享界面的導航欄背景
    id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:@"內容分享"
                                                              oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                               qqButtonHidden:YES
                                                        wxSessionButtonHidden:YES
                                                       wxTimelineButtonHidden:YES
                                                         showKeyboardOnAppear:NO
                                                            shareViewDelegate:nil
                                                          friendsViewDelegate:nil
                                                        picViewerViewDelegate:nil];
****************************************************************************************///
///#begin zh-cn
/**
 *@brief顯示分享菜單
 *
 *@param container 用於顯示分享界面的容器,如果只顯示在iPhone客戶端可以傳入nil。如果需要在iPad上顯示需要指定容器。
 *@param shareList 平台類型列表
 *@param content 分享內容
 *  @param  statusBarTips   狀態欄提示標識:YES:顯示; NO:隱藏
 *  @param  authOptions 授權選項,用於指定接口在需要授權時的一些屬性(如:是否自動授權,授權視圖樣式等),默認可傳入nil
 *  @param  shareOptions    分享選項,用於定義分享視圖部分屬性(如:標題、一鍵分享列表、功能按鈕等),默認可傳入nil
 *  @param  result  分享返回事件處理
 */
///#end
///#begin en
/**
 *@briefShow share menu.
 *
 *@param container A container for the share view, if only displayed in the iPhone can pass nil. If you want to display on the iPad needs to be specified container.
 *@param shareList Platform type list.
 *@param content Share content object.
 *  @param  statusBarTips   The status bar Tip flag. YES indicates display. NO indicates hidden.
 *  @param  authOptions Authorized options,Used to authorization for custom configuration(Such as: whether the automatic authorization, authorization view style, etc.)Default nil.
 *  @param  shareOptions    Share options,Used to share for custom configuration(Such as: title, one key sharing, function buttons, etc.)Default nil.
 *  @param  result  Result handler.
 */
///#end
[ ShareSDK showShareActionSheet:nil(container)
                     shareList:nil

                     content: publishContent

                     statusBarTips:YES

                     authOptions:nil (authOptions)

                     shareOptions:  nil (shareOptions)

                     result:^( ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end)  {

                                               if  ( state ==  SSResponseStateSuccess)

                                               {

                                                       NSLog(@"分享成功") ;

                                               }

                                               else  if  ( state ==  SSResponseStateFail)

                                               {

                                                       NSLog(@"分享失敗,錯誤碼:%d,錯誤描述:%@" [ error errorCode] [ error errorDescription]) ;

                                               }

                            }]
 
 

第四步:實現評論功能


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM