i have implemented URL Scheme in my App so that whenever i click the URL, it will open up my app. I have also inserted some request in the URL such as
Yuvtime//:?registerName=Puppy&Passwords=67825
and i use the openUrl in AppDelegate to handle the processing of the URL and pass the data (registerName=Puppy&Passwords=67825`) to my rootViewController which is the registration page of my app, the code is as shown below.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
// Check the calling application Bundle ID
if ([[url scheme] isEqualToString:#"yuvitime"])
{
NSLog(#"Calling Application Bundle ID: %#", sourceApplication);
NSLog(#"URL scheme:%#", [url scheme]);
self.yuvitimeRequest = [url query];
NSLog(#"URL query: %#", yuvitimeRequest);
return YES;
}
else
return NO;
}
And in the RegistrationviewController, I implemented the code as follows.
-(void)viewWillAppear:(BOOL)animated{
[super viewDidAppear:animated];
AppDelegate * myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.requestFromURL = myAppDelegate.yuvitimeRequest;
NSLog(#"%# hello google", self.requestFromURL);
if (self.requestFromURL.length > 0){
self.isRequestFromURL = YES;
}
if(self.isRequestFromURL == YES){
self.mainViewController.joinRoomName = self.requestFromURL;
self.isRequestFromURL = NO;
self.requestFromURL = #"";
[self presentViewController:self.mainViewController animated:YES completion:nil];
}
}
However, the problem i encounter is that:
If the user already has its app opened, this viewWilAppear in the RegistrationViewController will not be called again, so the data will not be passed automatically from the URL. (Only by killing the app, and click the URL will the data be passed as what we initially desired).
I also understand that when an App is already open and called again, only
(void)applicationWillEnterForeground:(UIApplication *)application &
(void)applicationDidBecomeActive:(UIApplication *)application
will be called in sequence in the app delegate. But somehow i stuck on how do i pass data to my registrationViewController from these two methods and if i can manage to do it in these two application methods, that would mean that my app checks every time on whether there is any registration data available from URL when the app comes to the foreground.
Since we already have the
(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
to handle the URL scheme, is it possible that we only check once without using the foreground and didBecomeActive app delegate functions when we click the URL?
EDIT: After implementing URL scheme, whenever you click a URL, a delegate method in AppDelegate.m will be triggered.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
This is also the function that will enable us to process the URL in our app and pass it around to other viewControllers.
I was kinda thinking, is there a way that we can implement a notification/function such that only when this openURL function is called, we then call the triggerURL function in registrationViewController.
Just out of curiosity!
Thanks
Regards
In your RegistrationViewController, you can register for UIApplicationDidBecomeActiveNotification and get notified of application being awakened even when your view controller was already loaded.
Add this line in your loadView:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(triggerURL) name:UIApplicationDidBecomeActiveNotification object:[UIApplication sharedApplication]];
This will allow the function updateSettings to be called every time your app wakes. Also remember to remove this listener at the end by:
[[NSNotificationCenter defaultCenter] removeObserver:self];
in your dealloc method.
Post that, this is how your viewWillAppear & triggerURL will look like:
- (void)viewWillAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self triggerURL];
}
- (void)triggerURL {
AppDelegate * myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.requestFromURL = myAppDelegate.yuvitimeRequest;
NSLog(#"%# hello google", self.requestFromURL);
if (self.requestFromURL.length > 0){
self.isRequestFromURL = YES;
}
if(self.isRequestFromURL == YES){
self.mainViewController.joinRoomName = self.requestFromURL;
self.isRequestFromURL = NO;
self.requestFromURL = #"";
[self presentViewController:self.mainViewController animated:YES completion:nil];
}
}
Related
My application has the following steps:
Send file with own extension to user's email
When file is opened, the app is started (after user has select it)
Now I want to use the data from the file.
But how?
I added following function to my AppDelegate.m file in Xcode.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url){
NSString *information = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
UIViewController *rootViewController = [UIViewController new];
//this is not the way to display this on the screen
NSLog(#"The file contained: %#",information);
}
return YES;
}
Is it possible to use this information string in react-native? What do I need to add to use it as 'this.props.information' in my react-native app?
i have implemented google play games in my app for leaderboard.
my requirement is to display info Alert when user login first time in safari(redirect to safari automatically for login)
i have written this code
[GPGManager sharedInstance].statusDelegate = self;
BOOL isSignedIn = [[GPGManager sharedInstance] signInWithClientID:kGoogleClient silently:flag];
NSLOG(#“is signedin %d",[GPGManager sharedInstance].isSignedIn);
but every time [GPGManager sharedInstance].isSignedIn this variable returns false.
any one have any idea ??
You must configure the GGLContext shared instance. You can do this in many places in your app. Often the easiest place to configure this instance is in your app delegate's application:didFinishLaunchingWithOptions: method.
In your app delegate's .h file, declare that this class implements the GIDSignInDelegate protocol.
#import <Google/SignIn.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, GIDSignInDelegate>
AppDelegate.h
In your app delegate's application:didFinishLaunchingWithOptions: method, configure the GGLContext shared instance and set the sign-in delegate.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSError* configureError;
[[GGLContext sharedInstance] configureWithError: &configureError];
NSAssert(!configureError, #"Error configuring Google services: %#", configureError);
[GIDSignIn sharedInstance].delegate = self;
return YES;
}
Implement the application:openURL:options: method of your app delegate. The method should call the handleURL method of the GIDSignIn instance, which will properly handle the URL that your application receives at the end of the authentication process.
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary *)options {
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
In the app delegate, implement the GIDSignInDelegate protocol to handle the sign-in process by defining the following methods:
- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
// Perform any operations on signed in user here.
NSString *userId = user.userID; // For client-side use only!
NSString *idToken = user.authentication.idToken; // Safe to send to the server
NSString *fullName = user.profile.name;
NSString *givenName = user.profile.givenName;
NSString *familyName = user.profile.familyName;
NSString *email = user.profile.email;
// ...
}
- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
withError:(NSError *)error {
// Perform any operations when the user disconnects from app here.
// ...
}
I'm building SDK for my product and there will be no app delegate class. I used Dropbox Integration in my SDK, so I'm bound to use following method.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSString *stringUrl = [url absoluteString];
if ([stringUrl containsString:#"cancel"]) {
// Handle if user cancelled the login
[[NSNotificationCenter defaultCenter] postNotificationName:#"dropboxRegistrationCancel" object:self];
return NO;
}
if ([[DBSession sharedSession] handleOpenURL:url]) {
if ([[DBSession sharedSession] isLinked]) {
NSLog(#"App linked successfully!");
// At this point you can start making API calls
[[NSNotificationCenter defaultCenter] postNotificationName:ApplicationComeBackFromDropBoxLoginPage object:self];
}
return YES;
}
// Add whatever other url handling code your app requires here
return NO;
}
So Issue is there is no app delegate than how can use above method?
Or any alternate solution for this to use this in another class (like to inherit UIResponder class).
Looking for help.
If you are building a SDK with no appDelegate, then well, you have nothing to do. It will be in your project where you will be using your sdk that you will have to add this line.
By the way, the line NSString *stringUrl = [url absoluteString]; is absolutly useless since you are not using stringUrl later on, and the
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// NSString *stringUrl = [url absoluteString];
return YES;
}
just means: yes you can quit my app to open another app when the user click on a link.
No biggy....
I'm new to ios(android dev)
I'm handling opening my app from url, and i got the url in current method in my AppDelegate.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(#"Calling Application Bundle ID: %#", sourceApplication);
NSLog(#"URL scheme:%#", [url scheme]);
NSLog(#"URL query: %#", [url query]);
return YES;
}
There are certain params in my link which i'd like to use to fill the textField of my LoginViewController.
Can i communicate with LoginViewController from AppDelegate? consider that LoginViewController already didLoad.
You can define one property let’s say strName in appDelegate.
Then you have to assign it’s value from this method
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSLog(#"Calling Application Bundle ID: %#", sourceApplication);
NSLog(#"URL scheme:%#", [url scheme]);
NSLog(#"URL query: %#", [url query]);
//for example
self.strName = [url query];
return YES;
}
Now you can access this property from anywhere.
Create Instance of appDelegate In your viewController
#define AppDel ((AppDelegate *)[UIApplication sharedApplication].delegate)
Then assign as below
textfield.text = AppDel.strName;
You can communicate with your controller in several ways:
Hold instance in appDelegate and call methods.
Save your data in some sort of storage (UserDefault for ex.) and extract in in viewWillAppear or viewDidAppear methods.
Use your own observer pattern implementation or NotificationCenter
I have an issue with URL Scheme in plist file as "m.zameen.com"
but i type this in iPhone's safari browser not op[en but when i open using :// it opened
// In AppDelegate.m file
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if([[url host] isEqualToString:#"page"]){
if([[url path] isEqualToString:#"/main"]){
[self.mainController setViewControllers:#[[[DLViewController alloc] init]] animated:YES];
}
else if([[url path] isEqualToString:#"/page1"]){
[self.mainController pushViewController:[[Page1ViewController alloc] init] animated:YES];
}
return YES;
}
else{
return NO;
}
}
// In DLViewController.m file
- (IBAction)page1Clicked:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"m.zameen.com://page/page1"]];
}
// In Page1ViewController.m file
- (IBAction)mainPageClicked:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"m.zameen.com://page/main"]];
}
In your project Info tab, add a new URL Type with the scheme you want to use to open the app, for example 'myAwesomeAppScheme', using your app bundle identifier 'com.myCompany.myAwesomeApp' in the identifier field and the scheme you want in the URL Schemes field:
Then in you app delegate you can check if the opened url has your scheme with something like this
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([url.scheme isEqualToString:#"myAwesomeAppScheme"]) {
...
}
}
And finally to open the app from an external app, the link has to be something like myAwesomeAppScheme://parameters/for/opening/viewcontrollers?otherParam=blahblah