How to use UIApplication handleOpenURL Notifications - ios

I am trying to handle UIApplication Notifications to get URL Schemes in current open view. I have tried several notifications but i don't know which object contains the URL Schemes.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
//[nc addObserver:self selector:#selector(DocumentToDropboxDelegate) name:UIApplicationWillResignActiveNotification object:nil];
[nc addObserver:self selector:#selector(DocumentToDropboxDelegate) name:UIApplicationDidFinishLaunchingNotification object:nil];
Can someone pelase help me with this issue.

As #Mike K mentioned you'll have to implement one (or both) of the following methods:
- application:handleOpenURL:
- application:openURL:sourceApplication:annotation:
on your UIApplicationDelegate. There is no matching notification for them.
Example below:
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if (url != nil && [url isFileURL]) {
[self.viewController handleOpenURL:url];
}
return YES;
}
//Deprecated
-(BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if (url != nil && [url isFileURL]) {
[self.viewController handleOpenURL:url];
}
return YES;
}

application:handleOpenURL: is called on your application delegate - not via a NSNotification. the preferred delegate method to implement is: application:openURL:sourceApplication:annotation:.
more info can be found here:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleOpenURL:

Related

Why its not showing anything when i open the app with url?

I have following setup, when i open my app (app number 2 open by app number 1 using url) using url in iPhone 7, then i am not getting any url inputs.
its not showing any alert box at all.
How do i check if i am receiving the url input in my app at all or not?
AppDelegate.m:
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
ViewController.m:
-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
//if (url != nil && [url isFileURL]) {
//[self.ViewController handleOpenURL:url];
//}
id test = (NSURL *) url;
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:#"My Title"
message:test
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
//NSLog(#">>> got <<< >>> <<< !!!! >>>> : %#", url);
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
The problem is that
-(BOOL) application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
... is a UIApplicationDelegate method. So it can't be located in ViewController.m. It needs to be in AppDelegate.m.

Dropbox API on iOS fails to link

I'm unable to get my app to link to dropbox on iOS.
I create the session and ask if it's linked... if no, it opens up Dropbox to get authorization. I say yes, and it returns to my app and sends a notification to start the upload.
But isLinked is still NO, so the file fails to upload if I try. Any advice is appreciated.
Interestingly, the -sessionDidReceiveAuthorizationFailure: userId: delegate method is never called.
-(void)connectToDropbox{
if (!dbSession) {
dbSession = [[DBSession alloc] initWithAppKey:#"key"
appSecret:#"secret"
root:kDBRootDropbox];
dbSession.delegate = self;
[DBSession setSharedSession:dbSession];
}
if ([[DBSession sharedSession] isLinked]) {
[self beginUpload:nil];
} else {
[[DBSession sharedSession] linkFromController:self.currentViewController];
}
}
- (void)beginUpload:(NSNotification *)note {
if (![[DBSession sharedSession] isLinked]) {
NSError * error = [NSError errorWithDomain:#"myApp"
code:0
userInfo:#{NSLocalizedDescriptionKey : #"Could not link to Dropbox"}];
[[NSNotificationCenter defaultCenter] postNotificationName:kErrorDidOccurNotification object:error];
NSLog(#"File upload failed with error: %#", error);
return;
}
///do the upload if we make it here.
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation NS_AVAILABLE_IOS(4_2){
if ([sourceApplication isEqualToString:#"com.getdropbox.Dropbox"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"beginDropboxUpload" object:nil];
}
/// more code
}
Your code in the app delegate isn't correct. You need something like this:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation NS_AVAILABLE_IOS(4_2) {
if ([[DBSession sharedSession] handleOpenURL:url]) {
NSString *query = url.query;
if ([[url absoluteString] rangeOfString:#"cancel"].location == NSNotFound) { // NO_I18N
[[NSNotificationCenter defaultCenter] postNotificationName:#"beginDropboxUpload" object:nil];
} else {
// Link cancelled
}
} else {
// Something other than Dropbox
}
}

Facebook SDK share always returns sharerDidCancel [duplicate]

This question already has answers here:
FBSDKShareDialog cancels when it should post
(4 answers)
Closed 7 years ago.
I'm trying to share a post with the Facebook SDK, but sharerDidCancel is always called, either if I share or cancel the post.
Here is my code
- (void)shareFacebook {
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:#"http://www.google.com"];
[FBSDKShareDialog showFromViewController:view
withContent:content
delegate:self];
}
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults :(NSDictionary*)results {
NSLog(#"FB: SHARE RESULTS=%#\n",[results debugDescription]);
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
NSLog(#"FB: ERROR=%#\n",[error debugDescription]);
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
NSLog(#"FB: CANCELED SHARER=%#\n",[sharer debugDescription]);
}
This class implements the FBSDKSharingDelegate, in my Info.plist I already entered the FacebookAppID and FacebookDisplayName.
In my AppDelegate I have this method:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}
What I'm doing wrong? I exactly followed the Facebook SDK guide.
Thanks
Solved, my error was in the AppDelegate method:
here is a link with the solution http://jitu1990.blogspot.it/2015/05/share-with-facebook-from-ios-app.html
And here is the code
- (BOOL)application:(UIApplication *)application openURL:(NSURL* )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url sourceApplication:sourceApplication annotation:annotation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return [ [FBSDKApplicationDelegate sharedInstance] application :application
didFinishLaunchingWithOptions:launchOptions]
}

how to call viewWillAppear after redirecting from safari to view controller in IOS

I am using Flickr in my app. When I click on Flickr button it is redirecting to safari to login with Flickr and Authorization.
Then after successful authorization it is redirecting to my app. But after redirecting I want to call ViewWillAppear. I also set <UIApplicationDelegate> at my view. But it is not calling any of the below methods.
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
NSLog(#"#####################Flickr Authorized is completed");
}
- (void)UIApplicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"#####################Flickr Authorized is completed at foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
NSLog(#"#####################Flickr Authorized is completed at become active");
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(#"######################Flickr Authorized is completed at openURL");
}
-(void)viewWillAppear:(BOOL)animated
{
NSLog(#"#####################Flickr Authorized is completed at viewWillAppear");
[super viewWillAppear:YES];
}
But Below method is calling after redirecting from safari at AppDelegate. I have used Code from SnapAndRun flickr sample.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(#"######################Call Back Here###########");
if ([self flickrRequest].sessionInfo)
{
// already running some other request
NSLog(#"Already running some other request");
}
else {
NSString *token = nil;
NSString *verifier = nil;
BOOL result = OFExtractOAuthCallback(url, [NSURL URLWithString:SRCallbackURLBaseString], &token, &verifier);
if (!result) {
NSLog(#"Cannot obtain token/secret from URL: %#", [url absoluteString]);
return NO;
}
[self flickrRequest].sessionInfo = kGetAccessTokenStep;
[flickrRequest fetchOAuthAccessTokenWithRequestToken:token verifier:verifier];
[activityIndicator startAnimating];
//[viewController.view addSubview:progressView];
}
return YES;
}
I know that we can call my view controllers method here. But Is there any alternative for default delegate method to be called after redirecting at my view itself.
Any Suggestions will be appreciated.
Thanks in Advance.
You can use notificationCenter.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(someMethod:) name:#"UIApplicationDidBecomeActiveNotification" object:nil];

Segue after openURL in AppDelegate

I have a button with a action (toggleApplication) that opens another application. When I return to my application from the one that was opened I want to segue to another view. But I get the following error from my code:
Receiver (RootViewController: 0x1f192450) has no segue with identifier 'showReceipt'
AppDelegate.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
RootViewController *controller = [RootViewController alloc];
return [controller handleURL:url];
}
RootViewController.m
- (IBAction)toggleApplication:(id)sender{
// Open application
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:theUrlString]];
}
- (BOOL)handleURL:(NSURL *)url{
[self performSegueWithIdentifier:#"showReceipt" sender:self];
return YES;
}
Figured it out by using NSNotificationCenter.
AppDelegate.m
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
[[NSNotificationCenter defaultCenter] postNotificationName:#"segueListener" object:nil];
return YES;
}
RootViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiptSegue) name:#"segueListener" object:nil];
}
- (IBAction)toggleApplication:(id)sender{
// Open application
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:theUrlString]];
}
- (void)receiptSegue{
[self performSegueWithIdentifier:#"showReceipt" sender:self];
}
Does exactly what I want. Don't know if it's the right approach though.

Resources