示例#1
0
        public unsafe bool ConnectViaHouseArrest(string bundleIdentifier)
        {
            if (_connected)
            {
                MobileDevice.AFCConnectionClose(hAFC);
                _connected = false;
            }

            var connectResult = MobileDevice.AMDeviceConnect(iPhoneHandle);

            if (connectResult == 1)
            {
                throw new Exception("Phone in recovery mode, support not yet implemented.");
            }
            if (MobileDevice.AMDeviceIsPaired(iPhoneHandle) == 0)
            {
                return(false);
            }
            if (MobileDevice.AMDeviceValidatePairing(iPhoneHandle) != 0)
            {
                return(false);
            }
            if (MobileDevice.AMDeviceStartSession(iPhoneHandle) == 1)
            {
                return(false);
            }

            if (string.IsNullOrEmpty(bundleIdentifier))
            {
                Console.WriteLine("Bundle identifier cannot be null when using house arrest service.");
                return(false);
            }

            // Connect via house arrest, only Documents directory is accessible on device
            if (MobileDevice.AMDeviceStartHouseArrestService(iPhoneHandle,
                                                             MobileDevice.__CFStringMakeConstantString(MobileDevice.StringToCString(bundleIdentifier)), null,
                                                             ref hService, 0) != 0)
            {
                Console.WriteLine("Unable to find bundle with id: {0}", bundleIdentifier);
                return(false);
            }

            // Need to stop session and disconnect for house arrest to work, comment out for AFC
            if (MobileDevice.AMDeviceStopSession(iPhoneHandle) != 0)
            {
                return(false);
            }
            if (MobileDevice.AMDeviceDisconnect(iPhoneHandle) != 0)
            {
                return(false);
            }

            if (MobileDevice.AFCConnectionOpen(hService, 0, ref hAFC) != 0)
            {
                return(false);
            }

            _connected = true;
            return(true);
        }