美文网首页
facebook视频分享

facebook视频分享

作者: 太平洋_cfd2 | 来源:发表于2024-09-01 11:48 被阅读0次
//
//  GenericShare.m
//  RNShare
//
//  Created by Diseño Uno BBCL on 23-07-16.
//  Copyright © 2016 Facebook. All rights reserved.
//

#import "GenericShare.h"

@implementation GenericShare
    RCT_EXPORT_MODULE();
- (void)shareSingle:(NSDictionary *)options
    reject:(RCTPromiseRejectBlock)reject
    resolve:(RCTPromiseResolveBlock)resolve
    serviceType:(NSString*)serviceType
    inAppBaseUrl:(NSString *)inAppBaseUrl {

    NSLog(@"Try open view");
    if([serviceType isEqualToString:@"com.apple.social.twitter"]) {
        SLComposeViewController *composeController = [SLComposeViewController  composeViewControllerForServiceType:serviceType];

        NSURL *URL = [RCTConvert NSURL:options[@"url"]];
        if (URL) {
            if (URL.fileURL || [URL.scheme.lowercaseString isEqualToString:@"data"]) {
                NSError *error;
                NSData *data = [NSData dataWithContentsOfURL:URL
                                                     options:(NSDataReadingOptions)0
                                                       error:&error];
                if (!data) {
                    reject(@"no data",@"no data",error);
                    return;
                }
                UIImage *image = [UIImage imageWithData: data];
                [composeController addImage:image];

            } else {
                [composeController addURL:URL];
            }
        }

        if ([options objectForKey:@"message"] && [options objectForKey:@"message"] != [NSNull null]) {
            NSString *text = [RCTConvert NSString:options[@"message"]];
            [composeController setInitialText:text];
        }


        UIViewController *ctrl = RCTPresentedViewController();
        [ctrl presentViewController:composeController animated:YES completion:Nil];
        resolve(@[@true, @""]);
      } else {
        NSString *errorMessage = @"Not installed";
        NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)};
        NSError *error = [NSError errorWithDomain:@"com.rnshare" code:1 userInfo:userInfo];

        NSLog(@"%@", errorMessage);
          reject(@"com.rnshare",@"Not installed",error);

        NSString *escapedString = [options[@"message"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];

        if ([options[@"social"] isEqualToString:@"twitter"]) {
            
            return;
//          NSString *URL = [NSString stringWithFormat:@"https://twitter.com/intent/tweet?text=%@&url=%@", escapedString, options[@"url"]];
//          [self openScheme:URL];
        }

        if ([options[@"social"] isEqualToString:@"facebook"]) {
//          NSString *URL = [NSString stringWithFormat:@"https://www.facebook.com/sharer/sharer.php?u=%@", options[@"url"]];
            NSURL *urlScheme = [NSURL URLWithString:@"facebook-reels://share"];
              if ([[UIApplication sharedApplication] canOpenURL:urlScheme])
              {
                  NSString *backgroundVideoString = options[@"backgroundVideo"];
                  NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
                  NSString *videoFilePath = [cachesDirectory stringByAppendingPathComponent:backgroundVideoString];
                  NSURL *backgroundVideoURL = [NSURL fileURLWithPath:videoFilePath];
                    NSData *const backgroundVideoData = [NSData dataWithContentsOfURL:backgroundVideoURL];
                  NSString *appID = options[@"appID"];
                  
                // Add background video to pasteboard items
                NSArray *pasteboardItems = @[@{@"com.facebook.sharedSticker.backgroundVideo" : backgroundVideoData,
                                               @"com.facebook.sharedSticker.appID" : appID}];

                // Set pasteboard options
                NSDictionary *pasteboardOptions = @{UIPasteboardOptionExpirationDate : [[NSDate date] dateByAddingTimeInterval:60 * 5]};

                // Attach the pasteboard items
                // This call is iOS 10+, can use 'setItems' depending on what versions you support
                [[UIPasteboard generalPasteboard] setItems:pasteboardItems options:pasteboardOptions];
                    
                [[UIApplication sharedApplication] openURL:urlScheme options:@{} completionHandler:nil];
              }
              else
              {
                 // Handle error cases
              }
//          [self openScheme:URL];
        }

      }
  }
  - (void)openScheme:(NSString *)scheme {
      UIApplication *application = [UIApplication sharedApplication];
      NSURL *schemeURL = [NSURL URLWithString:scheme];

      if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
          if (@available(iOS 10.0, *)) {
              [application openURL:schemeURL options:@{} completionHandler:nil];
          }
          NSLog(@"Open %@", schemeURL);
      }

  }

  @end

相关文章

网友评论

      本文标题:facebook视频分享

      本文链接:https://www.haomeiwen.com/subject/wfscljtx.html