Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

ios - Can Siri be invoked programmatically within my app using private APIs?

Instead of having the user to hold the home button, I would like to programmatically launch Siri within my application. Since there are no public APIs available to accomplish this, I would like to know, if anybody has been able to programmatically launch Siri via private API and if so which private API did you use?

I am not interested in any return values from Siri. All I want to do is to launch it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer depends a bit on whether this is for an app that just won't go to the App Store (enterprise app, or personal/hobby app), or whether it will actually be run on jailbroken phones.

If you can rely on a jailbroken phone, and jailbreak utilities like MobileSubstrate, then I believe you can implement a method to open Siri just like Ryan Petrich's libActivator does, as I show in this other answer.

However, if you are building for normal, jailed phones, I still think you can "hack" it (with Private APIs), by simulating the way the user opens Siri. First, press the Home button, hold it for a bit, then release it.

This code works for me (iOS 6.1):

#import "GSEvent.h"

and

- (void)launchSiri {
   [self simulateTouchEvent: kGSEventMenuButtonDown];

   double delayInSeconds = 1.0;
   dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
   dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
      [self simulateTouchEvent: kGSEventMenuButtonUp];
   });
}

- (void)simulateTouchEvent: (GSEventType)type
{
   struct GSEventRecord record;
   memset(&record, 0, sizeof(record));
   record.type = type;
   record.timestamp = GSCurrentEventTimestamp();
   GSSendSystemEvent(&record);  
}

This relies on having the GSEvent.h header, which isn't part of the public set of headers. I believe I got mine here, as well as GSWindow.h that it pulls in. Obviously, you'd then need to download these two headers and add them to your project.

This code is in the GraphicsServices private framework, so you'll also need to add that framework to your project. Do so just like adding a normal framework, but you need to browse to somewhere like

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/System/Library/PrivateFrameworks/GraphicsServices.framework

to find it (path adjusted for your Xcode installation directory, and SDK).

Disclaimer: I did test this on a jailbroken phone, but it was definitely inside a normal app, installed in the /var/mobile/Applications/ sandbox area, and I'm 99% sure I haven't done anything that depends on being jailbroken.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...