WatchKit call webview - ios

I'm currently working on a Apple Watch app and I'm facing a problem.
Since the app company I'm working on doesn't have a API, and is not planning to make, I use a UIWebView to parse some HTML into the app.
I'm trying to port this to the Apple Watch, but I cannot open a UIWebView in background using the handleWatchKitExtensionRequest delegate.
Is there any way to run a UIWebView background task and send some data from it to the Apple Watch app?

I'm not aware of any method to run a UIWebView in the background. I think you'll have to use something like NSURLSession to download your HTML. Hopefully, you can reuse your parsing logic as-is.

Related

What's the best way to create a custom sharing method on iOS?

A streaming app that I'm using on iOS has a share button that allows me to hook into the sharing app extension. I'd like to be able to send the URL string that it has to a custom API on my server.
Would I need to create an entire app just to have that custom sharing method, or is there another way?
If that's the only way, how can I get that app on my phone without going through the store?
It seems like when I create, build and install apps through Xcode, Apple intentionally breaks the app after a short while?

UIApplication.sharedApplication().beginIgnoringInteractionEvents() on Apple Watch

I can not use
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
in WatchKit Extension, I have error:
'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.
Is there some alternative ?
The short answer is: No there isn't.
The long answer:
Please keep in mind that the extension is not executed on the watch but on your phone. So if you would call UIApplication.sharedApplication() it would return you the application of the extension on your phone, anyway! Everything you do inside your extension is stuff that manipulates the extension on your phone. The only exception from this are the WatchKit methods. And even they are basically calls that are converted into instructions that are send over bluetooth to tell the watch what to do. At no time you can write code that executes on the watch!
You have no control what so ever about what the watch does with the instructions you send to it. You are basically acting as a server talking to a client and you have no control over the client. You should send as little instructions as possible and once you send them, your task is done, the rest is up to the watch.
That being said, you should carefully plan your UI in a way that you do not need any calls that manipulate the event delivery. You should focus on simple 'if user taps x I do y' interaction.
Another thing to keep in mind is, that your extension can not communicate with your main iOS app. You can create a shared app group between your iOS app and your watch extension to share data between them, however you can not directly communicate with your app. If you want to use parts of your apps logic, extract the module in question into a framework (this has become very easy with Xcode 6) and use the framework in both, your app and your extension.

Access code and data in hosting app using today extension

I'm wondering if it's at all possible to access features in the main app using a today extension in iOS 8. For example, can a media player send commands from its extension to the main app without opening the app (I know there is a framework for this but it's just an example)? The only solution I can see is using URIs but the problem is that it will open the app which isn't the behavior I'm looking for.
So for a complete example using the media player:
There is an extension in the today screen that allows you to play/pause using a button.
The user presses the button and the app plays/pauses in the background without leaving the notification center.
Any way to achieve this behavior?
Code can be shared using an embedded framework, however there are some backwards compatibility issues.
Data can be shared using shared user defaults.
Both are explained in the apple docs.

Call Javascript function in background task in iOS

I have a phonegap app and I want to execute Javascript code in a background task. When the application enters background I start executing a javascript function which takes data out of a web database and sends some values to a web service, the returning data may toggle a push-notification to the user.
I found either how to call javascript from Objective-C here on Stackoverflow and how to implement Long-Running Background Tasks in the iOS SDK Documentation pages, but I haven't found anything in combining both (what I think is e.g.: When I invoke javascript code via a webview, does this View even exist when the App is in background?) so I want to ask some more experienced iOS Developers if this is even possible.
I don't think its really possible. Javascript requires a UIwebview to run (in realtime) and must be visible to the user. When an app is put in background only a small amount of tasks are allowed to run- webview not one of them.
Your better off sending your data to a web server that runs a cron-job or similar to send out a push notification via apples push notification server - You can also bypass this and use a service like http://urbanairship.com/
I experimented with this on iOS 5+ and it's possible. I created a small PhoneGap plugin that demonstrates how this works. Check it out, and see the README for most of the info.
https://github.com/jocull/phonegap-backgroundjs

How to use Apple (in app purchases) in UIWebView?

Is there any way to use Apple In App purchases from a UIWebView inside the application ?
Since i want to use a web view to load an external website and use in app purchases as payement method inside the app.
You can call Objective-C methods from UIWebView. This you can do by trapping your custom urls in the shouldStartLoadWithRequest: method. As well, you can call JS methods in HTML from Objective-C code (stringByEvaluatingJavaScriptFromString:). Details are here.
I think it is possible to use Apple's InApp purchase from inside UIWebView, though it depends on your exact requirements. As long as you use Apple's payment gateway (so that they get their 30% cut :)) it's okay.
I don't think it works that way. It's a Cocoa touch API and has to be called from a Cocoa object / controller.
It also uses delegates to communicate back with your app.
Also sounds like what you are trying to do is use the in app purchase as your payment system for a web app. Might be outside the terms of service of AppStore.
Try Google In App Payments?

Resources