ios – Find out how to go knowledge from a Swift ViewController to an Obj-C ViewController?


I’m attempting to go some easy knowledge from my ViewController in Swift to a ViewController in Goal C however I’ve little expertise with Obj-C and I am unable to discover any info on learn how to accomplish it.

Right here is my Swift ViewController code:

func navigateToUpdateViewController(imageURL: URL) {
        let storyboard = UIStoryboard(title: "Important", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "suotaViewController") as! SUOTAViewController
        vc.imageURL = imageURL
        self.navigationController?.pushViewController(vc, animated: true)
    }

I’m attempting to determine learn how to add the code on the Obj-C facet to make this code work. I simply want the imageURL property to be set. I’ve tried this code however I do not actually perceive it and it does not even work:

@property (nonatomic, retain) NSURL *imageURL;

Right here is the Obj-C ViewController:

#outline UIALERTVIEW_TAG_REBOOT 1

#import "SUOTAViewController.h"

@interface SUOTAViewController ()

@property (nonatomic, retain) NSURL *imageURL;

@finish

@implementation SUOTAViewController

@synthesize blockSize;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textView.textual content = @"";
    
    storage = [ParameterStorage getInstance];
    supervisor = storage.supervisor;
    
    [self.progressView setProgress:0];
    [self.progressTextLabel setText:[NSString stringWithFormat:@"%d%%", 0]];
    
    // Allow notifications on the standing attribute
    [manager setNotifications:GenericServiceManager.SPOTA_SERVICE_CBUUID characteristicUUID:GenericServiceManager.SPOTA_SERV_STATUS_CBUUID enable:YES];
}

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    
    // Allow notifications for BLE occasions
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didDisconnectFromDevice:)
                                                 title:BluetoothManagerDisconnectedFromDevice
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didUpdateValueForCharacteristic:)
                                                 title:GenericServiceManagerDidReceiveValue
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didSendValueForCharacteristic:)
                                                 title:GenericServiceManagerDidSendValue
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onBleOperationError:)
                                                 title:GenericServiceManagerWriteError
                                               object:nil];

    step = 1;
    [self doStep];
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].idleTimerDisabled = NO;

    // Disable notifications for BLE occasions
    [[NSNotificationCenter defaultCenter] removeObserver:self title:BluetoothManagerDisconnectedFromDevice object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self title:GenericServiceManagerDidReceiveValue object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self title:GenericServiceManagerDidSendValue object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self title:GenericServiceManagerWriteError object:nil];
}

- (void) didDisconnectFromDevice: (NSNotification*)notification {
    if (step != 8) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:step != 7 ? @"Add Failed" : @"System Disconnected"
                                                            message:@"The connection to the distant system was misplaced."
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
}

- (void) didUpdateValueForCharacteristic: (NSNotification*)notification {
    CBCharacteristic *attribute = (CBCharacteristic*) notification.object;
    if ([characteristic.UUID isEqual:GenericServiceManager.SPOTA_SERV_STATUS_CBUUID]) {
        char worth;
        [characteristic.value getBytes:&value length:sizeof(char)];
        
        NSString *message = [self getErrorMessage:value];
        [self debug:message UILog:(value != SPOTAR_CMP_OK)];
        
        if (expectedValue != 0) {
            // Test if worth equals the anticipated worth
            if (worth == expectedValue) {
                // If that's the case, proceed with the subsequent step
                step = nextStep;
                
                expectedValue = 0; // Reset
                
                [self doStep];
            } else {
                // Else show an error message
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
                
                expectedValue = 0; // Reset
            }
        }
    }
}

- (void) didSendValueForCharacteristic: (NSNotification*)notification {
    if (step && step != 7) {
        [self doStep];
    }
}

- (void) onBleOperationError: (NSNotification*)notification {
    [self debug:[NSString stringWithFormat:@"Error in BLE operation on characteristic %@", ((CBCharacteristic*)notification.object).UUID] UILog:YES];
    if (step != 8) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Add Failed"
                                                            message:@"The firmware add process encountered an error."
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    }
}

- (void) doStep {
    [self debug:[NSString stringWithFormat:@"*** Next step: %d", step] UILog:NO];
    
    swap (step) {
        case 1:  self.memoryBank;
            [self debug:[NSString stringWithFormat:@"Set SPOTA_MEM_DEV: %#010x", _memDevData] UILog:YES];
            NSData *memDevData = [NSData dataWithBytes:&_memDevData length:sizeof(uint32_t)];
            [manager writeValue:GenericServiceManager.SPOTA_SERVICE_CBUUID characteristicUUID:GenericServiceManager.SPOTA_MEM_DEV_CBUUID data:memDevData];
            break;
        
            
        case 2: {
            // Step 2: Set reminiscence params
            uint32_t _memInfoData = 0;
            if (self.memoryType == MEM_TYPE_SUOTA_SPI)  self.spiSCKGPIO;
             else if (self.memoryType == MEM_TYPE_SUOTA_I2C)  (self.i2cSCLGPIO << 8) 
            [self debug:[NSString stringWithFormat:@"Set SPOTA_GPIO_MAP: %#010x", _memInfoData] UILog:YES];
            NSData *memInfoData = [NSData dataWithBytes:&_memInfoData length:sizeof(uint32_t)];
            
            step = 3;
            [manager writeValue:GenericServiceManager.SPOTA_SERVICE_CBUUID characteristicUUID:GenericServiceManager.SPOTA_GPIO_MAP_CBUUID data:memInfoData];
            break;
        }
            
        case 3: {
            // Load patch knowledge
            [self debug:[NSString stringWithFormat:@"Loading data from %@", storage.file_url.absoluteString.stringByRemovingPercentEncoding] UILog:YES];
            fileData = [[NSData dataWithContentsOfURL:storage.file_url] mutableCopy];
            [self appendChecksum];
            [self debug:[NSString stringWithFormat:@"Upload size: %d bytes", (int) [fileData length]] UILog:YES];
            
            // Step 3: Set patch size
            chunkSize = MIN(supervisor.suotaPatchDataSize, supervisor.suotaMtu - 3);
            blockSize = MAX(blockSize, chunkSize);
            if (blockSize > fileData.size) {
                blockSize = fileData.size;
                if (chunkSize > blockSize)
                    chunkSize = blockSize;
            }
            blockStartByte = 0;
            [self debug:[NSString stringWithFormat:@"Chunk size: %d bytes", chunkSize] UILog:YES];

            step = 4;
            [self doStep];
            break;
        }
            
        case 4: {
            // Set patch size
            [self debug:[NSString stringWithFormat:@"Set SPOTA_PATCH_LEN: %d", blockSize] UILog:YES];
            NSData *patchLengthData = [NSData dataWithBytes:&blockSize length:sizeof(uint16_t)];
            
            step = 5;
            
            [manager writeValue:GenericServiceManager.SPOTA_SERVICE_CBUUID characteristicUUID:GenericServiceManager.SPOTA_PATCH_LEN_CBUUID data:patchLengthData];
            break;
        }
            
        case 5: {
            // Ship present block in chunks of 20 bytes
            if (blockStartByte == 0)
                [self debug:@"Upload procedure started" UILog:YES];

            step = 0;
            expectedValue = 0x02;
            nextStep = 5;
            
            int dataLength = (int) [fileData length];
            int chunkStartByte = 0;
            
            whereas (chunkStartByte < blockSize) {
                
                // Test if we have now lower than present block-size bytes remaining
                int bytesRemaining = blockSize - chunkStartByte;
                int currChunkSize = bytesRemaining >= chunkSize ? chunkSize : bytesRemaining;

                [self debug:[NSString stringWithFormat:@"Sending bytes %d to %d (%d/%d) of %d", blockStartByte + chunkStartByte + 1, blockStartByte + chunkStartByte + currChunkSize, chunkStartByte + currChunkSize, blockSize, dataLength] UILog:NO];
                
                double progress = (double)(blockStartByte + chunkStartByte + currChunkSize) / (double)dataLength;
                [self.progressView setProgress:progress];
                [self.progressTextLabel setText:[NSString stringWithFormat:@"%d%%", (int)(100 * progress)]];
                
                // Step 4: Ship subsequent n bytes of the patch
                char bytes[currChunkSize];
                [fileData getBytes:bytes range:NSMakeRange(blockStartByte + chunkStartByte, currChunkSize)];
                NSData *byteData = [NSData dataWithBytes:bytes length:currChunkSize];
                
                // On to the chunk
                chunkStartByte += currChunkSize;
                
                // Test if we're passing the present block
                if (chunkStartByte >= blockSize) {
                    // Put together for subsequent block
                    blockStartByte += blockSize;
                    
                    int bytesRemaining = dataLength - blockStartByte;
                    if (bytesRemaining == 0) {
                        nextStep = 6;
                        
                    } else if (bytesRemaining < blockSize) {
                        blockSize = bytesRemaining;
                        nextStep = 4; // Again to step 4, setting the patch size
                    }
                }
            
                [manager writeValueWithoutResponse:GenericServiceManager.SPOTA_SERVICE_CBUUID characteristicUUID:GenericServiceManager.SPOTA_PATCH_DATA_CBUUID data:byteData];
            }
            
            break;
        }
            
        case 6: {
            // Ship SUOTA END command
            step = 0;
            expectedValue = 0x02;
            nextStep = 7;

            uint32_t suotaEnd = 0xFE000000;
            [self debug:[NSString stringWithFormat:@"Send SUOTA END command: %#010x", suotaEnd] UILog:YES];
            NSData *suotaEndData = [NSData dataWithBytes:&suotaEnd length:sizeof(uint32_t)];
            [manager writeValue:GenericServiceManager.SPOTA_SERVICE_CBUUID characteristicUUID:GenericServiceManager.SPOTA_MEM_DEV_CBUUID data:suotaEndData];
            break;
        }
            
        case 7: {
            [self debug:@"Upload completed" UILog:YES];
            NSTimeInterval elapsed = [[NSDate date] timeIntervalSinceDate:uploadStart];
            [self debug:[NSString stringWithFormat:@"Elapsed time: %.3f", elapsed] UILog:YES];
            // Watch for person to verify reboot
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"System has been up to date" message:@"Do you want to reboot the system?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Sure, reboot", nil];
            [alert setTag:UIALERTVIEW_TAG_REBOOT];
            [alert show];
            break;
        }
            
        case 8: {
            // Return to overview of gadgets
            [self dismissViewControllerAnimated:YES completion:nil];
            break;
        }
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == UIALERTVIEW_TAG_REBOOT) {
        if (buttonIndex != alertView.cancelButtonIndex) {
            // Ship reboot sign to system
            step = 8;
            uint32_t suotaReboot = 0xFD000000;
            [self debug:[NSString stringWithFormat:@"Send SUOTA REBOOT command: %#010x", suotaReboot] UILog:YES];
            NSData *suotaRebootData = [NSData dataWithBytes:&suotaReboot length:sizeof(uint32_t)];
            [manager writeValue:GenericServiceManager.SPOTA_SERVICE_CBUUID characteristicUUID:GenericServiceManager.SPOTA_MEM_DEV_CBUUID data:suotaRebootData];
        }
    }
}

- (void) debug:(NSString*)message UILog:(BOOL)uiLog {
    if (uiLog) {
        self.textView.textual content = [self.textView.text stringByAppendingString:[NSString stringWithFormat:@"n%@", message]];
        [self.textView scrollRangeToVisible:NSMakeRange([self.textView.text length], 0)];
    }
    NSLog(@"%@", message);
}

- (void) appendChecksum {
    uint8_t crc_code = 0;
    
    const char *bytes = [fileData bytes];
    for (int i = 0; i < [fileData length]; i++) {
        crc_code ^= bytes[i];
    }
    
    [self debug:[NSString stringWithFormat:@"Checksum for file: %#4x", crc_code] UILog:YES];
    
    [fileData appendBytes:&crc_code length:sizeof(uint8_t)];
}

@finish

Leave a Reply