18033

Can we open the contacts on watch extension

Question:

How to open up the contacts of iPhone programmatically in watch extension as we do in iOS using AddressBook.

Thanks in advance

Answer1:

Your app runs on the phone, so you can fetch the contacts and send it to watch

Answer2:

In general to communicate with iPhone from your WatchKit extension you use

+ (BOOL)openParentApplication:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo, NSError *error)) reply; // launches containing iOS application on the phone. userInfo must be non-nil

method of WKInterfaceController class.

So for example, you can attach IBAction from your button in Storyboard to this method

- (IBAction)callPhoneAppButtonTapped { NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"text to display on iPhone", @"key", nil]; [InterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) { NSLog(@"Reply received by Watch app: %@", replyInfo); }]; }

<strong>Note:</strong> In order to fetch data from Address Book user needs to grant your app permission. But app will be launched in background and user will be focused on Watch so it will be better to ask for this permission in your iPhone app.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) { if (!granted){ NSLog(@"Access denied"); return; } NSLog(@"Access granted"); }); }

In order to handle message sent by openParentApplication:reply in your AppDelegate implement

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply { NSLog(@"Request received by iOS app"); NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"your value to return to Apple Watch", @"key", nil]; // Here your app will be launch in background. Fetch AddressBook or other data you need. // Remember to call reply block in the end. // Example of saving data to Address Book NSString *firstName; NSString *lastName; firstName = @"Maggie"; lastName = @"Peggie"; ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil); ABRecordRef contact = ABPersonCreate(); ABRecordSetValue(contact, kABPersonFirstNameProperty, (__bridge CFStringRef) firstName, nil); ABRecordSetValue(contact, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, nil); ABAddressBookAddRecord(addressBookRef, contact, nil); ABAddressBookSave(addressBookRef, nil); reply(dictionary); }

Answer3:

While the current versions of Apple Watch apps cannot themselves execute code, your WatchKit Extension runs on the phone and can access all of the iPhone APIs that a standard iOS application can. As developers, we are much more limited in how we can programmatically change the interface, but not in what is done in terms of accessing services.

Therefore, there is no technical requirement to access Address Book data via your iOS app—you could make these requests for Address Book data directly. If these methods execute rapidly, the choice of whether to do this directly in the Extension or in your iPhone app would come down to decisions about what would minimise code complexity and thus maximise code maintainability. Apple have indicated that latency in communication between the iPhone app and WatchKit <em>Extension</em> can largely be ignored as it will be trivial. (It is latency between the Extension, running on the phone, and the Watch app that we need to be focussed on.)

However, we have also been told that WatchKit Extensions may be immediately terminated when Watch apps are, and we need to be prepared for engagement time measured in seconds, not minutes. WatchKit Extensions are not given the kind of latitude that iPhone apps are to complete things in the background after the user interface has terminated. Therefore, the recommendation is that anything that may be more time consuming or which needs to be completed for data integrity should be run in the iPhone app. lvp's answer gives code that could assist with that.

Recommend

  • Sitecore folder & IIS permissions
  • Are chrome apps code visible to the users?
  • How to debug office add-in for mac?
  • How to run .ear file in JBoss 6?
  • Force iPad app to launch in portrait when landscape is supported
  • How many percent of the tweets does twitter sample API give?
  • Which table should be Parent table and which should be child table?
  • Capturing STDOUT in RSpec
  • ASP.NET MVC2 Error: No parameterless constructor defined for this object
  • Adding a new element into the DOM with angularjs does not initiate it
  • NRefactory: How do I access unresolved Named Arguments on a Property Attribute?
  • HighCharts - Show tooltip on column where value is 0 or null
  • Change device language on Android 6.0 (Android M)
  • Retrieve IP address of device
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • cygwin cannot exec 'git-add--interactive' permission denied
  • How to set ini file attributes during an Inno install
  • Meteor: Do Something On Email Verification Confirmation
  • Breeze - Deleted Items nav properties bug
  • Cannot resolve symbol 'MyApi'
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • javaw.exe and eclipse startup problems
  • Java applet as stand-alone Windows application?
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • using HTMLImports.whenReady not working in chrome
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • embed rChart in Markdown
  • need help with bizarre java.net.HttpURLConnection behavior
  • Authorize attributes not working in MVC 4
  • Busy indicator not showing up in wpf window [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF