UIApplication openURL background

夙愿已清 提交于 2019-12-17 16:29:08

问题


In my iOS 4 application, I need to open a URL from the background, at a time specified from the user. However, for some reason, I cannot launch a URL from the background for some reason. Here is my code for opening a URL:

if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]])
{
     // the URL wasn't opened. we will ignore this for now.
}

That code is all being launched from a daemon thread created earlier. I have tested this code on the simulator, and the URL is not opened, and the method returns YES for some reason, but when I open my application again (via fast-app-switching) it opens the URL. Is there some way that I can make my application come to the foreground again at this point (not via a local notification) so that the URL may open, or is this a bug or undocumented feature. Also, if there is another way to open the URL, that will work in the background, that would be helpful too.


回答1:


I believe I have found it (doesn't work on simulator, because it passes commands to the terminal on the mac, as there is no command line for the simulator). Use this:

system([[NSString stringWithFormat:@"uiopen \"%@\"", myUrlToOpen] UTF8String]);

That will pass the command uiopen (found via jailbreak) to the device, forcing it to open that URL. I hope this helps others in my position.




回答2:


UIKit calls can only run on the main thread. I think that because this is running on a background thread, Safari still visits the URL, but your app is still active. Try opening Safari shortly after and see if it loaded the URL.

The solution would be to put this on the main thread, and thus leave your app at the time that code is run. That doesn't sound like what you want it to do, sorry. If you want to visit the URL from within your app you can present an UIWebView.




回答3:


This isn't supported by iOS. Per the relevant section of Apple's iOS Programming Guide:

Support for some types of background execution must be declared in advance by the application that uses them [via] an array that contains one or more strings with the following values:

  • audio - The application plays audible content to the user while in the background.
  • location - The application keeps users informed of their location, even while running in the background.
  • voip - The application provides the ability for the user to make phone calls using an Internet connection.

...

In addition to the preceding keys, iOS provides two other ways to do work in the background:

  • Applications can ask the system for extra time to complete a given task.
  • Applications can schedule local notifications to be delivered at a predetermined time.

Your use does not meet any of the allowed types of multitasking.



来源:https://stackoverflow.com/questions/4170254/uiapplication-openurl-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!