RN中调用原生的分享功能(UIActivityViewController)

xiaoxiao2021-02-28  7

新建子类,通过继承RCTViewManager的方式,导出一个按钮或者自己自定义的view,点击这个view的时候触发分享方法

如下:

- (UIView *)view

{

  UIButton* button = [UIButton new];

  [button addTarget:self action:@selector(testMethod) forControlEvents:UIControlEventTouchUpInside];

  return button;

 

//  return activityViewController.view;

}

- (void)testMethod {

  NSLog(@"成功点击");

  if ([UIApplication sharedApplication].keyWindow.rootViewController != nil) {

    NSLog(@"有root vc");

    

    NSMutableArray *items = [[NSMutableArray alloc] init];

    [items addObject:@"分享的title"];

    //图片必须存在

    [items addObject:[UIImage imageNamed:@"already_shoucang"]];

    [items addObject:@"https://www.baidu.com"];

    

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];

    

    

    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityViewController animated:true completion:^{

      

    }];

    activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){

      NSLog(@"%@  ----   %@", activityType, returnedItems);

    };

  } else {

    NSLog(@"么有 root vc");

  }

}

转载请注明原文地址: https://www.6miu.com/read-1100331.html

最新回复(0)