简体   繁体   中英

Present view controller is not showing

When user click on share button i want to show presentViewController for share data into whatsapp or other some apps. At the same time i want to kill that 2nd viewcontrlooer with in 5 sec.

If user click on share button or not 2nd ViewController will be killed after 5 seconds. if user click on share button i want to show presentViewController and at the same time i want to kill 2nd ViewController. how it is possible?

I tried below way but it is not working.

I implemented Share method in 1st Viewcontroller, when user clicks on share button in 2nd ViewController i am calling share method in 1st Viewcontroller still it is not showing presentViewController

below is the share method in 1st Viewcontroller

    -(void)share:(NSString *)code :(NSString *)brandname :(NSString *)clickid :(NSString *)url{

    NSString *textToShare =[NSString stringWithFormat:@"%@%@%@",@"Install and register ",brandname,@" app  here" ];
    NSString *subText = [NSString stringWithFormat:@"%@%@%@%@%@",@"it's awesome, plus ad me will reward you '",code,@"' on ad me to redeem reward \n\n",@"No ad me? Get ad me \n",@"Link: https://e2by6.app.goo.gl/0RKn" ];
    NSString *longd =[NSString stringWithFormat:@"%@%@",url,clickid];

    //NSURL *long_link=[NSURL URLWithString:longd];

    NSArray *objectsToShare = @[textToShare, longd,  subText];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                                   UIActivityTypePrint,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo];

    activityVC.excludedActivityTypes = excludeActivities;

    [self presentViewController:activityVC animated:YES completion:nil];
}

Is their any way to do this job?

Thank you.

I am using this code,

[activityVC setCompletionWithItemsHandler:^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            //dismiss your viewcontroller here
        }else{
          //dismiss your viewcontroller here
        }
    }];

Don't call viewcontroller dismiss method when you click on share button, That time you will get presentViewController. Implement Compilation block for ActivityViewContoller that compilation block will execute when you complete share data into whatsapp or gmail etc. it returns boolean value share is success or not. In that block you should call viewcontroller dismiss method.

Your code becomes,

   -(void)share:(NSString *)code :(NSString *)brandname :(NSString *)clickid :(NSString *)url{

    NSString *textToShare =[NSString stringWithFormat:@"%@%@%@",@"Install and register ",brandname,@" app  here" ];
    NSString *subText = [NSString stringWithFormat:@"%@%@%@%@%@",@"it's awesome, plus ad me will reward you '",code,@"' on ad me to redeem reward \n\n",@"No ad me? Get ad me \n",@"Link: https://e2by6.app.goo.gl/0RKn" ];
    NSString *longd =[NSString stringWithFormat:@"%@%@",url,clickid];

    //NSURL *long_link=[NSURL URLWithString:longd];

    NSArray *objectsToShare = @[textToShare, longd,  subText];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                                   UIActivityTypePrint,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo];

    activityVC.excludedActivityTypes = excludeActivities;

[activityVC setCompletionWithItemsHandler:^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
        if (completed) {
            //dismiss your viewcontroller here
        }else{
          //dismiss your viewcontroller here
        }
    }];

    [self presentViewController:activityVC animated:YES completion:nil];
}

I hope it will helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM