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

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.

Related

How do I call this method that registers device

I do not know how to replace "method" with the following app delegate method.
viewcontroller
[(AppDelegate *)[[UIApplication sharedApplication] delegate] method];
appdelegate
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
TUPushHelper * helper = [[TUPushHelper alloc] initWithTokenData:devToken];
[helper registerDevice];
}
You do not need to call it programmatically. The delegate receives this message after the registerForRemoteNotificationTypes: method of UIApplication is invoked and there is no error in the registration process. didFailToRegisterForRemoteNotificationsWithError: will be called otherwise.
To register your device for remote push notifications you have to do this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
...
}
Edit:
Check Apple Guide for APNS here
also check out this tutorial

enabling notifications

I would like to enable notifications in my iphone app. So, I modify in app ID:
After that, I generate again Development and Distribution Provisioning Profiles and installed in my xcode.
My app is a tabbed based application, the first tab is UITableViewController
I add this lines:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
So, I suppose I should have my app in the list of app installed with notifications in my iphone, but it isnt.
Did I miss some step?
Yep. According to this and this you should add the registerForRemoteNotificationTypes method in your didFinishLaunchingWithOptions, which should the look something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
return YES;
}
Depending on which Types you registered your app will appear in the notifications section and you can turn on and off the different types (sound, badge, banner).
Hope that helps.
First enable your remote notification in app delegate. See below :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Let the device know we want to receive push notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return YES;
}

network activity indicator is not shown on iPhone (iOS 5)

just to test it I enabled the activity indicator of the status bar as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: TRUE];
//...
}
I never disable the indicator so it should always be visible, but it isn't. It is visible in the iPhone Simulator but not on the device. Why?
Please not that the application is not active when a call is made to didFinishLaunchingWithOptions. You should move this to the viewDidLoad method instead. Thus the code should look like this:
- (void)viewDidLoad {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
// Some other code goes here...
[super viewDidLoad];
}
Or you could put this code anywhere else where the view has been loaded. Do not forget to stop it once the data has been loaded.

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.

How can I make a call, as I recieve a push notification?

I have developed an appointment application. When I receive a push notification, I want to make a call to a particular person, but right now it is just opening the application when I get a push notification.
How do I write a code to make a call to a particular appointment as I get the push notification of that appointment?
For push notification you have to code in appDelegate,
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://%#",[userInfo valueForKey:#"phno"]]]];
}
For local notification,
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://%#",[notification.userInfo valueForKey:#"phno"]]]];
}
Upon notification, open the dialer app with the given phone number. Note: this will initially launch your app, then quickly switch to the dialer.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel://5555555555"]];
}