Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iMokhles/0bd4d903a8765ca8a713 to your computer and use it in GitHub Desktop.
Save iMokhles/0bd4d903a8765ca8a713 to your computer and use it in GitHub Desktop.
is a simple trick to open iOS application and even a page within the application from WatchKit
/*
//
// How to open iOS application using WatchKit
//
// Created by Mokhlas Hussein on 09/04/15.
// Copyright (c) 2015 iMokhles (Mokhlas Hussein). All rights reserved.
//
*/
/******* Within the App Watch Extension *******/
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
@interface InterfaceController : WKInterfaceController
@end
@implementation InterfaceController
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Configure interface objects here.
}
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
- (IBAction)openAboutUS {
NSDictionary *requst = @{@"pageID":@"openAboutUS"};
[InterfaceController openParentApplication:requst reply:^(NSDictionary *replyInfo, NSError *error) {
// u can get any reply here or check if there are an error
}];
}
@end
/******* Within the App Delegate *******/
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// checking the launch url
if (![launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]) {
NSLog(@"url is nil");
} else {
NSLog(@"url is not nil");
}
return YES;
}
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply {
// handle the request from the watch
NSString *urlStrin;
urlStrin = [userInfo objectForKey:@"pageID"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"URL-SCHEME://%@", urlStrin]]];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
// handle the request from the url scheme ;)
NSString *pageName = [[NSString stringWithFormat:@"%@", url.host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([pageName isEqualToString:@"openAboutUS"]) {
[[NSNotificationCenter defaultCenter]postNotificationName:@"OpenAboutUsNotification" object:nil];
}
return YES;
}
@end
/******* Within the Main ViewController *******/
@interface MainViewController : UIViewController
@end
@implementation MainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// add notification to open the requested page
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(aboutPresd:) name:@"OpenAboutUsNotification" object:nil];
}
- (void)aboutPresd:(id)sender {
// i used to push it from the storyboard u can use any other method
UIViewController *aboutPage = [self.storyboard instantiateViewControllerWithIdentifier:@"aboutPageID"];
[self.navigationController pushViewController:aboutPage animated:YES];
}
@end
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment