天天看點

iOS 分享功能開發

iOS 開發過程中可能會遇到需要進行第三方分享的需求,比如向QQ,微信,微網誌等分享

如下圖

iOS 分享功能開發

我們今天要講到的方式是使用了一個第三方工具: http://www.sharesdk.cn

一,新增賬號

去官網注冊:http://www.sharesdk.cn

建立應用,最終獲得 App Key,App Secret

iOS 分享功能開發

二,SDK內建

下載下傳SDK

把下載下傳的SDK拖到項目工程,在彈出的對話框中選中"Copy items if needed"和"Create groups",并點選“Finish“按鈕

iOS 分享功能開發
添加依賴庫檔案 必須添加的依賴庫: SystemConfiguration.framework QuartzCore.framework  CoreTelephony.framework libicucore.dylib libz.1.2.5.dylib Security.framework
iOS 分享功能開發

根據社交平台需要添加的依賴庫

Social.framework    騰訊微網誌

Accounts.framework  騰訊微網誌

MessageUI.framework 短信和郵件

libstdc++.dylib   QQ好友和QQ空間SSO授權需要(新注冊的騰訊開放平台帳号隻支援SSO授權權限)

libsqlite3.dylib   QQ好友和QQ空間SSO授權需要(新注冊的騰訊開放平台帳号隻支援SSO授權權限)

CoreMotion.framework   Google+ 

CoreLocation.framework Google+

MediaPlayer.framework  Google+

CoreText.framework   Google+ 

AssetsLibrary.framework    Google+

AddressBook.framework  Google+

到此為止SDK內建就算完成 下來我們開始代碼部分

三,初始ShareSDK和社交平台

在AppDelegate.m中添加ShareSDK頭檔案

1.設定根視圖控制器

必須設定rootViewController。如果使用storyBoard,系統會自動設定根視圖控制器。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
    HelpViewController *mainController=[[HelpViewController alloc]init];
    _window.rootViewController=mainController;
    [_window makeKeyAndVisible];

    return YES;
}
           

以上示例代碼中講 HelpViewController 的一個對象設定為了 此應用的rootViewcontroller;

2.初始化ShareSDK

導入頭檔案,加上初始化方法。

#import <ShareSDK/ShareSDK.h>
           
//1.初始化ShareSDK應用,字元串"iosv1101"是應該換成你申請的ShareSDK應用中的Appkey
    [ShareSDK registerApp:@"iosv1101"];
    
    //2. 初始化社交平台
    //2.1 代碼初始化社交平台的方法
    [self initializePlat];
           

[self initializePlat ]  方法見下一步(此處以微信,qq,騰訊微網誌 ,新浪微網誌為例);

3.初始化社交平台

可以用代碼初始化或在ShareSDK開發者背景的社會化平台設定中填入社交平台的資訊。

#import "WXApi.h"
#import "WeiboSDK.h"
#import "WeiboApi.h"
#import <TencentOpenAPI/QQApi.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
           
社交平台的AppKey、AppSecret、回調位址等需要您自已去相應平台建立應用擷取。
- (void)initializePlat
{
    /**
     連接配接微信應用以使用相關功能,此應用需要引用WeChatConnection.framework和微信官方SDK
     http://open.weixin.qq.com上注冊應用,并将相關資訊填寫以下字段
     **/
    [ShareSDK connectWeChatWithAppId:@""
                           appSecret:@""
                           wechatCls:[WXApi class]];
    /**
     連接配接QQ應用以使用相關功能,此應用需要引用QQConnection.framework和QQApi.framework庫
     http://mobile.qq.com/api/上注冊應用,并将相關資訊填寫到以下字段
     **/
    //舊版中申請的AppId(如:QQxxxxxx類型),可以通過下面方法進行初始化
    //    [ShareSDK connectQQWithAppId:@"QQ075BCD15" qqApiCls:[QQApi class]];
    
    [ShareSDK connectQQWithQZoneAppKey:@""
                     qqApiInterfaceCls:[QQApiInterface class]
                       tencentOAuthCls:[TencentOAuth class]];
    
    
    
    
    //
    /**
     連接配接騰訊微網誌開放平台應用以使用相關功能,此應用需要引用TencentWeiboConnection.framework
     http://dev.t.qq.com上注冊騰訊微網誌開放平台應用,并将相關資訊填寫到以下字段
     
     如果需要實作SSO,需要導入libWeiboSDK.a,并引入WBApi.h,将WBApi類型傳入接口
     **/
    [ShareSDK connectTencentWeiboWithAppKey:@""
                                  appSecret:@""
                                redirectUri:@"http://www.sharesdk.cn"
                                   wbApiCls:[WeiboApi class]];
    //
    
    
    
    
    /**
     連接配接QQ空間應用以使用相關功能,此應用需要引用QZoneConnection.framework
     http://connect.qq.com/intro/login/上申請加入QQ登入,并将相關資訊填寫到以下字段
     
     如果需要實作SSO,需要導入TencentOpenAPI.framework,并引入QQApiInterface.h和TencentOAuth.h,将QQApiInterface和TencentOAuth的類型傳入接口
     **/
    //    [ShareSDK connectQZoneWithAppKey:@""
    //                           appSecret:@""
    //                   qqApiInterfaceCls:[QQApiInterface class]
    //                     tencentOAuthCls:[TencentOAuth class]];
    
    
    /**
     連接配接新浪微網誌開放平台應用以使用相關功能,此應用需要引用SinaWeiboConnection.framework
     http://open.weibo.com上注冊新浪微網誌開放平台應用,并将相關資訊填寫到以下字段
     **/
    [ShareSDK connectSinaWeiboWithAppKey:@""
                               appSecret:@""
                             redirectUri:@"http://www.sharesdk.cn"];
    
    
}
           

四,設定URL Scheme

1.需要依賴用戶端分享或者要支援SSO授權(可以了解成跳到用戶端授權)的平台都需要配置平台的URL Scheme(應用分享到社交平台後通過識别URL Scheme傳回應用)。具體配置URL Scheme請參考iOS配置SSO授權

(http://wiki.mob.com/%E9%85%8D%E7%BD%AEsso%E6%8E%88%E6%9D%83-2/)。

iOS 分享功能開發
2.需要在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];
}
           

五,構造分享内容并分享

1.構造一個id<ISSContent>類型的對象,把需要分享的标題、内容、圖檔、URL等 包裝在這個對象。

 在需要分享的viewController 中導入頭檔案

#import <ShareSDK/ShareSDK.h>
           

在需要分享的地方 實作如下方法

比如在點選一個button的時候調用方法 shareAct

-(void)shareAct:(id)sender
{
//    
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"jpg"];
    
    //1、構造分享内容
    id<ISSContent> publishContent = [ShareSDK content:@"要分享的内容"
                                       defaultContent:@"預設内容"
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"ShareSDK"
                                                  url:@"http://www.mob.com"
                                          description:@"這是一條示範資訊"
                                            mediaType:SSPublishContentMediaTypeNews];
    //1+建立彈出菜單容器(iPad必要)
    id<ISSContainer> container = [ShareSDK container];
    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    //2、彈出分享菜單
    [ShareSDK showShareActionSheet:container
                         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)
                                {
                                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                    message:nil
                                                                                   delegate:self
                                                                          cancelButtonTitle:@"OK"
                                                                          otherButtonTitles:nil, nil];
                                    [alert show];
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失敗"
                                                                                    message:[NSString stringWithFormat:@"失敗描述:%@",[error errorDescription]]
                                                                                   delegate:self
                                                                          cancelButtonTitle:@"OK"
                                                                          otherButtonTitles:nil, nil];
                                    [alert show];
                                }
                            }];

}
           

到此基本完成

測試一下 點選微網誌分享 

iOS 分享功能開發
如有問題 可留言,或者QQ群:414319235  歡迎交流學習