ShareSDK For iOS

repository·master·Indexed 19 days ago

https://github.com/mobclub/sharesdk-for-ios

A social sharing and authentication SDK for iOS providing a unified framework to integrate over 40 social platforms, including WeChat, Facebook, and Twitter. It supports third-party login (OAuth), user information retrieval, and content sharing via a built-in share menu or direct platform integration. The SDK is modular and available via CocoaPods, requiring the mob_sharesdk main module and optional modules for UI, extensions, and specific social platforms.

Tokens
2.6K
Snippets
9
Records
14
Agent score
67%

What's inside ShareSDK For iOS

  1. Overview of ShareSDK For iOS

    master

    ShareSDK is a social sharing component for iOS that provides a unified interface for integrating various Social Networking Service (SNS) features. It is designed to simplify the integration of third-party social platforms.

    Main Features:

    • Authorize: Implement third-party login (OAuth) flows.
    • Get User Info: Retrieve user data such as userid, nickname, etc.
    • Share: Share content across multiple social platforms.

    ShareSDK supports over 40 platforms, including Sina, WeChat, Tencent QQ, Facebook/FB Messenger, Twitter, Google+, and Instagram. You can selectively include only the platforms your application requires.

  2. Configure URL Schemes and LSApplicationQueriesSchemes

    master

    To allow your app to jump to social media apps (like WeChat or Facebook) and return to your app, you must configure two settings in your Info.plist:

    1. LSApplicationQueriesSchemes: Add the schemes for all platforms you intend to support so your app can query if they are installed.
    2. URL Schemes: Set up the specific URL scheme required by each platform to handle the callback.
      • WeChat: Use wx + appId (e.g., wx617c77c82218ea2c).
      • Facebook: Use fb + appKey (e.g., fb1412473428822331).

    Note: Refer to the ShareSDK demo for the full list of required schemes for all supported platforms.

  3. Initialize ShareSDK and configure platforms

    master

    To use ShareSDK, you must first add your MOBAppKey and MOBAppSecret to your project's Info.plist.

    Then, in your AppDelegate.m, import the header and use [ShareSDK registPlatforms:] within application:didFinishLaunchingWithOptions: to configure the specific credentials for each social platform you have installed.

    ```objc
    #import <ShareSDK/ShareSDK.h>
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
            // QQ
            [platformsRegister setupQQWithAppId:@
  4. Install ShareSDK via CocoaPods

    master

    ShareSDK is modular. You must include the required main module, and then add optional modules for UI, specific social platforms, and extensions based on your needs.

    Required:

    • mob_sharesdk (Main module)

    Optional Modules:

    • mob_sharesdk/ShareSDKUI: Required if you want to use the built-in share menu or editor.
    • mob_sharesdk/ShareSDKExtension: Required if you are using the ShareSDKUI module.
    • mob_sharesdk/ShareSDKConfigFile: For configuration file sharing.
    • mob_sharesdk/ShareSDKRestoreScene: For restoring scenes.

    Social Platforms: Add only the specific platforms you intend to support using the mob_sharesdk/ShareSDKPlatforms/[PlatformName] syntax (e.g., QQ, SinaWeibo, WeChat, Facebook, etc.).

    Note: For WeChat, choose either WeChat (standard) or WeChatFull (includes WeChat Pay, but do not use both).

    # Main module (required)
    pod 'mob_sharesdk'
     
    # UI module (optional, required if using share menu)
    pod 'mob_sharesdk/ShareSDKUI'
     
    # Social platform modules (add only what you need)
    pod 'mob_sharesdk/ShareSDKPlatforms/QQ'
    pod 'mob_sharesdk/ShareSDKPlatforms/SinaWeibo'
    pod 'mob_sharesdk/ShareSDKPlatforms/WeChat' # or 'WeChatFull'
    
    # Extension module (required if UI module is used)
    pod 'mob_sharesdk/ShareSDKExtension'
  5. Configure Info.plist for WeChat SDK (iOS 9+)

    master

    To support WeChat capabilities such as sharing, collecting, paying, and logging in on iOS 9 and later, you must whitelist the weixin URL scheme in your project's Info.plist. This allows the app to check if WeChat is installed.

    Additionally, if your app needs to access non-HTTPS resources, you must configure NSAppTransportSecurity to allow arbitrary loads.

    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>weixin</string>
    </array>
    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
  6. Configure Linker Flags and Frameworks for WeChat SDK

    master

    Depending on your SDK version, specific frameworks and linker flags must be configured in your Xcode project settings to ensure successful compilation and runtime behavior.

    For SDK 1.7.4 and later:

    • Link Frameworks: CFNetwork.framework must be linked.
    • Other Linker Flags: Add -Objc and -all_load to your project configuration.

    For SDK 1.6.x:

    • Link Frameworks: CoreTelephony.framework must be linked.

    For SDK 1.6.3:

    • Link Frameworks: Security.framework must be linked in the Build Phases section.

    For SDK 1.5:

    • Link Libraries: SystemConfiguration.framework, libz.dylib, and libsqlite3.0.dylib are required for user statistics features.
  7. Initialize ShareSDK and Platforms

    master

    To use ShareSDK, you must first set your MOBAppkey and MOBAppSecret in your project's Info.plist.

    Then, in your AppDelegate.m, import the header and call [ShareSDK registPlatforms:] within application:didFinishLaunchingWithOptions: to initialize the SDK and the specific social platforms you intend to use.

    ```objc
    #import <ShareSDK/ShareSDK.h>
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
            // Example: QQ
            [platformsRegister setupQQWithAppId:@
  8. Troubleshoot CocoaPods search issues

    master

    If pod search mob_sharesdk returns no results, follow these steps to refresh your local CocoaPods environment:

    1. Update your pod specs: $pod setup
    2. Clean the search index: $rm ~/Library/Caches/CocoaPods/search_index.json
    $pod setup
    $rm ~/Library/Caches/CocoaPods/search_index.json
  9. Share content directly to a specific platform

    master

    If you do not want to use the UI menu, you can share directly to a specific platform (e.g., SSDKPlatformTypeSinaWeibo) using the [ShareSDK share:parameters:onStateChanged:] method. This requires a SSDKPlatformType and a shareParams dictionary.

    ```objc
    #import <ShareSDK/ShareSDK.h>
    
    // 1. Create share params
    NSArray* imageArray = @[[UIImage imageNamed:@
  10. Share content using the Share Menu UI

    master

    To trigger the built-in ShareSDK share menu, follow these steps:

    1. Import both <ShareSDK/ShareSDK.h> and <ShareSDKUI/ShareSDK+SSUI.h>.
    2. Create a shareParams dictionary using SSDKSetupShareParamsByText:images:url:title:type:.
    3. Call [ShareSDK showShareActionSheet:...] passing the parameters and a completion block to handle the SSDKResponseState (Success, Fail, etc.).
    ```objc
    #import <ShareSDK/ShareSDK.h>
    #import <ShareSDKUI/ShareSDK+SSUI.h>
    
    // 1. Create share params
    NSArray* imageArray = @[[UIImage imageNamed:@
  11. Authorize users and retrieve user information

    master

    ShareSDK provides methods for social authentication and fetching user profiles:

    • Authorization: Use [ShareSDK authorize:settings:onStateChanged:] to log a user into a platform or bind their account. The callback provides an SSDKUser object on success.
    • Get User Info: Use [ShareSDK getUserInfo:onStateChanged:] to retrieve profile data for a specific platform. The callback provides an SSDKUser object on success.
    // Auth
    [ShareSDK authorize:platformType
                   settings:setting
             onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
                 // Handle state (Success, Fail, Cancel)
             }];
    
    // Get User Info
    [ShareSDK getUserInfo:platformType
               onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
                   // Handle state
               }];