Twitter sharing and authorization (iOS)

Keywords: iOS network xcode

Preparation
Register for appkey
Integrated sharesdk (download address)
Xcode configuration: urlScheme is the registered appkey, white list: douyinsharesdk, douyinopensdk
Business code
Initialization

import <ShareSDK/ShareSDK.h>

[ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {

//Jitter
[platformsRegister setupDouyinByAppKey:@"app_key" appSecret:@"app_secret"];

}];
share
Can share pictures, album pictures, single video, multiple videos

Sharing pictures

//General parameter settings ---- photo sharing can use album address, Sandbox path and network photo address
NSString *imageURL = @"http://img.hb.aicdn.com/28a4962c297205e0868cdb45bb527e2bc5319f08f019-l7N1A3_fw658";
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters SSDKSetupShareParamsByText:nil

                            images:@[imageURL]
                               url:nil
                             title:nil
                              type:SSDKContentTypeImage];
                              

[ShareSDK share:SSDKPlatformTypeDouyin parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary userData, SSDKContentEntity contentEntity, NSError *error) {

if (state == SSDKResponseStateSuccess) {
    NSLog(@"Success!");
}else{
    NSLog(@"%@",error);
}

}];
Share video

//General parameter settings ---- video sharing can use album address and sandbox path, and does not support network video. If you use network video, please download it to sandbox directory or album first

NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"mp4"];
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters SSDKSetupShareParamsByText:nil
                                images:nil
                                   url:[NSURL URLWithString:videoPath]
                                 title:nil
                                  type:SSDKContentTypeVideo];
                                  
[ShareSDK share:SSDKPlatformTypeDouyin parameters:parameters onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
    if (state == SSDKResponseStateSuccess) {
        NSLog(@"Success!");
    }else{
        NSLog(@"%@",error);
    }
}];

Share multiple videos

//Platform Customization: only albums can be used and the local identifier can be used

__block NSMutableArray *assetLocalIds = [NSMutableArray array];
__weak typeof(self) weakSelf = self;

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    
    NSURL *url_1 = [[NSBundle mainBundle] URLForResource:@"cat" withExtension:@"mp4"];
    NSURL *url_2 = [[NSBundle mainBundle] URLForResource:@"cat" withExtension:@"mp4"];
    
    PHAssetChangeRequest *req_1 = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url_1];
    NSString *localId_1 = req_1.placeholderForCreatedAsset.localIdentifier;
    [assetLocalIds addObject:localId_1];
    
    PHAssetChangeRequest *req_2 = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url_2];
    NSString *localId_2 = req_2.placeholderForCreatedAsset.localIdentifier;
    [assetLocalIds addObject:localId_2];
    
} completionHandler:^(BOOL success, NSError * _Nullable error) {
    if (success) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            [parameters SSDKSetupDouyinParamesByAssetLocalIds:assetLocalIds type:SSDKContentTypeVideo];
            
            [ShareSDK share:SSDKPlatformTypeDouyin parameters:parameters onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                if (state == SSDKResponseStateSuccess) {
                    NSLog(@"Success!");
                }else{
                    NSLog(@"%@",error);
                }
            }];
        });
    }
}];

To grant authorization
[ShareSDK authorize:SSDKPlatformTypeDouyin settings:nil onStateChanged:^(SSDKResponseState state, SSDKUser user, NSError error) {

    if (state == SSDKResponseStateSuccess)
    {
        NSLog(@"%@",[user.credential rawData]);
        NSLog(@"%@",user.rawData);
       
    }
    else
    {
        NSLog(@"%@",error);
    }
}];

Posted by avillanu on Sun, 10 Nov 2019 11:09:43 -0800