How to use OpenURL to pass in a URL and then performSegue while retaining the URL?

StackOverflow https://stackoverflow.com/questions/16474615

  •  21-04-2022
  •  | 
  •  

Question

I am trying to open a file (csv) from an e-mail into my app, parse it and import the data (students) into a course. I have successfully parsed and imported the data via a file in iTunes and Dropbox, but am struggling on getting the file passed in from e-mail.

Specifically, the flow looks like this: Open E-mail > select file to open with my app > app launches and successfully reads the file into my first view controller ... but here is where I get stuck:

I would like a modal view to popup so the user can select which course they want the imported data (students) to go into.

The App Delegate tells the first view controller to handle the URL:

// from the first view controller
-(void) handleOpenURL:(NSURL *)url
{
    _importedFileURL=url; // if I NSLog this, I can see the file successfully makes it in
     [self performSegueWithIdentifier:@"Select Course for Import" sender:self]; // ***FAIL ***//
}

The storyboard is wired up correctly, because if I use an NSNotification I can get the modal view to appear (but then I lose the value for the url). It seems as though the view hasn't gotten a chance to appear and therefore can't handle the segue.

Any suggestions on how I can get the modal view to appear and retain the _importedFileURL?

Was it helpful?

Solution

Solved: using NSNotification Center, as suspected :D

In my AppDelegate(.m) I added the following routine within openURL

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
  ... other code to handle Dropbox imported URLs

  // Check to see if a file has been sent over via another app, like e-mail
  if (url != nil && [url isFileURL])
      {
        MainViewController  *firstViewController = [[MainViewController alloc]init];
        [firstViewController handleOpenURL:url];
        NSURL *fileFromEmail=url;
        // add the url as a dictionary item, so that I can open it with NSNotificationCenter
        NSDictionary *fileInfo=[NSDictionary dictionaryWithObject:fileFromEmail forKey:@"fileFromEmail"];
        // add a notification named "segueListener" and pass on the dictionary object
        [[NSNotificationCenter defaultCenter] postNotificationName:@"segueListener" object:nil userInfo:fileInfo];

        return YES;
    }
}

and then in my MainViewController(.m) in viewDidLoad, I added the added an NSNotification Observer:

- (void)viewDidLoad
{    
    [super viewDidLoad];
    ... 
    // if a notification named "segueListener" is sent out, perform the method: segueToImportFile
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(segueToImportFile:) name:@"segueListener" object:nil];
}

and then added the handleOpenURL method:

-(void) handleOpenURL:(NSURL *)url
{
   // capture the url and store it into _importedFileURL (NSURL)
    _importedFileURL=url;
}

and segueToImportFile method:

-(void)segueToImportFile:(NSNotification *)notification
{
    NSDictionary *file=[notification userInfo]; // get the dictionary object userInfo
    _importedFileURL=[file objectForKey:@"fileFromEmail"]; 
    [self performSegueWithIdentifier:@"Select Course for Import" sender:self];
}

and finally the segue will send on _importedFileURL to the new TableViewController that lists the courses:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UINavigationController *navigationController=segue.destinationViewController;
    SelectCourseTableViewController *selectCourseTableViewController=[[navigationController viewControllers]objectAtIndex:0];
    selectCourseTableViewController.segueFrom=@"Email Import"; // from the storyboard
    selectCourseTableViewController.importedFile = _importedFileURL;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top