2014-10-29 84 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) 
相关问题