2013-05-10 21 views
1

我想從電子郵件中打開一個文件(csv)到我的應用程序,解析它並將數據(學生)導入到課程中。我已經通過iTunes和Dropbox中的文件成功地解析並導入了數據,但我正在努力從電子郵件中獲取文件。如何使用OpenURL傳遞URL,然後在保留URL的情況下執行Segue?

具體而言,流量是這樣的: 打開電子郵件>選擇文件與我的應用程序>應用程序啓動打開併成功讀取文件到我的第一個視圖控制器......但這裏是我卡住:

我想要一個模態視圖彈出,以便用戶可以選擇他們想要導入的數據(學生)進入哪個課程。

應用程序委託講述的第一個視圖控制器來處理的網址:

// 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 ***// 
} 

故事板的接線是否正確了,因爲如果我使用NSNotification我可以出現在模式視圖(後來我失去url的值)。看起來似乎沒有機會出現,因此無法處理這種情況。

關於如何讓模態視圖出現並保留_importedFileURL的任何建議?

回答

0

解決:使用NSNotification中心,爲疑似:d

在我的AppDelegate(.M)我添加了以下程序中的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; 
    } 
} 

,然後在我的viewDidLoad MainViewController(.M) ,我添加了添加了一個NSNotification觀察:

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

,然後加入handleOpenURL方法:

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

和segueToImportFile方法:

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

終於SEGUE將在_importedFileURL發送到新TableViewController,列出了課程:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    UINavigationController *navigationController=segue.destinationViewController; 
    SelectCourseTableViewController *selectCourseTableViewController=[[navigationController viewControllers]objectAtIndex:0]; 
    [email protected]"Email Import"; // from the storyboard 
    selectCourseTableViewController.importedFile = _importedFileURL; 
} 
相關問題