在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多个视图,这会造成很多重复。
让我知道你是否有任何想法。
感谢
格雷格
presentViewController是UIViewController中相关联的方法,因此你不能运行的应用程序委托此方法。 – ZeMoon 2014-10-29 10:19:22
你所描述的将需要通知。你不能在应用程序之外向用户显示任何警报。 – ZeMoon 2014-10-29 10:20:49
您还会注意到,在调用openUrl委托方法时,应用程序已启动。 – ZeMoon 2014-10-29 10:24:04