How to handle the UIActivityViewController "Cancel" button

914 views Asked by At

There is a "cancel" button that comes by default with UIActivityViewController. How can I call a method when that button is pressed?

I have written this code to share the audio file.

NSURL* outURL;
UIActivityViewController *controller;

if (&UIActivityTypeAirDrop != NULL) {
    controller.excludedActivityTypes = @[UIActivityTypeAirDrop];

    for (int i=0;i<[arrayselecturls count];i++) {
        str_SongsselectName=[[arrayselecturls objectAtIndex:i] objectForKey:@"Song"];

        NSString* ext = [TSLibraryImport extensionForAssetURL:[[arrayselecturls objectAtIndex:i] objectForKey:@"SongURl"]];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        outURL = [[NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:[[arrayselecturls objectAtIndex:i] objectForKey:@"Song"]]] URLByAppendingPathExtension:ext];

        NSString *str_selectfilename=[NSString stringWithFormat:@"%@.mp3",str_SongsselectName];
        //        NSURL *url = outURL;
        //        NSLog(@"url is %@",url);
        NSArray *objectsToShare = [NSArray arrayWithObjects:outURL,str_selectfilename, nil];
        //  objectsToShare = @[outURL];
        controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
        // Exclude all activities except AirDrop.
        NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                        UIActivityTypePostToWeibo,
                                        UIActivityTypeMessage, UIActivityTypeMail,
                                        UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                        UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                        UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                        UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
        controller.excludedActivityTypes = excludedActivities;
    }
    [self presentViewController:controller animated:YES completion:nil];

}
else{
    UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Your device has no Airdrop!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [Alert show];
}
1

There are 1 answers

0
Ravikumar On

oooooop! I got the solution.This solution is working fine for me.

-(IBAction)ActionArtsist_airdropClick:(id)sender{
    view_shareView.hidden=YES;
    borderImageView . hidden =YES;
    darkBg . hidden =YES;
    view_artistdarlView.hidden=YES;

    tblv_albumSongTableView.userInteractionEnabled=YES;
    Data = [NSData dataWithContentsOfURL: exportURL];
     UIActivityViewController *controller;

    if (&UIActivityTypeAirDrop != NULL) {
        controller.excludedActivityTypes = @[UIActivityTypeAirDrop];

    NSString *str_selectfilename;

    for (int i=0;i<[arr_atistsongsSelct count];i++) {
        str_songsSlelectName=[[arr_atistsongsSelct objectAtIndex:i] objectForKey:@"songsName"];

        NSString* ext = [TSLibraryImport extensionForAssetURL:[[arr_atistsongsSelct objectAtIndex:i] objectForKey:@"songsURl"]];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSURL* outURL = [[NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:[[arr_atistsongsSelct objectAtIndex:i] objectForKey:@"songsName"]]] URLByAppendingPathExtension:ext];

        str_selectfilename=[NSString stringWithFormat:@"%@.mp3",str_songsSlelectName];

        NSArray *objectsToShare = [NSArray arrayWithObjects:outURL,str_selectfilename, nil];
       controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
        // Exclude all activities except AirDrop.
        NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                        UIActivityTypePostToWeibo,
                                        UIActivityTypeMessage, UIActivityTypeMail,
                                        UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                        UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                        UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                        UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
        controller.excludedActivityTypes = excludedActivities;
    }

This is the handling function.

        **[controller setCompletionHandler:^(NSString *activityType, BOOL completed) {
            if (!completed)
            {

                [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
                [self performSelector:@selector(ActiviHideArtistSong) withObject:self afterDelay:4.0 ];

            }

            return;
        }];**

upto here

        [self presentViewController:controller animated:YES completion:nil];

}
    else{
        UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Your device has no Airdrop!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [warningAlert show];
    }
}

-(void)ActiviHideArtistSong{
    [self btnimageArtistSongscheck_lick:nil];

}