run iOS app from web (angular) - ios

I'm developing an iOS app
i have a payment page designed by angular
user click on a payment button in ios app and i run a url page with few paramaters :
UIApplication.sharedApplication().openURL(NSURL(string:"http://www.testt.com/price/personId/packageName")!)
price is money user has entered in textfield and package-name id the schema name i should send it to web page that runs my app (return to app with running that string) i have declared in info.plist
then after been successful or unsuccessful payment. it should return to app by clicking on “return to app” button on web site.
actually angular runs the packageName i have sent with url like this way : http://packageName://
i tried to implement this by universal link like this way : packageName:// but wont open this link because of special chars in url.i used encoding method to encode chars but not successful because url removes the chars :// then i tried app site association method which i faced cannot parse app site association file
so i have few question for you :
1_is there any trick to run url with special chars ??
2_what would you do if you were me ??
3_i tried apple-app-site-association too but can not parse error which i have a question about this method how could this file opens my app? this way : applink:http://msite.com ?? because it contains spacial chars in it again
excuse my awful English at the end
talk to me before voting down
update :
var encodedChars="openMyApp" //schema name
encodedChars=encodedChars.addingPercentEncoding(withAllowedCharacters:CharacterSet.alphanumerics)!
let url="http://test.com/#/payment/\(id)/\(price!)/\(encodedChars)"
UIApplication.shared.openURL(NSURL(string: url)! as URL)
angular code :
if (this.accounting.packageName === 'openMyApp') {
this.url = this.accounting.packageName + '://';
} else {
this.url = 'http://' + this.accounting.packageName;
}
<a class="btn btn-default" title="" href="{{url}}"></a>

I have implemented custom url scheme for my app. If i write some thing like this
myappName://
in safari and press enter, my app is opened.
My angular + backend developer is using this line of code to open my app using custom url scheme. he is sending parameters along the scheme which i can read
this.document.location.href = 'myappname://appname.com/login?name='+id+'&id='+token;
Follow these 2 simple steps, i hope this will help you.
1.How to implement custom url schemes
Official docs
Helpful link
2.How to handle custom URL scheme with params in app delegate
handle the custom url and read params in appDelegate in this function
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let query = url.query {
//that's my logic. yours url may be different
let components = query.components(separatedBy: "=")
print(components)
}
}
let me know if you need any help

Related

How to fetch the appID without using Branch SDK iOS?

I am working in iOS swift project. I have integrated branch for deep link activity. In the branch link we have appended the appID and retrieved the id when the user clicked the app link using below code in didFinishLaunch in AppDelegate.
let branch: Branch = Branch.getInstance()
branch.initSession(launchOptions: launchOptions, automaticallyDisplayDeepLinkController: true, deepLinkHandler: { params, error in
if error == nil {
// parse appID
}
})
But sometimes, we can’t fetch the ID due to branch error. So we planned to remove branch integration in our code. But plist contains URL Scheme, URL identifier etc.. for navigate to this app.
So, When the user clicks the app link, how to fetch the value with out using above code?
Additional note:
The below function is not triggered in my app while clicking the app link. Is any other way to trigger this function, so that I can check the url value in the below function to processing further.
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
}

Authorization callback URL GitHub

I'm beginning iOS developer. I create an application that uses GitHub authorization. When I register a new OAuth application in GitHub developer program I must enter Authorization callback URL. But I do not have any site for my app. What do I need to specify in this field?
You can use deep linking.
you can read more about it here
The deeplink will try to open the app or redirect to it. The web browser or SFAuthenticationSession will close the browser and call the completion hander where you can check for the response code without any implementation for the deeplink.
To add the deep link in the app you can this below:
Select the project in Xcode navigator.
then select your target that you want to add the deep link to it.
select info from the top bar
at the bottom open the URL Types
add a name for the scheme
when you generate the URL for the oauth you can pass anything you want I just pass login in this example:
func getAuthenticateURL() -> URL {
var urlComponent = URLComponents(string: "https://github.com/login/oauth/authorize")!
var queryItems = urlComponent.queryItems ?? []
queryItems.append(URLQueryItem(name: "client_id", value: "YOUR_CLIENT_ID_HERE"))
queryItems.append(URLQueryItem(name: "redirect_uri", value: "APP_SCHEME_GOES_HERE://login"))
urlComponent.queryItems = queryItems
return urlComponent.url!
}
Then when you need to login do this:
import SafariServices
var authSession: SFAuthenticationSession?
func authenticate(with url: URL, completion: #escaping ((_ token: String?, _ error: Error?) -> Void)) {
authSession?.cancel()
authSession = SFAuthenticationSession(url: url, callbackURLScheme: nil, completionHandler: { url, error in
//get the token and call the completion handler
})
authSession?.start()
}
or use ASWebAuthenticationSession the same way if you're on iOS 12
Using SFAuthenticationSession you can do something like this. On your App add URLType:
Then on GitHub 'Developer Settings' for your app, add Authorization callback URL like this:
This way, after you login and authorize, Git Hub will call back //yourappname and Safari will redirect it back to your app completing the flow.

How to handle a file sent with 'Open in...' from another app to my own iOS app?

I am developing an app that reads csv files and display its content in a tableViewController. When doing my testings using Xcode, I have a sample file within the projects directory and I am perfectly able to read that file using its location path. The problem that I have is I want to be able to take any csv file (sent to me via some method), click the 'Open in' button and have the file sent to my app.
My app is being displayed in the available applications to which I can send the file to. My question is what happens after? When I choose to send it to my app it then switches over to my app and from there, I don't know how to receive the file and read it to extract its content. Where does the file go and how do I interact with it?
This is handled in your AppDelegate, more precisely, you get passed an URL to the document and then you handle it from there in optional function, e.g.:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
do {
let data = try Data(contentsOf: url)
// Do something with the file
} catch {
print("Unable to load data: \(error)")
}
return true
}
More info: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application

iMessaged-based invitations for GameCenter for iOS 10

I'm trying to update my app to work correctly with the new features of GameCenter in iOS10.
I create a new GKGameSession on device1, get a share URL, and all that works fine. I send the share URL out via a share sheet to device 2.
Device2 clicks the link, the device briefly displays 'Retrieving...' and then launches my app. Great! But, now what? Is there context information available for this URL that I can somehow access? Otherwise I have no way how to respond when the app is launched.
Previously you'd get a callback to something adhering to the GKLocalPlayerListener protocol, to the method player:didAcceptInvite:, and you could join the match that way. But with these iCloud-based messages, the player might not be even logged into GameCenter, right? This part seems to have been glossed over in the WWDC presentation.
Also, as of today (12/28/2016) there is no Apple documentation on these new methods.
Since the GKGameSessionEventListener callback session:didAddPlayer: only fires if the game is already running, to be sure you can process this callback every time requires a work around. I've tested this and it works.
When you send out an iMessage or email invite to the game, don't include the Game Session Invite URL directly in the message. Instead use a registered URL that will open your app when opened on a device on which your app is installed. Check here to see how:
Complete Tutorial on iOS Custom URL Schemes
But add a percent escaped encoding of the game invite URL as a parameter to this URL thusly (I'm assuming the registration of a url e.g. newGameRequest but it will be best to make this quite unique, or even better - though it requires more setup, try Universal Link Support as this will allow you to direct users who don't have your app installed to a webpage with a download link)
let openOverWordForPlayerChallenge = "newGameRequest://?token="
gameState.gameSession?.getShareURL { (url, error) in
guard error == nil else { return }
// No opponent so we need to issue an invite
let encodedChallengeURL = url!.absoluteString.addingPercentEncoding(withAllowedCharacters:.urlHostAllowed)
let nestedURLString = openOverWordForPlayerChallenge + encodedChallengeURL!
let nestedURL = URL(string: nestedURLString)!
}
send the URL in a message or email or WhatsApp or whatever. Then in your app delegate, add the following:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
var success = false
if let queryString = url.query {
if let urlStringToken = queryString.removingPercentEncoding {
let token = "token="
let startIndex = urlStringToken.startIndex
let stringRange = startIndex..<urlStringToken.index(startIndex, offsetBy: token.characters.count)
let urlString = urlStringToken.replacingOccurrences(of: token, with: "", options: .literal, range: stringRange)
if let url = URL(string: urlString) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
success = true
}
}
}
}
return success
}
Now you can be sure the session:didAddPlayer: will be called. What's the betting this workarround is good for about 2 weeks, and they fix this in the next release of iOS showcased at WWDC 2017 ! Update: this problem hasn't been fixed - so the workaround above remains good!
I agree, the lack of documentation is frustrating. From what I can see, we have to:
add <GKGameSessionEventListener> protocol in the class' header
Then session:didAddPlayer: fires on the joining player's device after accepting an invite link.
update:
Unfortunately, I'm not surprised to hear your results. I hadn't tried all of those scenarios, but GKTurnBasedMatch had similar shortcomings. The way I got around it there was: I added a list of player statuses to match data (invited, active, quit, etc). I gave the player a view of "pending invitations." When they opened that view, I would load all of their matches and display the entries where the player was in invited state. With GKGameSession, that should work too.
Or, it might be easier if you could maintain a local list of sessions that you are aware of. Whenever the game becomes active, pull the entire list of sessions from the server and look for a new entry. The new entry would have to be the match the player just accepted by clicking the share URL.

iOS 9 safari iframe src with custom url scheme not working

I use this solution https://gist.github.com/davidwkeith/2662899 to redirect from web page into my app if app installed.
But it doesn't work on iOS 9. It's still working in Google-chrome. But iframe with custom URL scheme can't launch the application from Safari.
If I replace
document.getElementById('loader').src = 'custom-protocol://my-app'
(where loader is iframe) with
window.location = 'custom-protocol://my-app'
it will work.
os: iOS 9 beta4 and beta5
Anybody knows this problem? Is it iOS 9 beta bug? Or it will not be fixed?
The previous answer is a partial implementation of Universal Links that is missing critical details and doesn't include a fallback to the App Store.
First, you can no longer set iframe src in order to trigger a URI scheme. You've correctly identified that issue. As you noted, you can, however, still set window.location = 'custom-protocol://my-app';. So if you know that a user has your app because you've previously opened their app from the browser and have a cookie stored that can be looked up on your backend, you can still safely fire custom-protocol://.
Second, you can detect the user agent string using navigator.userAgent. Pre-iOS 9 you can still use the iframe to fire a URI scheme, then fallback after a timeout. On iOS 9, you can choose whether to fire the URI scheme or not based on cookies, then take the user to the App Store. I work on this at Branch and making use of cookies to recall whether a user likely has the app is something we've implemented. Feel free to reach out if you have more questions about that, or make use of our solution directly.
Implementing Universal Links is not quite as simple as the other answer describes. In reality, there is considerably more complexity. Here's a complete list of steps (I've helped several apps integrate in recent weeks using these steps):
1. Configure your app to register approved domains
i. Registered your app at developer.apple.com if you haven't
ii. Enable ‘Associated Domains’ on your app identifier on developer.apple.com
iii. Enable ‘Associated Domain’ on in your Xcode project
iv. Add the proper domain entitlement, applinks:yourdomain.com, in your app
2. Configure your website to host the ‘apple-app-site-association’ file
i. Buy a domain name or pick from your existing
ii. Acquire SSL certification for the domain name (you can use CloudFlare for this!)
iii. Create structured ‘apple-app-site-association’ JSON file
{
"applinks": {
"apps": [ ],
"details": {
"TEAM-IDENTIFIER.YOUR.BUNDLE.IDENTIFIER": {
"paths": [
"*"
]
}
}
}
}
iv. Sign the JSON file with the SSL certification
v. Configure the file server
The apple-app-site-association file:
- must be sent with the header ‘application/pkcs7-mime’
- must be sent from the endpoint youdomain.com/apple-app-site-association
- must return a 200 http code.
Example Express+Node:
var aasa = fs.readFileSync(__dirname + '/static/apple-app-site-association');
app.get('/apple-app-site-association', function(req, res, next) {
res.set('Content-Type', 'application/pkcs7-mime');
res.status(200).send(aasa);
});
credit: borrowed liberally from this blog post
Yes with iOS9 now you can deep link. Check the link for detailed explanation but I laid out the basics.
http://blog.hokolinks.com/how-to-implement-apple-universal-links-on-ios-9/
first you must go to your target and click capabilities. Add the associated domain.
Next you must upload apple-app-site-association file.
Basically open a JSON editor and construct something like this
{
"applinks": {
"apps": [],
"details": {
"TBEJCS6FFP.com.domain.App": {
"paths":[ "*" ]
}
}
}
}
Next you must support Univeral links in your app. You need to implement
extension AppDelegate {
func application(application: UIApplication, continueUserActivity userActivity: NSUserActivity, restorationHandler: ([AnyObject]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb {
let webpageURL = userActivity.webpageURL! // Always exists
if !handleUniversalLink(URL: webpageURL) {
UIApplication.sharedApplication().openURL(webpageURL)
}
}
return true
}
private func handleUniversalLink(URL url: NSURL) -> Bool {
if let components = NSURLComponents(URL: url, resolvingAgainstBaseURL: true), let host = components.host, let pathComponents = components.path?.pathComponents {
switch host {
case "domain.com":
if pathComponents.count >= 4 {
switch (pathComponents[0], pathComponents[1], pathComponents[2], pathComponents[3]) {
case ("/", "path", "to", let something):
if validateSomething(something) {
presentSomethingViewController(something)
return true
}
default:
return false
}

Resources