Opening in app, getting UIApplicationLaunchOptionsURLKey from launchOptions - iphone

I'm trying to open a .pdf-file in my app. I adapted the Info.plist so a .pdf can be opened in my application.
I use the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
thePDFurl = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
return YES;
}
In an other class, where my appDelegate (containing that didFinishLaunchingWithOptions), I've got the line:
appDel = [[UIApplication sharedApplication]delegate];
[theLabel setText:[NSString stringWithFormat:#"%#", appDel.thePDFurl]];
Somehow, theLabel always shows (null). What am I missing?

I may be misunderstanding what you're trying to do. If so, ignore.
If you want the user to be able to "Open with..." a PDF using your app, you can implement
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
E.g.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(#"Open URL:\t%#\n"
"From source:\t%#\n"
"With annotation:%#",
url, sourceApplication, annotation);
NSString *filepath = [url path];
//...
return YES;
}
I'm pretty sure this works for both launching the app and calling it (i.e. if it's already in the background).

You may retain the pdfurl variable and also get the absolute string value from NSURL using the absoluteString method.
[theLabel setText:[NSString stringWithFormat:#"%#", [appDel.thePDFurl absoluteString]]]

I guess you were all right. It just so happened the view got loaded before the method applicationDidFinishLaunching was finished. Thanks all...

When you are calling within application:didFinishLaunchingWithOptions: ,
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[viewController view] was implicitly called and there was no appDel.thePDFurl assigned yet.

Related

Linking facebook sdk to ios

I have a dead line and dont know what to do with that. IM tryin to use react-native-fbsdk with firebase on and wanted to link the sdk to ios. Everything is good but this function:
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
// Add any custom logic here.
return YES;
Im trying to put this here:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:#"index.ios" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:#"tzawajna"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
// Add any custom logic here.
return YES;
}
And Its giving an error of
use of undeclared identifier FBSDKApplicationDelegate
What should I do.
Thanks
Check whether In your application:openURL:options implementation ( or for any other method in the file), the parameter is named app and not application.
You can either change the parameter name in the method signature to application:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
This error can be caused because of the difference in the parameter of such method.

Integration of Dropbox in iPhone application error 400

I am using the great source code called GSDropboxDemoApp which nicely integrate the Dropbox in the application. However, after linked to the app to Dropbox, it gives error in loading folder contents. Below are the codes
I have already edited the info.plist
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#warning Potentially incomplete method implementation. Fill in your Dropbox credentials!
#warning NB: you must also update the URL scheme listed under the CFBundleURLTypes key in GSDropboxDemoApp-Info.plist
NSString *dropboxAppKey = #"sxxxxxxxxxx";
NSString *dropboxAppSecret = #"cxxxxxxxxx";
NSString *dropboxRoot = #"kDBRootAppFolder"; // either kDBRootAppFolder or kDBRootDropbox
DBSession* dbSession = [[DBSession alloc] initWithAppKey:dropboxAppKey
appSecret:dropboxAppSecret
root:dropboxRoot];
[DBSession setSharedSession:dbSession];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[GSViewController alloc] initWithNibName:#"GSViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([[DBSession sharedSession] handleOpenURL:url]) {
if ([[DBSession sharedSession] isLinked]) {
NSLog(#"App linked to Dropbox successfully");
} else {
NSLog(#"App not linked to Dropbox!");
}
return YES;
}
return NO;
}
Error Message
GSDropboxDemoApp[4674:907] [WARNING] DropboxSDK: error making request to /1/metadata/kDBRootAppFolder - (400) Expected a root of either 'dropbox' or 'sandbox', got 'kDBRootAppFolder'
You should be using a constant, like so:
NSString *dropboxRoot = kDBRootAppFolder; // either kDBRootAppFolder or kDBRootDropbox
Constants are used so that you don't need to worry about the actual value itself.
And for those of us (in the future that stumble across this question) getting the same error using ruby motion, it needs to be:
dropboxRoot = KDBRootAppFolder # Capital K

Inter-App Communication

I have an application that accepts input from the user. The information is used to create a query string which is forwarded to a second application using a custom URL scheme. The second application is opened as expected, but the application:openURL method in the AppDelegate of the 2nd application is never visited. Attached is the code from this method:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ProcessorViewController *viewController = (ProcessorViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"ProcessorController"];
return [viewController handleURL:url];
}
Any assistance determining why this method in the app delegate is never visited would be greatly appreciated.

How to use [[UIApplication sharedApplication] openURL:] open other app?

I followed http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html instruction to open app1(GlassButton) within app2(FontTest).
The open method of FontTest as following:
-(void)open {
BOOL res = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"glassbutton://"]];
if (res) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"glassbutton://"]];
}
}
The value of "res" is "YES", but nothing happen after openURL method be called.
The info-list of "FontTest"as following:
URL Schemes: glassbutton
URL identifier: com.yourcompany.glassbutton
I tried to open twitter and facebook apps by "twitter://" and "fb://" successfully. But I do not know why I cannot open this small app. I'm not sure whether any thing/step wrong or missing? Need I handle - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url for FontTest, if yes, how to handle it? Could you please kindly help me? Thanks in advance!
To request the launch of your app use something like this:
NSString *urlString= #"glassbutton://com.yourcompany.glassbutton";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
Then, in the glassbutton app, you'll need to handle any special behavior within the app delegate method:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
//your app specific code here for handling the launch
return YES;
}
Note that within the app you are opening the above delegate method will only get called AFTER the following method is called:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Handle accordingly, good luck.

How to handle a custom URL in an already-open app?

I have an app based on the Utility template (i.e. Main and Flip view controllers). The Flip view allows selecting a certain item to be used on the main view. So far - works great.
Now I tried adding a custom URL. Something to the effect of: myapp://itemID=40 that will basically tell the main view: "no need to flip - you'll be dealing with item 40".
I registered the URL type scheme as "myapp" and added the following method to the app delegate:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if (!url) {
return NO;
}
NSString *urlString = [url absoluteString];
NSLog(#"URL received: %#", urlString);
NSString *itemID = [urlString stringByReplacingOccurrencesOfString:#"myapp://itemID=" withString:#""];
NSLog(#"Item received: %#", itemID);
[_mainViewController setStartupItem:itemID];
return YES;
}
As you can see, the itemID is set to a property called startupItem in the mainViewController.
I then added one line to the regular application method to verify that startupItem will be nil in case of no URL:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Make sure URL string is empty
[_mainViewController setStartupItem:nil];
// Override point for customization after application launch.
// Add the main view controller's view to the window and display.
self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}
And in MainViewController.m I added code to handle the item to the viewDidLoad event.
And here's my problem: this scheme works great if the app is started from a URL the first time. If it's already running, then we never reach viewDidLoad again and therefore don't handle that particular item, but go on as if none was passed.
My humble question is: which UIViewController should I put my handling code in? Or, am I approaching all this the wrong way? Should this be handled in my model?
As always, thanks in advance for your time!
Guy
I would take a look at the docs for UIApplicationDelegate protocol, specifically;
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
And this which is deprecated.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
Well definitely not in a method that gets called only once while the application starts up! You need to refactor the item handling code in its own method, then call this method from viewDidLoad (once during startup) and handleOpenURL each time it gets called.