小编Ale*_*erø的帖子

目标C:如何使用HTTP POST上传图像和文本?

我已成功创建了两种不同的方法,每种方法都可以上传图像或文本.但我在写一个可以同时发布文本和图像的方法时遇到问题!

//这是我的新方法,巫婆比@sgosha更好地工作:

- (void) upload {
    NSString *urlString = @"http://www.examplescript.com";
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSMutableData *body = [NSMutableData data];


    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];

    // file
    NSData *imageData = UIImageJPEGRepresentation(imageView.image, 90);

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"userfile\"; filename=\".jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

    // Text parameter1
    NSString *param1 = @"parameter …
Run Code Online (Sandbox Code Playgroud)

iphone file-upload objective-c http-post ipad

36
推荐指数
2
解决办法
5万
查看次数

Objective-c,如何从另一个类访问实例变量

我习惯用Java编程并使用类变量来访问其他类的数据.然后我发现类变量在Obj-C中的工作方式不同,并且遇到了问题.

我的问题是我想在用户登录后访问另一个类中的用户输入密码.已经阅读了不同的论坛,我应该使用类方法(+)来访问这些数据.但是因为我需要在第二类中创建第一个类的新实例,这意味着输入的密码不存在于第一类的新实例中.

我的代码如下:

class1.h

@interface class1 : UIViewController {
    UITextField *usernameField;
    UITextField *passwordField;
        UIButton *loginButton;
        NSString *password;
}

@property (nonatomic, retain) IBOutlet UITextField *usernameField;
@property (nonatomic, retain) IBOutlet UITextField *passwordField;
@property (nonatomic, retain) IBOutlet UIButton *loginButton;
@property (nonatomic, retain) NSString *password;

-(IBAction) loginButtonPushed;
+(NSString *)password;

@end
Run Code Online (Sandbox Code Playgroud)

class1.m

#import "viewSwitcherViewController.h"

@implementation viewSwitcherViewController

@synthesize usernameField;
@synthesize passwordField;
@synthesize loginButton;
@synthesize password; // not needed

-(IBAction) loginButtonPushed {
        password = passwordField.text; //not needed
    // ...Code for switching view if successful login... EDITED:
    class2 …
Run Code Online (Sandbox Code Playgroud)

objective-c class-reference

7
推荐指数
2
解决办法
2万
查看次数