如何处理UIActivityViewController“取消”按钮

时间:2014-09-08 14:42:01

标签: objective-c ios7 uiactivityviewcontroller uiactivitytypeairdrop

默认情况下,UIActivityViewController会显示“取消”按钮。如何在按下该按钮时调用方法?

我已经编写了这段代码来共享音频文件。

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 个答案:

答案 0 :(得分:-1)

oooooop! 我得到了解决方案。这个解决方案对我来说很好。

-(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;
    }

这是处理功能。

        **[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;
        }];**

到此为止

        [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];

}