How to share Parse login session with iOS extension? - ios

When writing an iOS extension for an app, is it possible to share the app's current Parse login session with the iOS extension?
iOS extensions are bundled with iOS apps but run in separate containers. So when the user logged in on the app, the extension cannot access that Parse instance. If the extension logged in again, it would create an additional login session for the user.
However the extension can share a common data container with the app. So I wonder if it is possible to store the PFSession.sessionToken in the shared container and let the extension communicate with Parse Server based on that existing session without having to login?

There are two possible solutions:
A) The elegant approach
Enable local data sharing which shares persistent data between the main iOS app and its extensions.
// Enable data sharing in main app.
Parse.enableDataSharingWithApplicationGroupIdentifier("...")
// Enable data sharing in app extensions.
Parse.enableDataSharingWithApplicationGroupIdentifier("...", containingApplicaiton: "...")
Local data sharing in Parse SDKs allows you do share persistent local
data between your main application and extensions that it contains,
including Keyboard, Share/Today/Photo/Action extensions and Document
Providers.
As described in the docs.
B) The manual approach
After login in the app, store the Parse session token PFUser.current().sessionToken in an ecrypted, shared data container.
The extension can then access the session token and continue the session with PFUser.become(sessionToken:).

Related

Can I perform a task in Main iOS app from the app Extension?

I have an app extension for Sharing (Share Extension) and when a user selects my app to share an image, my app will send that image to a server.
The problem is that the server requires an authenticated identity (AWS Cognito) to send the object to the server. Since I cannot share Authentication from my main app to my extension and I don't want to have the user sign in every time they want to share, I'm stuck.
I can see this being done with messaging apps where a user sends a message from a share extension. I'm not sure how they achieve this. Since the user is not asked to login again in the extension, somehow the credentials are either being shared with the extension or the app is momentarily launched to perform that upload while remaining in the background (not sure this is possible).
So my question is what is the approach I should be using. Should the extension somehow be directing the main app to upload the image or should I figure a way of sharing the access tokens with the extension in a secure way and accessing them without any user action?
The solution is to setup a shared container for the app & the extension, please see "Sharing data with your containing app" section in this article: https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#:~:text=To%20enable%20data%20sharing%2C%20use,App%20to%20an%20App%20Group.

Authenticating a Today Widget for API access

I need to add a Today Widget/extension to an existing iOS app, which requires the user to sign in via a WKWebView.
As the extension only communicates with Apple frameworks or via openURL() back to the main app, how would I best share the authentication token stored in the main app with the Today Widget/extension, so that it can make authenticated API calls?
Can this be done with a shared keychain? Or is there another secure alternative?
It is not feasible to have the user sign in again in the Today extension UI.
You can use App Groups to share data between the main application and today widget extension. You need to activate App Group for your application and create:
UserDefaults.init(suiteName: "group.com.yourOrganization.yourApplicationName")
In the main application set your data in this UserDefaults and try to access it from within your Today Widget extension.
Use a Shared Keychain Group to store the token, as described at 14m00s in this WWDC video.

Multiple iOS apps with single database and single log on

I created multiple apps in android with single database (Content provider) , single log on (which means if i login in one app it should work for all , if I logout from one app it should logout from all other apps) , I have to open one app from another app and every day I have to update all my off line data to server (it should happen particular time on every day ) . Now I am going to port this system (all apps) to iOS , is it possible in iPhone ? And I am not going to submit this apps to app store.
Yes it is possible. You would need to save your database at container url which you can get using:
- (NSURL *)containerURLForSecurityApplicationGroupIdentifier:(NSString *)groupIdentifier;
The groupIdentifier param needs to be same for all the apps and needs to be configured inside Target->Capabilities->App Groups.
Once you have configured same appGroup for each application, you can use above method to save database at the location, provided by the method. That way the same DB would be accessible to all the apps.
There is NSUserDefaults init methid which takes suitename(appGroup), and creates a shared UserDefaults, which can be used for your single log-on purpose.
- (instancetype)initWithSuiteName:(NSString *)suitename;
Refer:
containerURLForSecurityApplication
NSUserDefaults
You can use AppGroups to share files between your apps. Using AppGroups you can implement a Single Sing-On functionality by storing a Bool in your database that each of your apps have access to that indicates whether the user is currently signed in or not and act accordingly when opening one of your apps.

How to access current firebase user from iOS Today Extension?

We're building a today widget on top of our iOS app which is working with Firebase. However we're struggling to access current signed-in user from the extension. The user at the main app is anonymous so passing credentials via shared container is not an option.
The only way that I found is passing uid and generate custom token to sign in but I was hoping to find out better way to share FIRUser between my main app and app extension. What would be the best way to achieve this?
You can share data between Host App and App Extension only using App Group.
There is no way of direct communication between your Host App and App Extension.
Even though an app extension bundle is nested within its containing
app’s bundle, the running app extension and containing app have no
direct access to each other’s containers.
App Group is a shared container used by both Host App as well as App Extension. It is like UserDefaults that store key-value pairs.
So you can save your current user information in App Group from Host App and then access it in your Today Extension from the same App Group.
For more on App Groups refer to: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

share core data between many users on app

I wanna develop an iOS app as the platform for many users. Is it possible to enable multiple users of my app to share resources on the same core data database?
The other alternative to iCloud (with the whole ownership issue) is to have an online server that stores the data remotely.
Then you can create a web service to store and retrieve the data from the server onto the device.
yes This can be done using a custom url scheme on the iphone by help of iCloud URL.
but it has some dark sides.If you use a custom url scheme, only one application "owns" the data. The other application would have to import data from the main application.
another method is system pasteboard i.e. clipboard. you can put stuff on it and then launch another application with a URL that tells the other app to check the pasteboard.There's also the system pasteboard .
for more information on custom URL scheme check this link

Resources