2014-10-29 86 views
38

在obj-C中,當另一個iOS應用程序(郵件附件,Web鏈接)被與我的應用程序關聯的文件或鏈接點擊時。然後,我會在openURL或didFinishLaunchingWithOptions上捕獲這個數據,並顯示UIAlertView以確認用戶想要導入數據。現在UIAlertView已貶值,我正在嘗試做同樣的事情,但不確定要做到這一點的最佳方式?Simple App Delegate方法顯示UIAlertController(在Swift中)

我無法在我的應用程序從另一個應用程序接收數據時顯示簡單的警報。此代碼在Objective-C運行良好與UIAlertView

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    if (url) 
    { 
     self.URLString = [url absoluteString]; 
     NSString *message = @"Received a data exchange request. Would you like to import it?"; 
     importAlert = [[UIAlertView alloc] initWithTitle:@"Data Received" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 
     [importAlert show]; 
    } 

    return YES; 
} 

但是,當我嘗試切換到UIAlertViewController和斯威夫特我似乎無法找到一個簡單的方法來顯示消息:

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool { 
    let URLString: String = url.absoluteString! 
    let message: String = "Received data. Would you like to import it?" 

    var importAlert: UIAlertController = UIAlertController(title: "Data Received", message: message, preferredStyle: UIAlertControllerStyle.Alert) 
    importAlert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
    importAlert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: 
    { action in 
     switch action.style { 
     case .Default: 
      println("default") 
     case .Cancel: 
      println("cancel") 
     case .Destructive: 
      println("destructive") 
     } 
    })) 

    self.presentViewController(importAlert, animated: true, completion: nil) 
    return true 
} 

我得到一個編譯時錯誤AppDelegate沒有名爲presentViewController

成員,我已經看到了一些令人費解的方法來獲取AppDelegate顯示S上一個UIAlertViewController tackOverflow,但我希望有一些簡單的東西。

我真正需要做的就是向用戶顯示一條快速消息,告訴他們他們獲得了一些數據,並讓他們決定他們想要做什麼。一旦完成,我的應用程序將繼續打開並進入前臺(類似於didFinishLaunchingWithOptions中用於冷啓動的代碼),並根據警報選擇添加或不添加新數據。

我可以標記一個全局變量,我檢查了我所有的viewWillAppear函數,但是由於我有30多個視圖,這會造成很多重複。

讓我知道你是否有任何想法。

感謝

格雷格

+0

presentViewController是UIViewController中相關聯的方法,因此你不能運行的應用程序委託此方法。 – ZeMoon 2014-10-29 10:19:22

+0

你所描述的將需要通知。你不能在應用程序之外向用戶顯示任何警報。 – ZeMoon 2014-10-29 10:20:49

+0

您還會注意到,在調用openUrl委託方法時,應用程序已啓動。 – ZeMoon 2014-10-29 10:24:04

回答

91

使用

self.window?.rootViewController?.presentViewController(importAlert, animated: true, completion: nil) 

所有你需要的是一個viewController對象從目前的AlertController嘗試。

在斯威夫特4:

self.window?.rootViewController?.present(importAlert, animated: true, completion: nil) 
+0

多好的回答,希望我能想到它。 ;-) – 2014-10-29 11:44:06

+1

哈哈..右後衛在雅 – ZeMoon 2014-10-29 11:46:22

+0

謝謝你,工作酷似我需要它,只有MOD是:self.window .rootViewController .presentViewController?(importAlert,動畫:真,完成:無) – 2014-10-30 01:44:00

1

從應用程序的委託裏面。

window.rootViweController.presentViewController..

+0

哈哈...我們發佈了同樣的解決方案間隔2秒! – ZeMoon 2014-10-29 11:41:27

20

使用此代碼來的appdelegate

dispatch_async(dispatch_get_main_queue(), { 
      let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet) 
     self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil) 
    }) 

希望這有助於推出alertCV!

+3

這裏最好的答案,恕我直言。由於:警告:'試圖在上呈現,其視圖不在窗口層次結構中! =>太快了,所以'dispatch_async(dispatch_get_main_queue()...'幫助我們在正確的時間出現在合適的地方! – 2015-10-08 00:32:24

+0

在我的情況,我沒有得到有關窗口層次結構的任何消息......但我堅持的viewController在沒有出現謝謝,夥計們 – 2015-12-22 11:17:48

+0

此方法效果然而,僅僅有'self.window .rootViewController .presentViewController(importAlert,動畫:真,完成:無)。?''中的方法didFinishLaunchingWithOptions'並沒有對我的工作很可能因爲視圖控制器不是在它被調用的時候創建的? – 2016-07-03 07:23:42

2

我發現最好的方法是:

 let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet) //.Alert .ActionSheet 
     var hostVC = UIApplication.sharedApplication().keyWindow?.rootViewController 
     while let next = hostVC?.presentedViewController { 
      hostVC = next 
     } 
     hostVC?.presentViewController(importantAlert, animated: true, completion: nil) 


     let delay = 1.5 * Double(NSEC_PER_SEC) 
     let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay)) 
     dispatch_after(time, dispatch_get_main_queue()) { 

      importantAlert.dismissViewControllerAnimated(true, completion: nil) 

      return 
     } 

我添加了一個計時器,這樣的UIAlertController被駁回,因爲它不具有任何按鈕。

感謝:如何從AppDelegate中呈現UIAlertController https://stackoverflow.com/a/33128884/6144027輝煌的答案。

0

斯威夫特3接受的答案的情況下,它可以幫助任何人:

self.window?.rootViewController?.present(importAlert, animated: true, completion: nil) 
相關問題