Search code examples
iosuialertviewuiactionsheet

iOS: Asking the user to confirm through ActionSheet before launching a dispatch Queue Request


I am still new to iOS, and in an app i am developing, there is this view that shows up when the user imports a file from external source. I would like to have the import executed only after the user confirms that in an action sheet, the hiearchy is as follows:

  • User choses from his email to open the file in the app.

  • He is then, switched to a progressView(which programatically becomes the root view during the importing process).

  • The process finishes and the default rootview is set back.

What I want is to ask if the user really wants to import as soon as the progressView Shows, and if not he has to cancel it(I do not know how to do it)

Thank you,

Here is the function:

- (void)handleImportURL:(NSURL *)url
{



    __block JASidePanelController * controller = (JASidePanelController *)self.window.rootViewController;

    // Show progress window
    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    __block PVProgressViewController * progressController = [storyboard instantiateViewControllerWithIdentifier:@"kProgressViewController"];


    self.window.rootViewController = progressController;
     // I think here somethings needs to be done with UIActionsheet       
    // Perform import operation
    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSError *outError;
        NSString * csvString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&outError];
        NSArray * array = [csvString csvRows];
        [[PVDatabaseController sharedController] importArray:array progressHandler:^(float progress) {
            progressController.progressBar.progress = progress;
        }];

        dispatch_async(dispatch_get_main_queue(), ^{
            self.window.rootViewController = controller;
        });
    });
}

Update: So here is what I tried to do with action sheet:

//First the function that calls the handleImport
    -(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
        // Handle CSV Import
        if (url != nil && [url isFileURL]) {
            [self handleImportURL:url];
            [self confirmImportAlert];

        }
        return YES;
    }

//To show the action sheet

- (void)confirmImportAlert {
    UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:@"Proceed through Import?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Yes", @"Maybe", nil];
    [myActionSheet showInView: self.window];
}

// what to do 
    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
        if(buttonIndex == 0){
            // Avoid Import
        }
        else{
            [self handleImportURL:_importURL];

            //initiate import
        }
    }

UPDATE 2: So I added and changed two methods(I cant seem to call the ActionSheetWilldismiss function in app delegate):

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSString *choice = [ actionSheet buttonTitleAtIndex:buttonIndex];
    if(buttonIndex == 0){
        //initiate import
        [self handleImportURL:_importURL];
    }
    else{
        //don't initiate import
    }
}

//This methods creats the action sheet
    - (void)confirmImportAlert {
        // importActionSheet is a property in the appdelegate.h

        if (!self.importActionSheet){
            UIActionSheet *myActionSheet = [[UIActionSheet alloc] initWithTitle:@"Proceed through Import?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Yes", nil];
            [myActionSheet showInView: self.window];
            myActionSheet.delegate = self;

            self.importActionSheet = myActionSheet;
        }


    }

And changed the function that calls import to this:

-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    // Handle CSV Import
    if (url != nil && [url isFileURL]) {

        [self confirmImportAlert];
        //[self handleImportURL:url];



    }
    return YES;
}

Solution

  • Whatever action did trigger the call to -handleImportURL: should instead creat a UIActionSheet and show it.

    If it was a button for example :

    -(void)buttonTapped:(UIButton *)sender
    {
        // [self handleImportURL:someURL];
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                 delegate:self
                                                        cancelButtonTitle:@"Cancel"
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:@"Confirm",nil];
        [actionSheet showInView:self.view];
    }
    

    Then you need to implement the delegate method:

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (actionSheet.cancelButtonIndex != buttonIndex) {
            [self handleImportURL:someURL];
        }
    }