In my app I want to download big files. As such, I want to display download progress to the user. For this I implemented the URLSessionDownloadDelegate.
This works great until the app goes to the background. When the user then reopens the app didWriteData is not called. However didFinishDownloadingTo is still called. Hence it is not a problem with the delegate itself.
Some other developers reported similar issues in https://forums.developer.apple.com/message/229215#229215.
However, so far there seem to be no solutions or workarounds to this issue. Did anyone here find any workaround?
If no, what could I use instead of SessionDownloadTask?
This seems to be a bug in iOS12. As a workaround I resume all the download tasks once the application became active again. Like this:
private(set) var session: URLSession?
func applicationDidBecomeActive(_ application: UIApplication) {
session?.getAllTasks(completionHandler: { tasks in
for task in tasks {
task.resume()
}
})
}
Related
I'm using background refresh to schedule local notifications and update my widget.
The problem is that the task registration method isn't called!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.Mahmoud.AdaniLite.AppRefresh", using: nil) { task in
print("Registered")
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
return true
}
I also have a break point inside the register method with a print statement but non of both worked!
I have done my research and so far i have tried the following already:
I made sure 'Permitted background task scheduler identifiers' in the info.plist is filled with the correct task identifier. I have triple checked that this string gets used correctly in the code as well.
Enabled 'Background fetch' capability.
In the info.plist I added "App processes data in the background" to the "Required background modes"
I'm running the app on a real device running iOS 15
Any help or guidance is deeply appreciated!
You’ve only registered your task handler so far. You also need to schedule your background task.
Please see this guide for more information: https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/using_background_tasks_to_update_your_app
It can be a bit unpredictable when your background task will be executed, and might not happen at all exactly when you expect it to.
This outlines how you can debug your task handlers to make sure they’re registered and have correct requests submitted:
https://developer.apple.com/documentation/backgroundtasks/starting_and_terminating_tasks_during_development
I'm building a macOS Catalyst app with support for multiple windows, which is implemented with the new UISceneDelegate set of APIs introduced in iOS 13.
According to Apple's documentation, as a new window is created, a scene delegate needs to connect to UISceneSession, which allows passing information to this session via its userInfo property. One way to set userInfo before the scene delegate is connected is in this function of UISceneDelegate:
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
connectingSceneSession.userInfo?["foo"] = "bar"
return UISceneConfiguration(
name: "Default Configuration",
sessionRole: connectingSceneSession.role
)
}
Looking at the Developer Reference page for this function, it seems it should be always called on app launch before the main app's scene is set up. Unfortunately, it seems with macOS Catalyst this is not true, but I'm not able to find any documentation or logic for why this function is not always called. In my application, which reuses trivial sample code for UISceneDelegate, this function is called randomly about 50% of the time on app launch. This is not great, as it means that randomly 50% of the time app's windows are not properly set up, as required information is not passed in the userInfo property of UISceneSession.
What is the exact logic for calls to application(_:configurationForConnecting:options:) or how to enforce that it is called deterministically on app launch for the first app's scene with macOS Catalyst?
This isn't specific to Catalyst - it happens in iOS too. It's to do with window restoration versus creation. To understand that, see my answer to this more generic question: Why is UIApplicationDelegate method `application(_:configurationForConnecting:options:)` not called reliably
It could not be called even in iOS, not only Mac Catalyst
Once you have configured UISceneSession with particular persistentIdentifier UIKit will not call application(_:configurationForConnecting:options:) for the session with this persistentIdentifier again.
UIKit calls this method shortly before creating a new scene
I am working on accepting a CKShare in a macOS app in Swift 4. I've already done all the following:
Create the CKShare and save it with its rootRecord to CloudKit
Add a participant (CKShare.Participant)
I've confirmed that the CKShare is on the CloudKit server and that the person I invited has access to it. Here's a screenshot: https://d.pr/i/0sMFQq
When I click the share link associated with the CKShare, it opens my app, but nothing happens and userDidAcceptCloudKitShareWith doesn't fire.
func application(_ application: NSApplication, userDidAcceptCloudKitShareWith metadata: CKShareMetadata) {
print("Made it!") //<-- This never gets logged :(
let shareOperation = CKAcceptSharesOperation(shareMetadatas: [metadata])
shareOperation.qualityOfService = .userInteractive
shareOperation.perShareCompletionBlock = {meta, share, error in
print("meta \(meta)\nshare \(share)\nerror \(error)")
}
shareOperation.acceptSharesCompletionBlock = { error in
if let error = error{
print("error in accept share completion \(error)")
}else{
//Send your user to where they need to go in your app
print("successful share:\n\(metadata)")
}
}
CKContainer.default().add(shareOperation)
}
Is there some kind of URL scheme I have to include in my info.plist? Or perhaps a protocol I need to conform to in my NSApplicationDelegate delegate? I can't, for the life of me, figure out what to do. Thanks in advance!
Update
I've tried a few more things on this. When I open the share link in a web browser, I see this:
Clicking OK makes the screen fade away to this:
Not particularly helpful. :) After doing this, the participant's status in CloudKit is still Invited, so the share still hasn't been accepted.
When I click on a share link within Messages, I am shown a popup like this:
After I click open, a new copy of my app shows up in the dock, then the app suddenly closes. The crash log states:
Terminating app due to uncaught exception 'CKException', reason: 'The application is missing required entitlement com.apple.developer.icloud-services'
I've tried turning iCloud off and on again in the Capabilities section of Xcode, but nothing changes. I know this exception can't be right because I can start my app normally and use CloudKit all day long. Only the CKShare causes this crash.
This is a mess. Save me, Obi-wan Kenobi, you're my only hope.
Yes,
You need to add this to your info.plist.
<key>CKSharingSupported</key>
<true/>
** EDITED ANSWER **
I use this code to share, I don't do it manually... not sure if this is an option under OS X I must confess. I am using iOS.
let share = CKShare(rootRecord: record2S!)
share[CKShareTitleKey] = "My Next Share" as CKRecordValue
share.publicPermission = .none
let sharingController = UICloudSharingController(preparationHandler: {(UICloudSharingController, handler:
#escaping (CKShare?, CKContainer?, Error?) -> Void) in
let modifyOp = CKModifyRecordsOperation(recordsToSave:
[record2S!, share], recordIDsToDelete: nil)
modifyOp.savePolicy = .allKeys
modifyOp.modifyRecordsCompletionBlock = { (record, recordID,
error) in
handler(share, CKContainer.default(), error)
}
CKContainer.default().privateCloudDatabase.add(modifyOp)
})
sharingController.availablePermissions = [.allowReadWrite,
.allowPrivate]
sharingController.delegate = self
sharingController.popoverPresentationController?.sourceView = self.view
DispatchQueue.main.async {
self.present(sharingController, animated:true, completion:nil)
}
This presents an activity controller in which you can choose say email and then send a link. You might also want to watch this video, focus on cloudKit JS right at the beginning.
Watch this WWDC video too https://developer.apple.com/videos/play/wwdc2015/710/
It talks about the cloudkit JSON API, using it you can query what has and what hasn't been shared in a terminal window/simple script perhaps. I did the same when using dropbox API a few years back. Hey you can even use the cloudkit JSON API within your code in place of the native calls.
I finally got it to work! I did all of the following:
Deleted my app from ~/Library/Developer/Excode/DerivedData
Made sure I had no other copies of my app archived anywhere on my machine.
Said a prayer.
Rebooted.
Sheesh, that was rough. :)
If your app is a Mac Catalyst app running on any version of macOS Catalina at least up to and including 10.15.4 Beta 1, a UIApplicationDelegate userDidAcceptCloudKitShareWith method will never be invoked.
After some significant debugging, we discovered that the MacCatalyst UIKit doesn’t even have an implementation for userDidAcceptCloudKitShareWithMetadata in its UIApplication delegate. It’s not broken, it’s just not there. So, at least temporarily, our workaround is the following, which seems to work, even if it’s very inelegant:
// Add CloudKit sharing acceptance handling to UINSApplicationDelegate, which is missing it.
#if targetEnvironment(macCatalyst)
extension NSObject {
#objc func application(_ application: NSObject, userDidAcceptCloudKitShareWithMetadata cloudKitShareMetadata: CKShare.Metadata) {
YourClass.acceptCloudKitShare(cloudKitShareMetadata: cloudKitShareMetadata)
}
}
#endif
If you are using a SceneDelegate, implement the delegate callback there, instead of on AppDelegate.
func windowScene(_ windowScene: UIWindowScene, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShare.Metadata) {
// ...
}
You need to create the app delegate for your SwiftUI app using #NSApplicationDelegateAdaptor:
#main
struct Sharing_ServiceApp: App
{
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene
{
WindowGroup
{
ContentView()
.environment(\.managedObjectContext, persistenceController.container.viewContext)
}
}
}
I put that line in and my code instantly started receiving the share requests.
Im in Swift and every-time I call them the delegate func didFailWithError gets called. Im using the same code I use for the facebook intersitial ads in all my apps and it works perfectly except for this new app I created. No ads pop up and I get a message in my console saying:
DiskCookieStorage changing policy from 2 to 0, cookie file:
file:///private/var/mobile/Containers/Data/Application/4E5FA239-208C-4B08-87C6-E4DB1CC3CC76/Library/Cookies/Cookies.binarycookies
This is how I setup the code in my GameViewController:
import UIKit
import SpriteKit
let interstitialFBAD: FBInterstitialAd = FBInterstitialAd(placementID: "2320559454634478_1321860725967683")
class GameViewController: UIViewController, FBInterstitialAdDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene(fileNamed:"GameScene") {
loadFBInterstitialAd()
}
}
//fbAds--------------------------------------------------------------------------------------------------------
func loadFBInterstitialAd() {
interstitialFBAD.delegate = self
interstitialFBAD.load()
print("what")
}
func interstitialAdDidLoad(_ interstitialAd: FBInterstitialAd) {
interstitialFBAD.show(fromRootViewController: self)
print("popup")
}
func interstitialAd(_ interstitialAd: FBInterstitialAd, didFailWithError error: Error) {
print("failed")
}
//fbAds--------------------------------------------------------------------------------------------------------
So from
Im in Swift and every-time I call them the delegate func didFailWithError gets called. Im using the same code I use for the facebook intersitial ads in all my apps and it works perfectly except for this new app I created. No ads pop up and I get a message in my console saying:
This seems like my first solution is not the answer, but it's so obvious I need to point it out nonetheless: from the Facebook docs it seems like there's a couple steps to creating an app that can use the SDK properly (namely creating an app in Facebook and using the proper Info.plist keys, etc).
If that's not what is happening, though, which I imagine it's not, then this could still be due to the application not being properly set up but for a reason other than Facebook SDK.
Without seeing how you are initializing the SDK in your AppDelegate and without confirmation it's not Info.plist keys that are missing for the ads you are attempting to show, it's hard to say what the issue could be here. Have you contacted Facebook support? They'd definitely be able to help here as well. Somewhere off this page I'm sure you can find proper live support as a paying ads customer.
I'm trying to programmatically return to the home screen in an iOS8 App using Swift. I want to continue the application running in the background though. Any ideas on how to do this?
Thanks in advance for the help.
When an app is launched, the system calls the UIApplicationMain function; among its other tasks, this function creates a singleton UIApplication object. Thereafter you access the object by calling the sharedApplication class method.
To exit gracefully (the iOS developer library explicitly warns you not to use exit(0) because this is logged as a crash ) you can use:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
For example, my app exits when the user shakes the device. So, in ViewController.swift:
override func motionEnded(motion: UIEventSubtype,
withEvent event: UIEvent?) {
if motion == .MotionShake{
//Comment: to terminate app, do not use exit(0) bc that is logged as a crash.
UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)
}}
Swift 4:
UIControl().sendAction(#selector(NSXPCConnection.suspend),
to: UIApplication.shared, for: nil)
Edit: It's worth mentioning that in iOS 12 there's a bug that will prevent network connectivity if the app is brought back from background after sending the suspend action.
For that you should use following code
import Darwin
exit(0)
To force your app into the background, you can legally launch another app, such as Safari, via a URL, into the foreground.
See: how to open an URL in Swift3
UIApplication.shared.open() (and the older openURL) are a documented public APIs.
If you set the exits-on-suspend plist key, opening another app via URL will also kill your app. The use of this key is a documented legal use of app plist keys, available for any app to "legally" use.
Also, if your app, for some impolite reason, continues to allocate and dirty large amounts of memory in the background, without responding to memory warnings, the OS will very likely kill it.
How about setting of info.plist?
You can set "Application does not run in background" true in info.plist with editor.
Or Add this lines with code editor.
<key>UIApplicationExitsOnSuspend</key>
<true/>
There is no way to "programmatically return to the home screen" except for crashing, exiting your program or calling unofficial API. Neither is welcome by Apple. Plus the Human Interface Guidelines are also covering this.