Inter-app communication in iOS using URL scheme - ios

I have two test apps: App1 & App2.
App1 takes String from the text field and triggers method:
#IBAction func openApp(sender: AnyObject) {
let url1 = ("app2://com.application.started?displayText="+textToSend.text!)
let url2 = url1.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
UIApplication.sharedApplication().openURL(NSURL(string: url2!)!)
}
Which actually opens App2 which has only label which should change to the text sent through the url, code is within AppDelegate.swift:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
let url = url.standardizedURL
let query = url?.query
ViewController().labelToDisplayResult.text = query
return true;
}
Unfortunately the line where I am trying to pass result of the URL to the actual label is giving me this error:
EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)
However I have all the data in the App2 for sure as I can see their values in debugger:
url NSURL "app2://com.application.started?displayText=564315712437124375" 0x00007fa4e3426320
query String? "displayText=564315712437124375"
Any idea why I am getting this error?
Thanks...

Your error
ViewController().labelToDisplayResult.text = query
ViewController() Create a new instance of ViewController,not the one loaded from storyboard.I guess labelToDisplayResult is an outlet, so it is nil,so you get EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)
This is what I usually do to handle openURL scheme,need to think about two states:
Target App is launched before,so when open url happen,the target app is in background or inactive state
Target App is not launched,so when open url happen,the target app is not running at all
In Appdelegate
class AppDelegate: UIResponder, UIApplicationDelegate {
var openUrl:NSURL? //This is used when to save state when App is not running before the url trigered
var window: UIWindow?
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
let url = url.standardizedURL
NSNotificationCenter.defaultCenter().postNotificationName("HANDLEOPENURL", object:url!)
self.openUrl = url
return true;
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return true
}
}
Then in the ViewController handle openURL
class ViewController: UIViewController {
#IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleOpenURL:", name:"HANDLEOPENURL", object: nil)
let delegate = UIApplication.sharedApplication().delegate as? AppDelegate
if let url = delegate?.openUrl{
testLabel.text = url.description
delegate?.openUrl = nil
}
}
func handleOpenURL(notification:NSNotification){
if let url = notification.object as? NSURL{
testLabel.text = url.description
}
}
deinit{
NSNotificationCenter.defaultCenter().removeObserver(self, name: "HANDLEOPENURL", object:nil)
}
}

Related

iOS Action Extension not opening my main app

I want to be able to open Safari links in my app. For this I have created an Action Extension. I have tried multiple variations of the following code to test but none of them open my main app:
First try:
func openContainerApp() {
var responder: UIResponder? = self as UIResponder
let selector = #selector(openURL(_:))
while responder != nil {
if responder!.responds(to: selector) && responder != self {
responder!.perform(selector, with: URL(string: "awesome://item?id=20036169")!)
}
responder = responder?.next
}
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
}
Second try:
func redirectToHostApp() {
let url = URL(string: "awesome://item?id=20036169")
let selectorOpenURL = sel_registerName("openURL:")
let context = NSExtensionContext()
context.open(url! as URL, completionHandler: nil)
var responder = self as UIResponder?
while (responder != nil){
if responder?.responds(to: selectorOpenURL) == true{
responder?.perform(selectorOpenURL, with: url)
}
responder = responder!.next
}
}
I have added the "awesome" URL scheme in my main app. I am also returning "true" for all possible openURL delegate methods in my app delegate:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return true
}
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return true
}
I have set the NSExtensionActivationSupportsWebURLWithMaxCount in my action extension to 1.
Despite all this, my main app doesn't open. Tapping on the action extension in Safari does nothing.
I have tried these solutions but none work:
https://stackoverflow.com/a/28037297/1634905
https://forums.developer.apple.com/thread/65621
I figured it out by myself and wanted to share this in case someone gets stuck with the same.
My problem was that I was calling the openContainerApp or redirectToHostApp functions from viwDidLoad() where the UIResponder is apparently not ready yet.
I moved the function call to viewWillAppear() and it worked.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
openContainerApp()
}

Swift - App Delegate not passing data

I'm developing an App that it's data is from a URL, here's a sample code that I'm using
AppDelegate.swift
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var fromUrl: String!
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject)-> Bool {
print("Host: \(url.host!)")
self.fromUrl = url.host!
return true
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
let appDelegate = AppDelegate()
override func viewDidLoad() {
super.viewDidLoad()
print(appDelegate.fromUrl)
}
It's is logging the url.host from app delegate. But when i try to log the value of fromUrl from the ViewController.swift it's returning nil. What do you think seems to be the problem? Thanks!
When you declare let appDelegate = AppDelegate() in ViewController you are actually instantiating another instance of AppDelegate. That is not the same instance that you are using as you actual ApplicationDelegate. Try getting that reference by using:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
print(appDelegate.fromUrl)
}

Open fully quitted app from URL scheme did not call the function from postNotificationName in iOS

Here I have set the code for URL scheme:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
if let userUrl = String(url) as String? {
print("\(userUrl)")
if (userUrl == "count://fromClipBoard") {
NSNotificationCenter.defaultCenter().postNotificationName("com.getContentFromClipBoard", object: self)
}
}
return false
}
And add the following code to my ViewController:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "setContentFromClipBoard", name: "com.getContentFromClipBoard", object: nil)
//............
}
func setContentFromClipBoard() {
tv.text = "WAHAHA"
if let clipBoard = UIPasteboard.generalPasteboard().string {
tv.text = clipBoard
}
}
And when my app is not fully quitted, com.getContentFromClipBoard called well and tv.text become clipBoard.
However, when I fully quit the app and then use this URL scheme, the com.getContentFromClipBoard isn't get called and tv.text remain empty.
So how can I fix it? Thanks!
Fixed by checking UIApplicationLaunchOptionsURLKey too inside didFinishLaunchingWithOptions.
(Delaying is waiting the view to load)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let userUrl = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), {
if (userUrl.absoluteString == "count://fromClipBoard") {
NSNotificationCenter.defaultCenter().postNotificationName("com.getContentFromClipBoard", object: self)
}
})
}
}

handleOpenURL won't trigger iOS Swift 2

I am trying to get an Instagram authentication token for an app Im working on, I have the app opening safari, Im able to log in and then it send me back to the app, but when it transfers me back to the app, handleOpenURL is not triggering in AppDelegate. Here is a sample of my code:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
....
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
InstagramAPIManager.sharedInstance.processOAuthStep1Response(url)
print(url)
print("handleOpenURL")
return true
}
}
and here is a my API Manager code that is triggering safari to open:
import Foundation
import Alamofire
class InstagramAPIManager {
static let sharedInstance = InstagramAPIManager()
func startOAuth2Login() {
let clientID: String = "abcdefg"
let authPath:String = "https://api.instagram.com/oauth/authorize/?client_id=\(clientID)&redirect_uri=grokInstagram://?aParam=paramVal&response_type=code"
if let authURL:NSURL = NSURL(string: authPath)
{
UIApplication.sharedApplication().openURL(authURL)
}
}
....
}
Any help would be appreciated!
In iOS 9, application:handleOpenURL: is deprecated. Instead, you should be using:
func application(app: UIApplication,
openURL url: NSURL, options: [String : AnyObject]) -> Bool {
The correct syntax seems to be
func application(_ app: UIApplication,
open url: URL,
options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return true
}
according to https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623112-application

subscribeNext not getting values in AppDelegate

I have scenario where my app receives an application:openURL:sourceApplication call from an extension and I want to open a tab in my tabBar and once its fully loaded then sendNext to a signal that the tabBar will be listening to once its loaded.
so sequence of events would be something like:
AppDelegate receives an OpenURL call
AppDelegate requests tabBar to load/open a tab called RemindersViewController
RemindersViewController RACSubject called RemindersLoadedSignal which i send to it a next value of true once the view has complete loading and binding
AppDelegate listens to the RemindersLoadedSignal and when it receives next from it it will emit its next signal to openURLSubject.
RemindersViewController listening to openURLSubject will perform some action since now its loaded and the signal was emitted.
class AppDelegate: UIResponder, UIApplicationDelegate {
let openURLSubject = RACSubject()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
RemindersLoadedSignal.subscribeNextAs({ (loaded:NSBool) -> () in
println("This subscription works for some reason")
})
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
myTabBar.selectTabWithClass(RemindersViewController.self)
//SendNext to openURLSubject once the tab is loaded
RemindersLoadedSignal.subscribeNextAs({ (loaded:NSBool) -> () in
println("This subscription Never gets called")
self.openURLSubject.sendNext(reminderURLRequest)
})
return true
}
}
let RemindersLoadedSignal = RACSubject()
class RemindersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
bindViewModel()
}
func bindViewModel(){
sharedAppDelegate().openURLSubject.subscribeNextAs { (request:OpenURLRequest) -> () in
println("got url request")
}
RemindersLoadedSignal.sendNext(NSBoolTrue)
}
}
I dont understand how the subscription in didFinishLaunchingWithOptions works and the one in application:openURL:... doesnt.
I ended up loading the view with doNext and Zipping the signal with the RemindersLoadedSignal.
If you see any potential problems with that please let me know.
Thanks
.doNext { (_:AnyObject!) -> Void in
if let myTabBar = self.window?.rootViewController as? UITabBarController {
myTabBar.selectTabWithClass(RemindersViewController.self)
}
}
.zipWith(RemindersLoadedSignal)

Resources