I've written an extension to share images to my web-service. The server is receiving the items without problem but something is happening on the server that execution is terminated before the expected state change.
I am trying to figure out how I can display a response from the web-server (like an error) after the post button has been hit in the share extension.
Is this possible? How can it be done?
my didSelectPost:
override func didSelectPost() {
let identifier = NSBundle.mainBundle().bundleIdentifier! + "." + NSUUID().UUIDString
let configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(identifier)
configuration.sharedContainerIdentifier = "group.aa.com"
let session = NSURLSession(configuration: configuration)
let request = urlRequestWithImage(attachedImage, text: contentText)!
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
task.resume()
extensionContext?.completeRequestReturningItems([], completionHandler: nil)
}
In Xcode select the app running which is right between the STOP icon and the device selector. From there, choose the extension and run that in the simulator or on your attached device, now you will see the prints from your code in Xcode
Related
I am working on an application that requires to download a certain number of files to be able to work offline. Obviously, download tasks are preferred to be done with the app in the background. I implemented an URLSession with a background configuration following Apple's documentation available here : https://developer.apple.com/documentation/foundation/url_loading_system/downloading_files_in_the_background. I also followed a tutorial on raywenderlich: https://www.raywenderlich.com/3244963-urlsession-tutorial-getting-started.
Basically, what I've done looks like this (I've made my class a Singleton but I have the same problem either way):
public final class DownloadService: NSObject {
static let shared = DownloadService()
static let identifier = "downloadService"
private var urlSession: URLSession!
var backgroundCompletionHandler: (() -> Void)? // This is attributed in the handleEventsForBackgroundURLSession delegate method in the AppDelegate
private override init() {
super.init()
let config = URLSessionConfiguration.background(withIdentifier: DownloadService.identifier)
config.isDiscretionary = true
urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
}
}
extension DownloadService: URLSessionDelegate {
// Delegate method called when the background session is finished.
public func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
DispatchQueue.main.async {
guard let completionHandler = self.backgroundCompletionHandler else {
Logger.fault("No completion for bg session", category: .network)
return
}
Logger.log("Complete background session", category: .network)
// This must be executed on the main thread
// Executes things such as updating the app preview in recent apps view
completionHandler()
}
}
}
extension DownloadService: URLSessionDownloadDelegate {
// Delegate method called when a download task is finished
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
// Perform
guard let sourceUrl = downloadTask.originalRequest?.url else {
return
}
Logger.log("Received file: %#", sourceUrl.lastPathComponent, category:.network)
// Check and save file
saveFile(originalFileURL: sourceUrl, downloadedTo: location)
}
}
And I start the download using:
/// Download file using a previously created URLSession.
/// - parameter filename: Name of the file.
/// - parameter baseURL: URL where the files are located.
/// - parameter size: Expected filesize in Bytes.
private func download(file filename: String, from baseURL: String, size: Int64) {
guard let url = URL(string: baseURL)?.appendingPathComponent(filename) else { return }
let task = urlSession.downloadTask(with: url)
task.countOfBytesClientExpectsToSend = 0
task.countOfBytesClientExpectsToReceive = size
task.resume()
}
My problem is that everything works fine when the app is in foreground, but whenever I put the app in the background or lock the screen, I have an error saying:
Task <46648342-7D13-4D1F-96A1-FDAE4C1F8475>.<362> finished with error [22] Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"
I have tried playing a bit with the URLSessionConfiguration, specifically the isDiscretionary parameter which is set to false by default, and it seems that setting it to true, as advised by Apple's documentation, even blocks the download from proceeding with the app in the foreground, resulting to the same error 'Invalid argument'.
I wonder if this parameter has anything to do with my problem, or if there's something I've misunderstood?
The exemple on raywenderlich provided above also works the same way, using isDiscretionary seems to make the download fail everytime.
I am using Xcode 11.3.1 with Swift 5 and targeting iOS13.
Let me know if any other information is needed and thank you for your help!
So, I was trying to do it with a simulator. Either by running from Xcode with the debugger, or by installing the app into the simulator (without the debugger since it affects the application lifecycle).
I tried to run it on a real device (iPad), and there's no sign of this error whatsoever! Setting isDiscretionary seems to work as intended so I'm not sure that this parameter was causing the issue on a simulator.
I'm adding an iMessage extension target to my app. The extension is supposed to send a message that has a url attribute. The behaviour I'm expecting when a user touches the message is to open the browser using the url attribute of the message.
I have a button in my messageView which executes this code:
#IBAction func labelButton(_ sender: Any) {
let layout = MSMessageTemplateLayout()
layout.imageTitle = "iMessage Extension"
layout.caption = "Hello world!"
layout.subcaption = "Test sub"
guard let url: URL = URL(string: "https://google.com") else { return }
let message = MSMessage()
message.layout = layout
message.summaryText = "Sent Hello World message"
message.url = url
activeConversation?.insert(message, completionHandler: nil)
}
If I touch the message, it expands the MessageViewController
I have then added this:
override func didSelect(_ message: MSMessage, conversation: MSConversation) {
if let message = conversation.selectedMessage {
// message selected
// Eg. open your app:
self.extensionContext?.open(message.url!, completionHandler: nil)
}
}
And now, when I touch the message, it opens my main app but still not my browser.
I have seen on another post (where I cannot comment, thus I opened this post) that it is impossible to open in Safari but I have a news app which inserts links to articles and allows with a click on the message to open the article in a browser window, while the app is installed.
So, can someone please tell how I can proceed to force opening the link in a browser window?
Thank you very much.
Here is a trick to insert a link in a message. It does not allow to create an object that has an url attribute but just to insert a link directly which will open in the default web browser.
activeConversation?.insertText("https://google.com", completionHandler: nil)
I have published a sample on github showing how to launch a URL from inside an iMessage extension. It just uses a fixed URL but the launching code is what you need.
Copying from my readme
The obvious thing to try is self.extensionContext.open which is documented as Asks the system to open a URL on behalf of the currently running app extension.
That doesn't work. However, you can iterate back up the responder chain to find a suitable handler for the open method (actually the iMessage instance) and invoke open with that object.
This approach works for URLs which will open a local app, like settings for a camera, or for web URLs.
The main code
#IBAction public func onOpenWeb(_ sender: UIButton) {
guard let url = testUrl else {return}
// technique that works rather than self.extensionContext.open
var responder = self as UIResponder?
let handler = { (success:Bool) -> () in
if success {
os_log("Finished opening URL")
} else {
os_log("Failed to open URL")
}
}
let openSel = #selector(UIApplication.open(_:options:completionHandler:))
while (responder != nil){
if responder?.responds(to: openSel ) == true{
// cannot package up multiple args to openSel so we explicitly call it on the iMessage application instance
// found by iterating up the chain
(responder as? UIApplication)?.open(url, completionHandler:handler) // perform(openSel, with: url)
return
}
responder = responder!.next
}
}
I am fairly new to swift(1 week) and iOS programming, and my problem is that I seem to miss some basic understanding. Below you see a function that is triggered by a background notification. I can and have verified that I receive the background notification reliably and the app comes active (printout of the raw data values on the console) As long as the app is in the foreground everything is working just as expected, it gets fired, and sends a single https request. The background triggers come on a timer every minute.
Now the whole thing changes when the app enters into the background. In this case I am still getting the triggers through the notification (console printout) and I can see in the debugger the same function that works like a charm in the foreground stumbles. It still works, it still gets fired, but a data packet is sent only so often, randomly as it seems between 2 and 30 minutes.
let config = URLSessionConfiguration.background(withIdentifier: "org.x.Reporter")
class queryService {
let defaultSession = URLSession(configuration: config)
var dataTask: URLSessionDataTask?
var errorMessage = ""
func getSearchResults(baseURL: String, searchTerm: String) {
dataTask?.cancel()
config.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData;
config.timeoutIntervalForRequest = 10
if var urlComponents = URLComponents(string: "https://host.com/reportPosition.php") {
urlComponents.query = "\(searchTerm)"
guard let url = urlComponents.url else { return }
dataTask = defaultSession.dataTask(with: url)
}
// 7
dataTask?.resume()
}
}
Try using dataTaskWithCompletion so you can see what's going wrong in the error.
URLSession.shared.dataTask(with: URL.init(string: "")!) { (data, response, error) in
if error != nil {
// Error
}
}.resume()
https://developer.apple.com/documentation/foundation/urlsession/1410330-datatask
EDIT
What you want to do is for background you get completions via delegate call backs so when you init ur URLSession do so using the following func
URLSession.init(configuration: URLSessionConfiguration.init(), delegate: self, delegateQueue: OperationQueue.init())
https://developer.apple.com/documentation/foundation/urlsession/1411597-init
Then conform ur class to the URLSessionDelegate like so
class queryService, URLSessionDelegate {
then implement the delegate methods listed here for call backs
https://developer.apple.com/documentation/foundation/urlsessiondelegate
EDIT2
Here is good tutorial about it
https://www.raywenderlich.com/158106/urlsession-tutorial-getting-started
I'm trying to create a new Notification Content Extension in iOS 10 for local notifications where the notification view controller responsible for the content extension downloads an image from the network and renders it in a UIImageView. I have the Notification Content Extension target set up with an appropriate Info.plist and the content extension works beautifully for simple things like rendering a label with some content, for example the sample code that comes in the template:
func didReceive(_ notification: UNNotification) {
self.label.text = notification.request.content.body
}
However, when I try to introduce NSURLSession (or URLSession in Swift 3) into the mix, the notification content totally fails to load - not even the label gets set anymore:
func didReceive(_ notification: UNNotification) {
self.label.text = notification.request.content.body
let session = URLSession.shared()
let url = URL(string: "https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World")!
let task = session.downloadTask(with: url) { (fileURL, response, error) in
if let path = fileURL?.path {
DispatchQueue.main.async {
self.imageView.image = UIImage(contentsOfFile:path)
}
}
}
task.resume()
}
Is the usage of NSURLSession in the notification content extension not allowed? Is my extension possibly being killed before the download completes? If so, how could I ensure that it's not killed so I can download and render the image?
At the point when func didReceive(_ notification: UNNotification) is called in the Content extension, any modifications to the content such as downloading images should have already occurred.
It seems that you use a Notification Service Extension to do the downloading of any additional content. The Notification Content extension is only responsible for providing a custom user interface if you need one.
In your Service extension you download the image using the url in the notification payload, and set it as an attachment on the UNNotification object. If you don't need any custom UI, the system will automatically display visual media attachments like a video or an image. If that suits your needs, you don't actually need a Notification Content Extension at all.
Pusher provide a great tutorial on setting up a Notification Service Extension for handling media attachments in push notifications on iOS 10 right here.
It's actually possible to download images in a Notification Content Extension. However, your code contains two problems:
The URL is not valid.
The fileURL returned by the downloadTask methode is deleted as soon as the function runs out of scope, which is already the case when you try to access the fileURLfrom another thread. Instead it's better to catch the fileURL content in a data variable and use that to produce an image in the main thread
Slightly corrected code:
guard let url = URL(string: "https://betamagic.nl/images/coredatalab_hero_01.jpg") else {
return
}
let task = URLSession.shared.downloadTask(with: url) { (fileURL, response, error) in
if let fileURL = fileURL,
let data = try? Data(contentsOf: fileURL) {
DispatchQueue.main.async {
self.imageView.image = UIImage(data: data)
}
}
}
task.resume()
Disable app transport security in Info.plist for you extension.
Tip: move file from tmp folder to cache for save
I am doing a app in which i need to upload videos taken from iPhone Camera and upload it to a server. When the app is in foreground the video gets uploaded but i don't know how to do it in background when the app is inactive. I used AFNetworking to do the multipart data upload. Here is the code i tried
var task:NSURLSessionUploadTask!
let FILEPICKER_BASE_URL = "My server Url"
var isUploading : Bool = false
var videoPath : String!
func upload(dictMain: NSMutableDictionary)
{
isUploading = true
let uuid = NSUUID().UUIDString + "-" + (getUserId().description)
let request = AFHTTPRequestSerializer().multipartFormRequestWithMethod("POST", URLString: FILEPICKER_BASE_URL, parameters: nil, constructingBodyWithBlock: { (fromData) in
do {
try fromData.appendPartWithFileURL(NSURL(fileURLWithPath: videoPath) , name: "fileUpload", fileName: uuid, mimeType: "video/quicktime")
}catch{
print(error)
}
}, error: nil)
let manager:AFURLSessionManager = AFURLSessionManager(sessionConfiguration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("Your video is uploading"));
task = manager.uploadTaskWithStreamedRequest(request, progress: nil , completionHandler: { (response, responseObject, error) in
NSLog("Resposne Object: \(responseObject)")
post(dictMain,response: responseObject!)
})
task.resume()
}
Also I don't know how to know my upload progress. I tried to use the following block in the progress parameter
{ (progress) in
print("\((progress))")
}
But it does not work the complier shows error
Cannot convert value of type '(_) -> _' to expected argument type 'AutoreleasingUnsafeMutablePointer<NSProgress?>' (aka 'AutoreleasingUnsafeMutablePointer<Optional<NSProgress>>')
Could any one share a snippet that really works. As i googled there are very very few basic tutorials on NSURLSession.uploadTaskWithStreamedRequest and all of them i tried didn't work on swift 2.2
I am using Xcode 7.3 Swift 2.2
Points I Know:
Background upload works only with File paths and not NSData. so i took the video url path from the UIImagePickerController function func video(videoPath: NSString, didFinishSavingWithError error: NSError?, contextInfo info: AnyObject)
To do background file transfer i have to enable them in the Target->Capabilities -> Background Modes -> Background fetch
Must set the Session manager with NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(""). But i don't know why and where that identifier is being used.
I know this is a long post. But if it is answered this will surely be helpful to many developers.
I fixed it by calling the upload function inside a DispatchQueue Background queue
DispatchQueue.global(qos: .background).async { upload() }