origin
Today, I watched the video with Baofeng Video, and found that it has a function, wifi uploading, which felt very interesting, so I looked up the relevant content on the Internet.
The principle of
Use CocoaHTTPServer framework, set up a local server on the iOS side, as long as the computer and mobile phone connected to the same hot spot or network, you can achieve through the computer browser to access the iOS server page, using POST file upload.
implementation
1. Download CocoaHTTPServer
2. Import the Core folder from the cocoaHttpServer-master directory
3. Import Samples/SimpleFileUploadServer MyHTTPConnection class files and web folder in the directory
When importing web folders, be sure to use a real directory, not xcode’s virtual directory
4. Import the CocoaAsyncSocket and CocoaLumberjack folders in the Vendor directory
5. Open the myhttpConnection. m file, Pragma mark Multipart form data Parser Delegate jump or find 139 lines *- (void) ProcessStartOfPartWithHeader (MultipartMessageHeader) header method, the line 151 uploadDirPath instead
NSString *uploadDirPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
Copy the code
This path is where the uploaded files are stored
6. Configure server startup in an appropriate place. Here’s an example of an AppDelegate
#import "AppDelegate.h"
#import <ifaddrs.h>
#import <arpa/inet.h>
#import "HTTPServer.h"
#import "DDLog.h"
#import "DDTTYLogger.h"
#import "MyHTTPConnection.h"
@interface AppDelegate(a)
@property (nonatomic.strong) HTTPServer * httpServer;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_httpServer = [[HTTPServer alloc] init];
[_httpServer setPort:1234];
[_httpServer setType:@"_http._tcp."];
// webPath is the path where the server searches for files such as HTML
NSString * webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"web"];
[_httpServer setDocumentRoot:webPath];
[_httpServer setConnectionClass:[MyHTTPConnection class]].NSError *err;
if ([_httpServer start:&err]) {
NSLog(@"port %hu",[_httpServer listeningPort]);
}else{
NSLog(@ "% @",err);
}
NSString *ipStr = [self getIpAddresses];
NSLog(IP address % @ "@", ipStr);
return YES;
}
- (NSString *)getIpAddresses{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr ! =NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((structsockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; }}// Free memory
freeifaddrs(interfaces);
return address;
}
Copy the code
7. After running, the console will print out the port number and IP. Enter IP + port number in the PC browser to access.
8. If the file is successfully uploaded, the file name will be displayed on the web page. You can check whether the file is successfully uploaded in the sandbox